def convert_gemm(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:6>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>bias_name = '<STR_LIT>'.format(w_name)<EOL>weights_name = '<STR_LIT>'.format(w_name)<EOL>W = weights[weights_name].numpy().transpose()<EOL>input_channels, output_channels = W.shape<EOL>keras_weights = [W]<EOL>has_bias = False<EOL>if bias_name in weights:<EOL><INDENT>bias = weights[bias_name].numpy()<EOL>keras_weights = [W, bias]<EOL>has_bias = True<EOL><DEDENT>dense = keras.layers.Dense(<EOL>output_channels,<EOL>weights=keras_weights, use_bias=has_bias, name=tf_name, bias_initializer='<STR_LIT>', kernel_initializer='<STR_LIT>',<EOL>)<EOL>layers[scope_name] = dense(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert Linear.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_matmul(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>if len(inputs) == <NUM_LIT:1>:<EOL><INDENT>weights_name = '<STR_LIT>'.format(w_name)<EOL>W = weights[weights_name].numpy().transpose()<EOL>input_channels, output_channels = W.shape<EOL>keras_weights = [W]<EOL>dense = keras.layers.Dense(<EOL>output_channels,<EOL>weights=keras_weights, use_bias=False, name=tf_name, bias_initializer='<STR_LIT>', kernel_initializer='<STR_LIT>',<EOL>)<EOL>layers[scope_name] = dense(layers[inputs[<NUM_LIT:0>]])<EOL><DEDENT>elif len(inputs) == <NUM_LIT:2>:<EOL><INDENT>weights_name = '<STR_LIT>'.format(w_name)<EOL>W = weights[weights_name].numpy().transpose()<EOL>input_channels, output_channels = W.shape<EOL>keras_weights = [W]<EOL>dense = keras.layers.Dense(<EOL>output_channels,<EOL>weights=keras_weights, use_bias=False, name=tf_name, bias_initializer='<STR_LIT>', kernel_initializer='<STR_LIT>',<EOL>)<EOL>layers[scope_name] = dense(layers[inputs[<NUM_LIT:0>]])<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>

Convert matmul layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_relu(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>relu = keras.layers.Activation('<STR_LIT:relu>', name=tf_name)<EOL>layers[scope_name] = relu(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert relu layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_lrelu(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:3>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>leakyrelu =keras.layers.LeakyReLU(alpha=params['<STR_LIT>'], name=tf_name)<EOL>layers[scope_name] = leakyrelu(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert leaky relu layer.

Args:
     params: dictionary with layer parameters
     w_name: name prefix in state_dict
     scope_name: pytorch scope name
     inputs: pytorch node inputs
     layers: dictionary with keras tensors
     weights: pytorch state_dict
     names: use short names for keras layers


def convert_sigmoid(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>sigmoid = keras.layers.Activation('<STR_LIT>', name=tf_name)<EOL>layers[scope_name] = sigmoid(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert sigmoid layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_softmax(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>def target_layer(x, dim=params['<STR_LIT>']):<EOL><INDENT>import keras<EOL>return keras.activations.softmax(x, axis=dim)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert softmax layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_tanh(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>tanh = keras.layers.Activation('<STR_LIT>', name=tf_name)<EOL>layers[scope_name] = tanh(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert tanh layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_hardtanh(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>def target_layer(x, max_val=float(params['<STR_LIT>']), min_val=float(params['<STR_LIT>'])):<EOL><INDENT>return tf.minimum(max_val, tf.maximum(min_val, x))<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert hardtanh layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_selu(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>selu = keras.layers.Activation('<STR_LIT>', name=tf_name)<EOL>layers[scope_name] = selu(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert selu layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_gather(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>weights_name = '<STR_LIT>'.format(w_name)<EOL>W = weights[weights_name].numpy()<EOL>input_channels, output_channels = W.shape<EOL>keras_weights = [W]<EOL>dense = keras.layers.Embedding(<EOL>input_channels,<EOL>weights=keras_weights, output_dim=output_channels, name=tf_name<EOL>)<EOL>layers[scope_name] = dense(layers[inputs[<NUM_LIT:1>]])<EOL>

Convert gather (embedding) layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


return '<STR_LIT>'.join(random.choice(string.ascii_letters) for _ in range(length))<EOL>

Generate a random string for the layer name.
:param length: a length of required random string
:return: generated random string


def convert_upsample_bilinear(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>output_size = params['<STR_LIT>']<EOL>align_corners = params['<STR_LIT>'] > <NUM_LIT:0><EOL>def target_layer(x, size=output_size, align_corners=align_corners):<EOL><INDENT>import tensorflow as tf<EOL>x = tf.transpose(x, [<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:1>])<EOL>x = tf.image.resize_images(x, size, align_corners=align_corners)<EOL>x = tf.transpose(x, [<NUM_LIT:0>, <NUM_LIT:3>, <NUM_LIT:1>, <NUM_LIT:2>])<EOL>return x<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert upsample_bilinear2d layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_upsample(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if params['<STR_LIT>'] != '<STR_LIT>':<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>scale = (params['<STR_LIT>'], params['<STR_LIT>'])<EOL><DEDENT>elif len(inputs) == <NUM_LIT:2>:<EOL><INDENT>scale = layers[inputs[-<NUM_LIT:1>] + '<STR_LIT>'][-<NUM_LIT:2>:]<EOL><DEDENT>upsampling = keras.layers.UpSampling2D(<EOL>size=scale, name=tf_name<EOL>)<EOL>layers[scope_name] = upsampling(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert nearest upsampling layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_elementwise_add(<EOL>params, w_name, scope_name, inputs, layers, weights, names<EOL>):

print('<STR_LIT>')<EOL>if '<STR_LIT>' in params:<EOL><INDENT>model0 = layers[inputs[<NUM_LIT:0>]]<EOL>model1 = layers[inputs[<NUM_LIT:1>]]<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:A>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>def target_layer(x):<EOL><INDENT>layer = tf.add(x[<NUM_LIT:0>], x[<NUM_LIT:1>])<EOL>return layer<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer, name=tf_name)<EOL>layers[scope_name] = lambda_layer([layers[inputs[<NUM_LIT:0>]], layers[inputs[<NUM_LIT:1>]]])<EOL><DEDENT>else:<EOL><INDENT>model0 = layers[inputs[<NUM_LIT:0>]]<EOL>model1 = layers[inputs[<NUM_LIT:1>]]<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:A>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>add = keras.layers.Add(name=tf_name)<EOL>layers[scope_name] = add([model0, model1])<EOL><DEDENT>

Convert elementwise addition.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_elementwise_mul(<EOL>params, w_name, scope_name, inputs, layers, weights, names<EOL>):

print('<STR_LIT>')<EOL>model0 = layers[inputs[<NUM_LIT:0>]]<EOL>model1 = layers[inputs[<NUM_LIT:1>]]<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:M>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>def target_layer(x):<EOL><INDENT>layer = tf.multiply(<EOL>x[<NUM_LIT:0>],<EOL>x[<NUM_LIT:1>]<EOL>)<EOL>return layer<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer, name=tf_name)<EOL>layers[scope_name] = lambda_layer([layers[inputs[<NUM_LIT:0>]], layers[inputs[<NUM_LIT:1>]]])<EOL>

Convert elementwise multiplication.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_elementwise_div(<EOL>params, w_name, scope_name, inputs, layers, weights, names<EOL>):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:D>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>def target_layer(x):<EOL><INDENT>layer = tf.div(<EOL>x[<NUM_LIT:0>],<EOL>x[<NUM_LIT:1>]<EOL>)<EOL>return layer<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer, name=tf_name)<EOL>layers[scope_name] = lambda_layer([layers[inputs[<NUM_LIT:0>]], layers[inputs[<NUM_LIT:1>]]])<EOL>


def convert_elementwise_sub(<EOL>params, w_name, scope_name, inputs, layers, weights, names<EOL>):

print('<STR_LIT>')<EOL>model0 = layers[inputs[<NUM_LIT:0>]]<EOL>model1 = layers[inputs[<NUM_LIT:1>]]<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:S>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>sub = keras.layers.Subtract(name=tf_name)<EOL>layers[scope_name] = sub([model0, model1])<EOL>

Convert elementwise subtraction.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_padding(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if params['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>if params['<STR_LIT:value>'] != <NUM_LIT:0.0>:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>if names:<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>padding_name = tf_name<EOL>padding_layer = keras.layers.ZeroPadding2D(<EOL>padding=((params['<STR_LIT>'][<NUM_LIT:2>], params['<STR_LIT>'][<NUM_LIT:6>]), (params['<STR_LIT>'][<NUM_LIT:3>], params['<STR_LIT>'][<NUM_LIT:7>])),<EOL>name=padding_name<EOL>)<EOL>layers[scope_name] = padding_layer(layers[inputs[<NUM_LIT:0>]])<EOL><DEDENT>elif params['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>def target_layer(x, pads=params['<STR_LIT>']):<EOL><INDENT>layer = tf.pad(x, [[<NUM_LIT:0>, <NUM_LIT:0>], [<NUM_LIT:0>, <NUM_LIT:0>], [pads[<NUM_LIT:2>], pads[<NUM_LIT:6>]], [pads[<NUM_LIT:3>], pads[<NUM_LIT:7>]]], '<STR_LIT>')<EOL>return layer<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL><DEDENT>

Convert padding layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_constant(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>params_list = params['<STR_LIT:value>'].numpy()<EOL>def target_layer(x, value=params_list):<EOL><INDENT>return tf.constant(value.tolist(), shape=value.shape)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name + '<STR_LIT>'] = params_list  <EOL>layers[scope_name] = lambda_layer(layers[list(layers.keys())[<NUM_LIT:0>]])<EOL>

Convert constant layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_conv(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:C>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>bias_name = '<STR_LIT>'.format(w_name)<EOL>weights_name = '<STR_LIT>'.format(w_name)<EOL>input_name = inputs[<NUM_LIT:0>]<EOL>if len(weights[weights_name].numpy().shape) == <NUM_LIT:5>:  <EOL><INDENT>W = weights[weights_name].numpy().transpose(<NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>, <NUM_LIT:1>, <NUM_LIT:0>)<EOL>height, width, channels, n_layers, n_filters = W.shape<EOL>if bias_name in weights:<EOL><INDENT>biases = weights[bias_name].numpy()<EOL>has_bias = True<EOL><DEDENT>else:<EOL><INDENT>biases = None<EOL>has_bias = False<EOL><DEDENT>if params['<STR_LIT>'][<NUM_LIT:0>] > <NUM_LIT:0> or params['<STR_LIT>'][<NUM_LIT:1>] > <NUM_LIT:0>:<EOL><INDENT>padding_name = tf_name + '<STR_LIT>'<EOL>padding_layer = keras.layers.ZeroPadding3D(<EOL>padding=(params['<STR_LIT>'][<NUM_LIT:0>],<EOL>params['<STR_LIT>'][<NUM_LIT:1>],<EOL>params['<STR_LIT>'][<NUM_LIT:2>]),<EOL>name=padding_name<EOL>)<EOL>layers[padding_name] = padding_layer(layers[input_name])<EOL>input_name = padding_name<EOL><DEDENT>if has_bias:<EOL><INDENT>weights = [W, biases]<EOL><DEDENT>else:<EOL><INDENT>weights = [W]<EOL><DEDENT>conv = keras.layers.Conv3D(<EOL>filters=n_filters,<EOL>kernel_size=(channels, height, width),<EOL>strides=(params['<STR_LIT>'][<NUM_LIT:0>],<EOL>params['<STR_LIT>'][<NUM_LIT:1>],<EOL>params['<STR_LIT>'][<NUM_LIT:2>]),<EOL>padding='<STR_LIT>',<EOL>weights=weights,<EOL>use_bias=has_bias,<EOL>activation=None,<EOL>dilation_rate=params['<STR_LIT>'][<NUM_LIT:0>],<EOL>bias_initializer='<STR_LIT>', kernel_initializer='<STR_LIT>',<EOL>name=tf_name<EOL>)<EOL>layers[scope_name] = conv(layers[input_name])<EOL><DEDENT>elif len(weights[weights_name].numpy().shape) == <NUM_LIT:4>:  <EOL><INDENT>if params['<STR_LIT>'][<NUM_LIT:0>] > <NUM_LIT:0> or params['<STR_LIT>'][<NUM_LIT:1>] > <NUM_LIT:0>:<EOL><INDENT>padding_name = tf_name + '<STR_LIT>'<EOL>padding_layer = keras.layers.ZeroPadding2D(<EOL>padding=(params['<STR_LIT>'][<NUM_LIT:0>], params['<STR_LIT>'][<NUM_LIT:1>]),<EOL>name=padding_name<EOL>)<EOL>layers[padding_name] = padding_layer(layers[input_name])<EOL>input_name = padding_name<EOL><DEDENT>W = weights[weights_name].numpy().transpose(<NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:1>, <NUM_LIT:0>)<EOL>height, width, channels_per_group, out_channels = W.shape<EOL>n_groups = params['<STR_LIT>']<EOL>in_channels = channels_per_group * n_groups<EOL>if n_groups == in_channels and n_groups != <NUM_LIT:1>:<EOL><INDENT>if bias_name in weights:<EOL><INDENT>biases = weights[bias_name].numpy()<EOL>has_bias = True<EOL><DEDENT>else:<EOL><INDENT>biases = None<EOL>has_bias = False<EOL><DEDENT>W = W.transpose(<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:2>)<EOL>if has_bias:<EOL><INDENT>weights = [W, biases]<EOL><DEDENT>else:<EOL><INDENT>weights = [W]<EOL><DEDENT>conv = keras.layers.DepthwiseConv2D(<EOL>kernel_size=(height, width),<EOL>strides=(params['<STR_LIT>'][<NUM_LIT:0>], params['<STR_LIT>'][<NUM_LIT:1>]),<EOL>padding='<STR_LIT>',<EOL>use_bias=has_bias,<EOL>activation=None,<EOL>depth_multiplier=<NUM_LIT:1>,<EOL>weights = weights,<EOL>dilation_rate=params['<STR_LIT>'][<NUM_LIT:0>],<EOL>bias_initializer='<STR_LIT>', kernel_initializer='<STR_LIT>'<EOL>)<EOL>layers[scope_name] = conv(layers[input_name])<EOL><DEDENT>elif n_groups != <NUM_LIT:1>:<EOL><INDENT>def target_layer(x, groups=params['<STR_LIT>'], stride_y=params['<STR_LIT>'][<NUM_LIT:0>], stride_x=params['<STR_LIT>'][<NUM_LIT:1>]):<EOL><INDENT>x = tf.transpose(x, [<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:1>])<EOL>def convolve_lambda(i, k):<EOL><INDENT>return tf.nn.conv2d(i, k, strides=[<NUM_LIT:1>, stride_y, stride_x, <NUM_LIT:1>], padding='<STR_LIT>')<EOL><DEDENT>input_groups = tf.split(axis=<NUM_LIT:3>, num_or_size_splits=groups, value=x)<EOL>weight_groups = tf.split(axis=<NUM_LIT:3>, num_or_size_splits=groups, value=W.transpose(<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>))<EOL>output_groups = [convolve_lambda(i, k) for i, k in zip(input_groups, weight_groups)]<EOL>layer = tf.concat(axis=<NUM_LIT:3>, values=output_groups)<EOL>layer = tf.transpose(layer, [<NUM_LIT:0>, <NUM_LIT:3>, <NUM_LIT:1>, <NUM_LIT:2>])<EOL>return layer<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[input_name])<EOL><DEDENT>else:<EOL><INDENT>if bias_name in weights:<EOL><INDENT>biases = weights[bias_name].numpy()<EOL>has_bias = True<EOL><DEDENT>else:<EOL><INDENT>biases = None<EOL>has_bias = False<EOL><DEDENT>if has_bias:<EOL><INDENT>weights = [W, biases]<EOL><DEDENT>else:<EOL><INDENT>weights = [W]<EOL><DEDENT>conv = keras.layers.Conv2D(<EOL>filters=out_channels,<EOL>kernel_size=(height, width),<EOL>strides=(params['<STR_LIT>'][<NUM_LIT:0>], params['<STR_LIT>'][<NUM_LIT:1>]),<EOL>padding='<STR_LIT>',<EOL>weights=weights,<EOL>use_bias=has_bias,<EOL>activation=None,<EOL>dilation_rate=params['<STR_LIT>'][<NUM_LIT:0>],<EOL>bias_initializer='<STR_LIT>', kernel_initializer='<STR_LIT>',<EOL>name=tf_name<EOL>)<EOL>layers[scope_name] = conv(layers[input_name])<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>W = weights[weights_name].numpy().transpose(<NUM_LIT:2>, <NUM_LIT:1>, <NUM_LIT:0>)<EOL>width, channels, n_filters = W.shape<EOL>n_groups = params['<STR_LIT>']<EOL>if n_groups > <NUM_LIT:1>:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>if bias_name in weights:<EOL><INDENT>biases = weights[bias_name].numpy()<EOL>has_bias = True<EOL><DEDENT>else:<EOL><INDENT>biases = None<EOL>has_bias = False<EOL><DEDENT>padding_name = tf_name + '<STR_LIT>'<EOL>padding_layer = keras.layers.ZeroPadding1D(<EOL>padding=params['<STR_LIT>'][<NUM_LIT:0>],<EOL>name=padding_name<EOL>)<EOL>layers[padding_name] = padding_layer(layers[inputs[<NUM_LIT:0>]])<EOL>input_name = padding_name<EOL>if has_bias:<EOL><INDENT>weights = [W, biases]<EOL><DEDENT>else:<EOL><INDENT>weights = [W]<EOL><DEDENT>conv = keras.layers.Conv1D(<EOL>filters=channels,<EOL>kernel_size=width,<EOL>strides=params['<STR_LIT>'],<EOL>padding='<STR_LIT>',<EOL>weights=weights,<EOL>use_bias=has_bias,<EOL>activation=None,<EOL>data_format='<STR_LIT>',<EOL>dilation_rate=params['<STR_LIT>'],<EOL>bias_initializer='<STR_LIT>', kernel_initializer='<STR_LIT>',<EOL>name=tf_name<EOL>)<EOL>layers[scope_name] = conv(layers[input_name])<EOL><DEDENT>

Convert convolution layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_convtranspose(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:C>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>bias_name = '<STR_LIT>'.format(w_name)<EOL>weights_name = '<STR_LIT>'.format(w_name)<EOL>if len(weights[weights_name].numpy().shape) == <NUM_LIT:4>:<EOL><INDENT>W = weights[weights_name].numpy().transpose(<NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:1>, <NUM_LIT:0>)<EOL>height, width, n_filters, channels = W.shape<EOL>n_groups = params['<STR_LIT>']<EOL>if n_groups > <NUM_LIT:1>:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>if params['<STR_LIT>'][<NUM_LIT:0>] > <NUM_LIT:1>:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>if bias_name in weights:<EOL><INDENT>biases = weights[bias_name].numpy()<EOL>has_bias = True<EOL><DEDENT>else:<EOL><INDENT>biases = None<EOL>has_bias = False<EOL><DEDENT>input_name = inputs[<NUM_LIT:0>]<EOL>if has_bias:<EOL><INDENT>weights = [W, biases]<EOL><DEDENT>else:<EOL><INDENT>weights = [W]<EOL><DEDENT>conv = keras.layers.Conv2DTranspose(<EOL>filters=n_filters,<EOL>kernel_size=(height, width),<EOL>strides=(params['<STR_LIT>'][<NUM_LIT:0>], params['<STR_LIT>'][<NUM_LIT:1>]),<EOL>padding='<STR_LIT>',<EOL>output_padding=<NUM_LIT:0>,<EOL>weights=weights,<EOL>use_bias=has_bias,<EOL>activation=None,<EOL>dilation_rate=params['<STR_LIT>'][<NUM_LIT:0>],<EOL>bias_initializer='<STR_LIT>', kernel_initializer='<STR_LIT>',<EOL>name=tf_name<EOL>)<EOL>layers[scope_name] = conv(layers[input_name])<EOL>layers[scope_name].set_shape(layers[scope_name]._keras_shape)<EOL>pads = params['<STR_LIT>']<EOL>if pads[<NUM_LIT:0>] > <NUM_LIT:0>:<EOL><INDENT>assert(len(pads) == <NUM_LIT:2> or (pads[<NUM_LIT:2>] == pads[<NUM_LIT:0>] and pads[<NUM_LIT:3>] == pads[<NUM_LIT:1>]))<EOL>crop = keras.layers.Cropping2D(<EOL>pads[:<NUM_LIT:2>],<EOL>name=tf_name + '<STR_LIT>'<EOL>)<EOL>layers[scope_name] = crop(layers[scope_name])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>

Convert transposed convolution layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_avgpool(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:P>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>height, width = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>height, width = params['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>stride_height, stride_width = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>stride_height, stride_width = params['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>padding_h, padding_w, _, _ = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>padding_h, padding_w = params['<STR_LIT>']<EOL><DEDENT>input_name = inputs[<NUM_LIT:0>]<EOL>pad = '<STR_LIT>' <EOL>if height % <NUM_LIT:2> == <NUM_LIT:1> and width % <NUM_LIT:2> == <NUM_LIT:1> andheight // <NUM_LIT:2> == padding_h and width // <NUM_LIT:2> == padding_w andstride_height == <NUM_LIT:1> and stride_width == <NUM_LIT:1>:<EOL><INDENT>pad = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>padding_name = tf_name + '<STR_LIT>'<EOL>padding_layer = keras.layers.ZeroPadding2D(<EOL>padding=(padding_h, padding_w),<EOL>name=padding_name<EOL>)<EOL>layers[padding_name] = padding_layer(layers[inputs[<NUM_LIT:0>]])<EOL>input_name = padding_name<EOL><DEDENT>pooling = keras.layers.AveragePooling2D(<EOL>pool_size=(height, width),<EOL>strides=(stride_height, stride_width),<EOL>padding=pad,<EOL>name=tf_name,<EOL>data_format='<STR_LIT>'<EOL>)<EOL>layers[scope_name] = pooling(layers[input_name])<EOL>

Convert Average pooling.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_maxpool(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:P>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>height, width = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>height, width = params['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>stride_height, stride_width = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>stride_height, stride_width = params['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>padding_h, padding_w, _, _ = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>padding_h, padding_w = params['<STR_LIT>']<EOL><DEDENT>input_name = inputs[<NUM_LIT:0>]<EOL>pad = '<STR_LIT>' <EOL>if height % <NUM_LIT:2> == <NUM_LIT:1> and width % <NUM_LIT:2> == <NUM_LIT:1> andheight // <NUM_LIT:2> == padding_h and width // <NUM_LIT:2> == padding_w andstride_height == <NUM_LIT:1> and stride_width == <NUM_LIT:1>:<EOL><INDENT>pad = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>padding_name = tf_name + '<STR_LIT>'<EOL>padding_layer = keras.layers.ZeroPadding2D(<EOL>padding=(padding_h, padding_w),<EOL>name=padding_name<EOL>)<EOL>layers[padding_name] = padding_layer(layers[inputs[<NUM_LIT:0>]])<EOL>input_name = padding_name<EOL><DEDENT>pooling = keras.layers.MaxPooling2D(<EOL>pool_size=(height, width),<EOL>strides=(stride_height, stride_width),<EOL>padding=pad,<EOL>name=tf_name,<EOL>data_format='<STR_LIT>'<EOL>)<EOL>layers[scope_name] = pooling(layers[input_name])<EOL>

Convert Max pooling.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_maxpool3(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:P>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>height, width, depth = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>height, width, depth = params['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>stride_height, stride_width, stride_depth = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>stride_height, stride_width, stride_depth = params['<STR_LIT>']<EOL><DEDENT>if '<STR_LIT>' in params:<EOL><INDENT>padding_h, padding_w, padding_d, _, _ = params['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>padding_h, padding_w, padding_d = params['<STR_LIT>']<EOL><DEDENT>input_name = inputs[<NUM_LIT:0>]<EOL>if padding_h > <NUM_LIT:0> and padding_w > <NUM_LIT:0> and padding_d > <NUM_LIT:0>:<EOL><INDENT>padding_name = tf_name + '<STR_LIT>'<EOL>padding_layer = keras.layers.ZeroPadding3D(<EOL>padding=(padding_h, padding_w, padding_d),<EOL>name=padding_name<EOL>)<EOL>layers[padding_name] = padding_layer(layers[inputs[<NUM_LIT:0>]])<EOL>input_name = padding_name<EOL><DEDENT>pooling = keras.layers.MaxPooling3D(<EOL>pool_size=(height, width, depth),<EOL>strides=(stride_height, stride_width, stride_depth),<EOL>padding='<STR_LIT>',<EOL>name=tf_name<EOL>)<EOL>layers[scope_name] = pooling(layers[input_name])<EOL>

Convert 3d Max pooling.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_adaptive_avg_pool2d(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>global_pool = keras.layers.GlobalAveragePooling2D(data_format='<STR_LIT>', name=tf_name)<EOL>layers[scope_name] = global_pool(layers[inputs[<NUM_LIT:0>]])<EOL>def target_layer(x):<EOL><INDENT>import keras<EOL>return keras.backend.expand_dims(x)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer, name=tf_name + '<STR_LIT:E>')<EOL>layers[scope_name] = lambda_layer(layers[scope_name])  <EOL>layers[scope_name] = lambda_layer(layers[scope_name])<EOL>

Convert adaptive_avg_pool2d layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_adaptive_max_pool2d(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>global_pool = keras.layers.GlobalMaxPooling2D(data_format='<STR_LIT>', name=tf_name)<EOL>layers[scope_name] = global_pool(layers[inputs[<NUM_LIT:0>]])<EOL>def target_layer(x):<EOL><INDENT>import keras<EOL>return keras.backend.expand_dims(x)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer, name=tf_name + '<STR_LIT:E>')<EOL>layers[scope_name] = lambda_layer(layers[scope_name])  <EOL>layers[scope_name] = lambda_layer(layers[scope_name])<EOL>

Convert convert_adaptive_max_pool2d layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_flatten(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT:R>' + random_string(<NUM_LIT:7>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>reshape = keras.layers.Reshape([-<NUM_LIT:1>], name=tf_name)<EOL>layers[scope_name] = reshape(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert reshape(view).

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_transpose(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if params['<STR_LIT>'][<NUM_LIT:0>] != <NUM_LIT:0>:<EOL><INDENT>if inputs[<NUM_LIT:0>] in layers:<EOL><INDENT>print('<STR_LIT>')<EOL>layers[scope_name] = layers[inputs[<NUM_LIT:0>]]<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if names:<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>permute = keras.layers.Permute(params['<STR_LIT>'][<NUM_LIT:1>:], name=tf_name)<EOL>layers[scope_name] = permute(layers[inputs[<NUM_LIT:0>]])<EOL><DEDENT>

Convert transpose layer.

Args:
     params: dictionary with layer parameters
     w_name: name prefix in state_dict
     scope_name: pytorch scope name
     inputs: pytorch node inputs
     layers: dictionary with keras tensors
     weights: pytorch state_dict
     names: use short names for keras layers


def convert_reshape(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>if len(inputs) > <NUM_LIT:1>:<EOL><INDENT>if layers[inputs[<NUM_LIT:1>]][<NUM_LIT:0>] == -<NUM_LIT:1>:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>reshape = keras.layers.Reshape(layers[inputs[<NUM_LIT:1>] + '<STR_LIT>'], name=tf_name)<EOL>layers[scope_name] = reshape(layers[inputs[<NUM_LIT:0>]])<EOL><DEDENT>else:<EOL><INDENT>if inputs[<NUM_LIT:0>] in layers:<EOL><INDENT>reshape = keras.layers.Reshape(params['<STR_LIT>'][<NUM_LIT:1>:], name=tf_name)<EOL>layers[scope_name] = reshape(layers[inputs[<NUM_LIT:0>]])<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT><DEDENT>

Convert reshape layer.

Args:
     params: dictionary with layer parameters
     w_name: name prefix in state_dict
     scope_name: pytorch scope name
     inputs: pytorch node inputs
     layers: dictionary with keras tensors
     weights: pytorch state_dict
     names: use short names for keras layers


def convert_squeeze(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if len(params['<STR_LIT>']) > <NUM_LIT:1>:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>def target_layer(x, axis=int(params['<STR_LIT>'][<NUM_LIT:0>])):<EOL><INDENT>import tensorflow as tf<EOL>return tf.squeeze(x, axis=axis)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert squeeze operation.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_unsqueeze(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:4>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>def target_layer(x):<EOL><INDENT>import keras<EOL>return keras.backend.expand_dims(x)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer, name=tf_name + '<STR_LIT:E>')<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert unsqueeze operation.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_shape(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>def target_layer(x):<EOL><INDENT>import tensorflow as tf<EOL>return tf.shape(x)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert shape operation.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


if mode is None:<EOL><INDENT>yield<EOL>return<EOL><DEDENT>old_mode = model.training<EOL>if old_mode != mode:<EOL><INDENT>model.train(mode)<EOL><DEDENT>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>if old_mode != mode:<EOL><INDENT>model.train(old_mode)<EOL><DEDENT><DEDENT>

A context manager to temporarily set the training mode of 'model'
to 'mode', resetting it when we exit the with-block.  A no-op if
mode is None.


def pytorch_to_keras(<EOL>model, args, input_shapes,<EOL>change_ordering=False, training=False, verbose=False, names=False,<EOL>):

<EOL>if isinstance(args, torch.autograd.Variable):<EOL><INDENT>args = (args, )<EOL><DEDENT>if isinstance(input_shapes, tuple):<EOL><INDENT>input_shapes = [input_shapes]<EOL><DEDENT>orig_state_dict_keys = _unique_state_dict(model).keys()<EOL>with set_training(model, training):<EOL><INDENT>trace, torch_out = torch.jit.get_trace_graph(model, tuple(args))<EOL><DEDENT>if orig_state_dict_keys != _unique_state_dict(model).keys():<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if version.parse('<STR_LIT>') < version.parse(torch.__version__):<EOL><INDENT>trace.set_graph(_optimize_graph(trace.graph(), OperatorExportTypes.ONNX))<EOL><DEDENT>else:<EOL><INDENT>trace.set_graph(_optimize_graph(trace.graph(), False))<EOL><DEDENT>trace.graph().lint()<EOL>if verbose:<EOL><INDENT>print(trace.graph())<EOL><DEDENT>nodes = list(trace.graph().nodes())<EOL>if six.PY3:<EOL><INDENT>from types import SimpleNamespace<EOL>seq_to_find =['<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>k = <NUM_LIT:0><EOL>s = <NUM_LIT:0><EOL>for i, node in enumerate(nodes):<EOL><INDENT>if node.kind() == seq_to_find[k]:<EOL><INDENT>if k == <NUM_LIT:0>:<EOL><INDENT>s = i<EOL><DEDENT>k += <NUM_LIT:1><EOL>if k == len(seq_to_find):<EOL><INDENT>reshape_op = nodes[s + k - <NUM_LIT:1>]<EOL>flatten_op = {<EOL>'<STR_LIT>': (lambda: '<STR_LIT>'),<EOL>'<STR_LIT>': (lambda: {}),<EOL>'<STR_LIT>':  (lambda: list(reshape_op.outputs())),<EOL>'<STR_LIT>': (lambda: reshape_op.scopeName()),<EOL>'<STR_LIT>': (lambda: list(reshape_op.inputs())[:<NUM_LIT:1>]),<EOL>'<STR_LIT>': (lambda: reshape_op.__str__()),<EOL>}<EOL>nodes = nodes[:s] + [SimpleNamespace(**flatten_op)] + nodes[s+k:]<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>k = <NUM_LIT:0><EOL>s = -<NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>graph_outputs = [get_leaf_id(n) for n in trace.graph().outputs()]<EOL>graph_inputs = [get_leaf_id(n) for n in trace.graph().inputs()]<EOL>state_dict = _unique_state_dict(model)<EOL>if verbose:<EOL><INDENT>print('<STR_LIT>', graph_inputs)<EOL>print('<STR_LIT>', graph_outputs)<EOL>print('<STR_LIT>', list(state_dict))<EOL><DEDENT>import re<EOL>import keras<EOL>from keras import backend as K<EOL>K.set_image_data_format('<STR_LIT>')<EOL>layers = dict()<EOL>keras_inputs = []<EOL>for i in range(len(args)):<EOL><INDENT>layers[graph_inputs[i]] = keras.layers.InputLayer(<EOL>input_shape=input_shapes[i], name='<STR_LIT>'.format(i)<EOL>).output<EOL>keras_inputs.append(layers[graph_inputs[i]])<EOL><DEDENT>outputs = []<EOL>group_indices = defaultdict(lambda: <NUM_LIT:0>, {})<EOL>for node in nodes:<EOL><INDENT>node_inputs = list(node.inputs())<EOL>node_input_names = []<EOL>for node_input in node_inputs:<EOL><INDENT>node_input_names.append(get_leaf_id(node_input))<EOL><DEDENT>node_type = node.kind()<EOL>node_scope_name = node.scopeName()<EOL>node_id = get_node_id(node)<EOL>node_name_regex = re.findall(r'<STR_LIT>', node_scope_name)<EOL>try: <EOL><INDENT>int(node_name_regex[-<NUM_LIT:1>])<EOL>node_weigth_group_name = '<STR_LIT:.>'.join(<EOL>node_name_regex[:-<NUM_LIT:1>]<EOL>)<EOL>node_weights_name = node_weigth_group_name + '<STR_LIT:.>' + str(group_indices[node_weigth_group_name])<EOL>group_indices[node_weigth_group_name] += <NUM_LIT:1><EOL><DEDENT>except ValueError:<EOL><INDENT>node_weights_name = '<STR_LIT:.>'.join(<EOL>node_name_regex<EOL>)<EOL><DEDENT>except IndexError:<EOL><INDENT>node_weights_name = '<STR_LIT:.>'.join(node_input_names)<EOL><DEDENT>node_attrs = {k: node[k] for k in node.attributeNames()}<EOL>node_outputs = list(node.outputs())<EOL>node_outputs_names = []<EOL>for node_output in node_outputs:<EOL><INDENT>if node_output.node().scopeName():<EOL><INDENT>node_outputs_names.append(node_output.node().scopeName())<EOL><DEDENT><DEDENT>if verbose:<EOL><INDENT>print('<STR_LIT>')<EOL>print('<STR_LIT>', node_scope_name)<EOL>print('<STR_LIT>', node_id)<EOL>print('<STR_LIT>', node_type)<EOL>print('<STR_LIT>', node_input_names)<EOL>print('<STR_LIT>', node_outputs_names)<EOL>print('<STR_LIT>', node_weights_name)<EOL>print('<STR_LIT>', node_attrs)<EOL>print('<STR_LIT>', node_id in graph_outputs)<EOL><DEDENT>AVAILABLE_CONVERTERS[node_type](<EOL>node_attrs,<EOL>node_weights_name, node_id,<EOL>node_input_names,<EOL>layers, state_dict,<EOL>names<EOL>)<EOL>if node_id in graph_outputs:<EOL><INDENT>outputs.append(layers[node_id])<EOL><DEDENT><DEDENT>model = keras.models.Model(inputs=keras_inputs, outputs=outputs)<EOL>if change_ordering:<EOL><INDENT>import numpy as np<EOL>conf = model.get_config()<EOL>for layer in conf['<STR_LIT>']:<EOL><INDENT>if layer['<STR_LIT>'] and '<STR_LIT>' in layer['<STR_LIT>']:<EOL><INDENT>layer['<STR_LIT>']['<STR_LIT>'] =tuple(np.reshape(np.array(<EOL>[<EOL>[None] +<EOL>list(layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:2>:][:]) +<EOL>[layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:1>]]<EOL>]), -<NUM_LIT:1><EOL>))<EOL><DEDENT>if layer['<STR_LIT>'] and '<STR_LIT>' in layer['<STR_LIT>']:<EOL><INDENT>if len(list(layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:1>:][:])) > <NUM_LIT:0>:<EOL><INDENT>layer['<STR_LIT>']['<STR_LIT>'] =tuple(np.reshape(np.array(<EOL>[<EOL>list(layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:1>:][:]),<EOL>layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>]), -<NUM_LIT:1><EOL>),)<EOL><DEDENT><DEDENT>if layer['<STR_LIT>'] and '<STR_LIT>' in layer['<STR_LIT>']:<EOL><INDENT>layer['<STR_LIT>']['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if layer['<STR_LIT>'] and '<STR_LIT>' in layer['<STR_LIT>']:<EOL><INDENT>layer['<STR_LIT>']['<STR_LIT>'] = <NUM_LIT:3><EOL><DEDENT><DEDENT>K.set_image_data_format('<STR_LIT>')<EOL>model_tf_ordering = keras.models.Model.from_config(conf)<EOL>for dst_layer, src_layer in zip(<EOL>model_tf_ordering.layers, model.layers<EOL>):<EOL><INDENT>dst_layer.set_weights(src_layer.get_weights())<EOL><DEDENT>model = model_tf_ordering<EOL><DEDENT>print('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>return model<EOL>

By given pytorch model convert layers with specified convertors.

Args:
    model: pytorch model
    args: pytorch model arguments
    input_shapes: keras input shapes (using for each InputLayer)
    change_ordering: change CHW to HWC
    training: switch model to training mode
    verbose: verbose output
    names: use short names, use random-suffix or keep original names for keras layers

Returns:
    model: created keras model.


def convert_sum(<EOL>params, w_name, scope_name, inputs, layers, weights, names<EOL>):

print('<STR_LIT>')<EOL>def target_layer(x):<EOL><INDENT>import keras.backend as K<EOL>return K.sum(x)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert sum.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_reduce_sum(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>keepdims = params['<STR_LIT>'] > <NUM_LIT:0><EOL>axis = params['<STR_LIT>']<EOL>def target_layer(x, keepdims=keepdims, axis=axis):<EOL><INDENT>import keras.backend as K<EOL>return K.sum(x, keepdims=keepdims, axis=axis)<EOL><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert reduce_sum layer.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_concat(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>concat_nodes = [layers[i] for i in inputs]<EOL>if len(concat_nodes) == <NUM_LIT:1>:<EOL><INDENT>layers[scope_name] = concat_nodes[<NUM_LIT:0>]<EOL>return<EOL><DEDENT>if names == '<STR_LIT>':<EOL><INDENT>tf_name = '<STR_LIT>' + random_string(<NUM_LIT:5>)<EOL><DEDENT>elif names == '<STR_LIT>':<EOL><INDENT>tf_name = w_name<EOL><DEDENT>else:<EOL><INDENT>tf_name = w_name + str(random.random())<EOL><DEDENT>cat = keras.layers.Concatenate(name=tf_name, axis=params['<STR_LIT>'])<EOL>layers[scope_name] = cat(concat_nodes)<EOL>

Convert concatenation.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_slice(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if len(params['<STR_LIT>']) > <NUM_LIT:1>:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>if params['<STR_LIT>'][<NUM_LIT:0>] not in [<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>]:<EOL><INDENT>raise AssertionError('<STR_LIT>')<EOL><DEDENT>def target_layer(x, axis=int(params['<STR_LIT>'][<NUM_LIT:0>]), start=int(params['<STR_LIT>'][<NUM_LIT:0>]), end=int(params['<STR_LIT>'][<NUM_LIT:0>])):<EOL><INDENT>if axis == <NUM_LIT:0>:<EOL><INDENT>return x[start:end]<EOL><DEDENT>elif axis == <NUM_LIT:1>:<EOL><INDENT>return x[:, start:end]<EOL><DEDENT>elif axis == <NUM_LIT:2>:<EOL><INDENT>return x[:, :, start:end]<EOL><DEDENT>elif axis == <NUM_LIT:3>:<EOL><INDENT>return x[:, :, :, start:end]<EOL><DEDENT><DEDENT>lambda_layer = keras.layers.Lambda(target_layer)<EOL>layers[scope_name] = lambda_layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert slice operation.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def convert_clip(params, w_name, scope_name, inputs, layers, weights, names):

print('<STR_LIT>')<EOL>if params['<STR_LIT>'] == <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>".format(params['<STR_LIT>']))<EOL>layer = keras.layers.ReLU(max_value=params['<STR_LIT>'])<EOL><DEDENT>else:<EOL><INDENT>def target_layer(x, vmin=params['<STR_LIT>'], vmax=params['<STR_LIT>']):<EOL><INDENT>import tensorflow as tf<EOL>return tf.clip_by_value(x, vmin, vmax)<EOL><DEDENT>layer = keras.layers.Lambda(target_layer)<EOL><DEDENT>layers[scope_name] = layer(layers[inputs[<NUM_LIT:0>]])<EOL>

Convert clip operation.

Args:
    params: dictionary with layer parameters
    w_name: name prefix in state_dict
    scope_name: pytorch scope name
    inputs: pytorch node inputs
    layers: dictionary with keras tensors
    weights: pytorch state_dict
    names: use short names for keras layers


def assert_json_type(value: JsonValue, expected_type: JsonCheckType) -> None:

def type_name(t: Union[JsonCheckType, Type[None]]) -> str:<EOL><INDENT>if t is None:<EOL><INDENT>return "<STR_LIT:None>"<EOL><DEDENT>if isinstance(t, JList):<EOL><INDENT>return "<STR_LIT:list>"<EOL><DEDENT>return t.__name__<EOL><DEDENT>if expected_type is None:<EOL><INDENT>if value is None:<EOL><INDENT>return<EOL><DEDENT><DEDENT>elif expected_type == float:<EOL><INDENT>if isinstance(value, float) or isinstance(value, int):<EOL><INDENT>return<EOL><DEDENT><DEDENT>elif expected_type in [str, int, bool, list, dict]:<EOL><INDENT>if isinstance(value, expected_type):  <EOL><INDENT>return<EOL><DEDENT><DEDENT>elif isinstance(expected_type, JList):<EOL><INDENT>if isinstance(value, list):<EOL><INDENT>for v in value:<EOL><INDENT>assert_json_type(v, expected_type.value_type)<EOL><DEDENT>return<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>raise TypeError("<STR_LIT>".format(<EOL>type_name(expected_type), type_name(type(value))))<EOL>

Check that a value has a certain JSON type.

    Raise TypeError if the type does not match.

    Supported types: str, int, float, bool, list, dict, and None.
    float will match any number, int will only match numbers without
    fractional part.

    The special type JList(x) will match a list value where each
    item is of type x:

    >>> assert_json_type([1, 2, 3], JList(int))


def json_get(json: JsonValue, path: str, expected_type: Any = ANY) -> Any:

elements = _parse_json_path(path)<EOL>current = json<EOL>current_path = "<STR_LIT>"<EOL>for i, element in enumerate(elements):<EOL><INDENT>if isinstance(element, str):<EOL><INDENT>if not isinstance(current, dict):<EOL><INDENT>msg = "<STR_LIT>".format(current_path)<EOL>raise TypeError(msg) from None<EOL><DEDENT>if element not in current:<EOL><INDENT>raise ValueError("<STR_LIT>".format(path))<EOL><DEDENT>current_path += "<STR_LIT:/>" + element<EOL>current = current[element]<EOL><DEDENT>else:<EOL><INDENT>if not isinstance(current, list):<EOL><INDENT>msg = "<STR_LIT>".format(current_path)<EOL>raise TypeError(msg) from None<EOL><DEDENT>if element >= len(current):<EOL><INDENT>msg = "<STR_LIT>".format(<EOL>current_path, len(current), element)<EOL>raise IndexError(msg)<EOL><DEDENT>current_path += "<STR_LIT>".format(i)<EOL>current = current[element]<EOL><DEDENT><DEDENT>if expected_type != ANY:<EOL><INDENT>assert_json_type(current, cast(JsonType, expected_type))<EOL><DEDENT>return current<EOL>

Get a JSON value by path, optionally checking its type.

    >>> j = {"foo": {"num": 3.4, "s": "Text"}, "arr": [10, 20, 30]}
    >>> json_get(j, "/foo/num")
    3.4
    >>> json_get(j, "/arr[1]")
    20

    Raise ValueError if the path is not found:

    >>> json_get(j, "/foo/unknown")
    Traceback (most recent call last):
        ...
    ValueError: JSON path '/foo/unknown' not found

    Raise TypeError if the path contains a non-object element:

    >>> json_get(j, "/foo/num/bar")
    Traceback (most recent call last):
        ...
    TypeError: JSON path '/foo/num' is not an object

    Or a non-array element:

    >>> json_get(j, "/foo[2]")
    Traceback (most recent call last):
        ...
    TypeError: JSON path '/foo' is not an array

    Raise an IndexError if the array index is out of bounds:

    >>> json_get(j, "/arr[10]")
    Traceback (most recent call last):
        ...
    IndexError: JSON array '/arr' too small (3 <= 10)

    Recognized types are: str, int, float, bool, list, dict, and None.
    TypeError is raised if the type does not match.

    >>> json_get(j, "/foo/num", str)
    Traceback (most recent call last):
        ...
    TypeError: wrong JSON type str != float

    float will match any number, int will only match numbers without a
    fractional part.

    >>> json_get(j, "/foo/num", float)
    3.4
    >>> json_get(j, "/foo/num", int)
    Traceback (most recent call last):
        ...
    TypeError: wrong JSON type int != float


def json_get_default(json: JsonValue, path: str,<EOL>default: Any, expected_type: Any = ANY) -> Any:

try:<EOL><INDENT>return json_get(json, path, expected_type)<EOL><DEDENT>except (ValueError, IndexError):<EOL><INDENT>return default<EOL><DEDENT>

Get a JSON value by path, optionally checking its type.

    This works exactly like json_get(), but instead of raising
    ValueError or IndexError when a path part is not found, return
    the provided default value:

    >>> json_get_default({}, "/foo", "I am a default value")
    'I am a default value'

    TypeErrors will be raised as in json_get() if an expected_type
    is provided:

    >>> json_get_default({"foo": "bar"}, "/foo", 123, int)
    Traceback (most recent call last):
        ...
    TypeError: wrong JSON type int != str


return self.__timeout<EOL>


return self.__deadline<EOL>


self.__validate_ping_param()<EOL>ping_proc = subprocrunner.SubprocessRunner(self.__get_ping_command())<EOL>ping_proc.run()<EOL>return PingResult(ping_proc.stdout, ping_proc.stderr, ping_proc.returncode)<EOL>

Sending ICMP packets.

:return: ``ping`` command execution result.
:rtype: :py:class:`.PingResult`
:raises ValueError: If parameters not valid.


return self.__destination<EOL>

The ping destination.

Returns:
    |str|:


return self.__packet_transmit<EOL>

Number of packets transmitted.

Returns:
    |int|:


return self.__packet_receive<EOL>

Number of packets received.

Returns:
    |int|:


try:<EOL><INDENT>return self.packet_transmit - self.packet_receive<EOL><DEDENT>except TypeError:<EOL><INDENT>return None<EOL><DEDENT>

Number of packet losses.

Returns:
    |int|: |None| if the value is not a number.


try:<EOL><INDENT>return (self.packet_loss_count / self.packet_transmit) * <NUM_LIT:100><EOL><DEDENT>except (TypeError, ZeroDivisionError, OverflowError):<EOL><INDENT>return None<EOL><DEDENT>

Percentage of packet loss |percent_unit|.

Returns:
    |float|: |None| if the value is not a number.


return self.__rtt_min<EOL>

Minimum round trip time of transmitted ICMP packets |msec_unit|.

Returns:
    |float|:


return self.__rtt_avg<EOL>

Average round trip time of transmitted ICMP packets |msec_unit|.

Returns:
    |float|:


return self.__rtt_max<EOL>

Maximum round trip time of transmitted ICMP packets |msec_unit|.

Returns:
    |float|:


return self.__rtt_mdev<EOL>

Standard deviation of transmitted ICMP packets.

Returns:
    |float|: |None| when parsing Windows ping result.


return self.__duplicates<EOL>

Number of duplicated packets.

Returns:
    |int|: |None| when parsing Windows ping result.


try:<EOL><INDENT>return (self.packet_duplicate_count / self.packet_receive) * <NUM_LIT:100><EOL><DEDENT>except (TypeError, ZeroDivisionError, OverflowError):<EOL><INDENT>return None<EOL><DEDENT>

Percentage of duplicated packets |percent_unit|.

Returns:
    |float|: |None| if the value is not a number.


return self.__icmp_replies<EOL>

ICMP packet reply information.

Returns:
    |list| of |dict|:


return {<EOL>"<STR_LIT>": self.destination,<EOL>"<STR_LIT>": self.packet_transmit,<EOL>"<STR_LIT>": self.packet_receive,<EOL>"<STR_LIT>": self.packet_loss_count,<EOL>"<STR_LIT>": self.packet_loss_rate,<EOL>"<STR_LIT>": self.rtt_min,<EOL>"<STR_LIT>": self.rtt_avg,<EOL>"<STR_LIT>": self.rtt_max,<EOL>"<STR_LIT>": self.rtt_mdev,<EOL>"<STR_LIT>": self.packet_duplicate_count,<EOL>"<STR_LIT>": self.packet_duplicate_rate,<EOL>}<EOL>

ping statistics.

Returns:
    |dict|:

Examples:
    >>> import pingparsing
    >>> parser = pingparsing.PingParsing()
    >>> parser.parse(ping_result)
    >>> parser.as_dict()
    {
        "destination": "google.com",
        "packet_transmit": 60,
        "packet_receive": 60,
        "packet_loss_rate": 0.0,
        "packet_loss_count": 0,
        "rtt_min": 61.425,
        "rtt_avg": 99.731,
        "rtt_max": 212.597,
        "rtt_mdev": 27.566,
        "packet_duplicate_rate": 0.0,
        "packet_duplicate_count": 0
    }


from collections import namedtuple<EOL>ping_result = self.as_dict()<EOL>return namedtuple("<STR_LIT>", ping_result.keys())(**ping_result)<EOL>

ping statistics.

Returns:
    |namedtuple|:

Examples:
    >>> import pingparsing
    >>> parser = pingparsing.PingParsing()
    >>> parser.parse(ping_result)
    >>> parser.as_tuple()
    PingResult(destination='google.com', packet_transmit=60, packet_receive=60, packet_loss_rate=0.0, packet_loss_count=0, rtt_min=61.425, rtt_avg=99.731, rtt_max=212.597, rtt_mdev=27.566, packet_duplicate_rate=0.0, packet_duplicate_count=0)


<EOL>logbook.get_level_name(log_level)<EOL>if log_level == logger.level:<EOL><INDENT>return<EOL><DEDENT>if log_level == logbook.NOTSET:<EOL><INDENT>set_logger(is_enable=False)<EOL><DEDENT>else:<EOL><INDENT>set_logger(is_enable=True)<EOL><DEDENT>logger.level = log_level<EOL>subprocrunner.set_log_level(log_level)<EOL>

Set logging level of this module. The module using
`logbook <https://logbook.readthedocs.io/en/stable/>`__ module for logging.

:param int log_level:
    One of the log level of the
    `logbook <https://logbook.readthedocs.io/en/stable/api/base.html>`__.
    Disabled logging if the ``log_level`` is ``logbook.NOTSET``.
:raises LookupError: If ``log_level`` is an invalid value.


try:<EOL><INDENT>if typepy.is_not_null_string(ping_message.stdout):<EOL><INDENT>ping_message = ping_message.stdout<EOL><DEDENT><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT>logger.debug("<STR_LIT>".format(ping_message))<EOL>self.__parser = NullPingParser()<EOL>if typepy.is_null_string(ping_message):<EOL><INDENT>logger.debug("<STR_LIT>")<EOL>self.__stats = PingStats()<EOL>return self.__stats<EOL><DEDENT>ping_lines = _to_unicode(ping_message).splitlines()<EOL>parser_class_list = (<EOL>LinuxPingParser,<EOL>WindowsPingParser,<EOL>MacOsPingParser,<EOL>AlpineLinuxPingParser,<EOL>)<EOL>for parser_class in parser_class_list:<EOL><INDENT>self.__parser = parser_class()<EOL>try:<EOL><INDENT>self.__stats = self.__parser.parse(ping_lines)<EOL>return self.__stats<EOL><DEDENT>except ParseError as e:<EOL><INDENT>if e.reason != ParseErrorReason.HEADER_NOT_FOUND:<EOL><INDENT>raise e<EOL><DEDENT><DEDENT>except pp.ParseException:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>self.__parser = NullPingParser()<EOL>return self.__stats<EOL>

Parse ping command output.

Args:
    ping_message (str or :py:class:`~pingparsing.PingResult`):
        ``ping`` command output.

Returns:
    :py:class:`~pingparsing.PingStats`: Parsed result.


zipcode = zipcode.split("<STR_LIT:->")[<NUM_LIT:0>]  <EOL>if len(zipcode) != valid_length:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>if _contains_nondigits(zipcode):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return zipcode<EOL>


return [z for z in zips if z["<STR_LIT>"] == zipcode]<EOL>

Retrieve zipcode dict for provided zipcode


return bool(matching(zipcode))<EOL>

Determine whether a given zip or zip+4 zipcode is real.


return [z for z in zips if z["<STR_LIT>"].startswith(partial_zipcode)]<EOL>

List of zipcode dicts where zipcode prefix matches `partial_zipcode`


return [z for z in zips if all([k in z and z[k] == v for k, v in kwargs.items()])]<EOL>

Use `kwargs` to select for desired attributes from list of zipcode dicts


return zips<EOL>

Return a list containing all zip-code objects.


<EOL>TEMPLATES = copy.deepcopy(settings.TEMPLATES)<EOL>for tpl_cfg in TEMPLATES:<EOL><INDENT>tpl_cfg['<STR_LIT>'] = [template_path(rel_dir) for rel_dir in relative_dirs]<EOL><DEDENT>return override_settings(TEMPLATES=TEMPLATES)<EOL>

Convenient decorator to specify the template path.


try:<EOL><INDENT>blocks = context.render_context[BLOCK_CONTEXT_KEY]<EOL><DEDENT>except KeyError:<EOL><INDENT>blocks = context.render_context[BLOCK_CONTEXT_KEY] = BlockContext()<EOL><DEDENT>if isinstance(template, six.string_types):<EOL><INDENT>template = get_template(template)<EOL><DEDENT>template = getattr(template, '<STR_LIT>', template)<EOL>local_blocks = {<EOL>block.name: block<EOL>for block in template.nodelist.get_nodes_by_type(BlockNode)<EOL>}<EOL>blocks.add_blocks(local_blocks)<EOL>extends = template.nodelist.get_nodes_by_type(ExtendsNode)<EOL>if extends:<EOL><INDENT>extends_node = extends[<NUM_LIT:0>]<EOL>parent_template = extends_node.get_parent(context)<EOL>resolve_blocks(parent_template, context)<EOL><DEDENT>return blocks<EOL>

Return a BlockContext instance of all the {% block %} tags in the template.

If template is a string, it will be resolved through get_template


try:<EOL><INDENT>alias, block_name = widget.split('<STR_LIT::>', <NUM_LIT:1>)<EOL><DEDENT>except ValueError:<EOL><INDENT>raise template.TemplateSyntaxError('<STR_LIT>' % widget)<EOL><DEDENT>return alias, block_name<EOL>

Parse a alias:block_name string into separate parts.


<EOL>if alias == '<STR_LIT>':<EOL><INDENT>yield context<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>widgets = context.render_context[WIDGET_CONTEXT_KEY]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise template.TemplateSyntaxError('<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>block_set = widgets[alias]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise template.TemplateSyntaxError('<STR_LIT>' % alias)<EOL><DEDENT>context.render_context.push()<EOL>context.render_context[BLOCK_CONTEXT_KEY] = block_set<EOL>context.render_context[WIDGET_CONTEXT_KEY] = widgets<EOL>yield context<EOL>context.render_context.pop()<EOL><DEDENT>

Temporarily update the context to use the BlockContext for the given alias.


block_set = context.render_context[BLOCK_CONTEXT_KEY]<EOL>for name in names:<EOL><INDENT>block = block_set.get_block(name)<EOL>if block is not None:<EOL><INDENT>return block<EOL><DEDENT><DEDENT>raise template.TemplateSyntaxError('<STR_LIT>' % (names,))<EOL>

Find the first matching block in the current block_context


_soft = kwargs.pop('<STR_LIT>', False)<EOL>try:<EOL><INDENT>widgets = context.render_context[WIDGET_CONTEXT_KEY]<EOL><DEDENT>except KeyError:<EOL><INDENT>widgets = context.render_context[WIDGET_CONTEXT_KEY] = {}<EOL><DEDENT>for alias, template_name in kwargs.items():<EOL><INDENT>if _soft and alias in widgets:<EOL><INDENT>continue<EOL><DEDENT>with context.render_context.push({BLOCK_CONTEXT_KEY: BlockContext()}):<EOL><INDENT>blocks = resolve_blocks(template_name, context)<EOL>widgets[alias] = blocks<EOL><DEDENT><DEDENT>return '<STR_LIT>'<EOL>


<EOL>info = {<EOL>'<STR_LIT>': field.field.widget.__class__.__name__,<EOL>'<STR_LIT>': field.field.__class__.__name__,<EOL>'<STR_LIT:name>': field.name,<EOL>}<EOL>return [<EOL>fmt.format(**info)<EOL>for fmt in (<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>)<EOL>]<EOL>

Return a list of widget names for the provided field.


@register.simple_tag(takes_context=True)<EOL>def reuse(context, block_list, **kwargs):

try:<EOL><INDENT>block_context = context.render_context[BLOCK_CONTEXT_KEY]<EOL><DEDENT>except KeyError:<EOL><INDENT>block_context = BlockContext()<EOL><DEDENT>if not isinstance(block_list, (list, tuple)):<EOL><INDENT>block_list = [block_list]<EOL><DEDENT>for block in block_list:<EOL><INDENT>block = block_context.get_block(block)<EOL>if block:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>with context.push(kwargs):<EOL><INDENT>return block.render(context)<EOL><DEDENT>

Allow reuse of a block within a template.

{% reuse '_myblock' foo=bar %}

If passed a list of block names, will use the first that matches:

{% reuse list_of_block_names .... %}


if not self.is_group():<EOL><INDENT>return self._display<EOL><DEDENT>return ((force_text(k), v) for k, v in self._display)<EOL>

When dealing with optgroups, ensure that the value is properly force_text'd.


return key in self.keys() or hasattr(self, key)<EOL>


return dict(self.choices).get(self.value, '<STR_LIT>')<EOL>


raw_value = super(NullBooleanFieldExtractor, self).raw_value<EOL>if raw_value is None:<EOL><INDENT>return '<STR_LIT:1>'<EOL><DEDENT>return raw_value<EOL>

When the value is None, it's actually rendered as '1', see
``django.forms.widgets.NullBooleanSelect.render``


try:<EOL><INDENT>return {True: '<STR_LIT:2>', False: '<STR_LIT:3>', '<STR_LIT:2>': '<STR_LIT:2>', '<STR_LIT:3>': '<STR_LIT:3>'}[self.raw_value]<EOL><DEDENT>except KeyError:<EOL><INDENT>return '<STR_LIT:1>'<EOL><DEDENT>

Maps True/False and 2/3 to the correct stringified version.

See ``django.forms.widgets.NullBooleanSelect.render``.


return self.content<EOL>


return self.path<EOL>


return self.stats<EOL>


return self.get_stats()['<STR_LIT>']<EOL>


return self.get_stats()['<STR_LIT>'].keys()<EOL>


return self.get_stats()['<STR_LIT>'][array]['<STR_LIT:type>']<EOL>


return self.get_stats()['<STR_LIT>'][array]['<STR_LIT:status>']<EOL>


return self.get_stats()['<STR_LIT>'][array]['<STR_LIT>'].keys()<EOL>

Return the components of the arrays (list).


return int(self.get_stats()['<STR_LIT>'][array]['<STR_LIT>'])<EOL>

Return the array's available components number.


Return the array's used components number.


return self.get_stats()['<STR_LIT>'][array]['<STR_LIT>']<EOL>

Return the array's config/status.

        U mean OK
        _ mean Failed


ret = {}<EOL>with open(self.get_path(), '<STR_LIT:r>') as f:<EOL><INDENT>lines = f.readlines()<EOL><DEDENT>ret['<STR_LIT>'] = self.get_personalities(lines[<NUM_LIT:0>])<EOL>ret['<STR_LIT>'] = self.get_arrays(lines[<NUM_LIT:1>:-<NUM_LIT:1>], ret['<STR_LIT>'])<EOL>self.content = reduce(lambda x, y: x + y, lines)<EOL>return ret<EOL>


return [split('<STR_LIT>', i)[<NUM_LIT:1>] for i in line.split('<STR_LIT::>')[<NUM_LIT:1>].split('<STR_LIT:U+0020>') if i.startswith('<STR_LIT:[>')]<EOL>

Return a list of personalities readed from the input line.


def get_arrays(self, lines, personalities=[]):

ret = {}<EOL>i = <NUM_LIT:0><EOL>while i < len(lines):<EOL><INDENT>try:<EOL><INDENT>md_device = self.get_md_device_name(lines[i])<EOL><DEDENT>except IndexError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if md_device is not None:<EOL><INDENT>ret[md_device] = self.get_md_device(lines[i], personalities)<EOL>i += <NUM_LIT:1><EOL>ret[md_device].update(self.get_md_status(lines[i]))<EOL><DEDENT><DEDENT>i += <NUM_LIT:1><EOL><DEDENT>return ret<EOL>


def get_md_device(self, line, personalities=[]):

ret = {}<EOL>splitted = split('<STR_LIT>', line)<EOL>ret['<STR_LIT:status>'] = splitted[<NUM_LIT:1>]<EOL>if splitted[<NUM_LIT:2>] in personalities:<EOL><INDENT>ret['<STR_LIT:type>'] = splitted[<NUM_LIT:2>]<EOL>ret['<STR_LIT>'] = self.get_components(line, with_type=True)<EOL><DEDENT>else:<EOL><INDENT>ret['<STR_LIT:type>'] = None<EOL>ret['<STR_LIT>'] = self.get_components(line, with_type=False)<EOL><DEDENT>return ret<EOL>

Return a dict of md device define in the line.


ret = {}<EOL>splitted = split('<STR_LIT>', line)<EOL>if len(splitted) < <NUM_LIT:7>:<EOL><INDENT>ret['<STR_LIT>'] = None<EOL>ret['<STR_LIT>'] = None<EOL>ret['<STR_LIT>'] = None<EOL><DEDENT>else:<EOL><INDENT>ret['<STR_LIT>'] = splitted[-<NUM_LIT:4>]<EOL>ret['<STR_LIT>'] = splitted[-<NUM_LIT:3>]<EOL>ret['<STR_LIT>'] = splitted[-<NUM_LIT:2>]<EOL><DEDENT>return ret<EOL>

Return a dict of md status define in the line.


def get_components(self, line, with_type=True):

ret = {}<EOL>line2 = reduce(lambda x, y: x + y, split('<STR_LIT>', line))<EOL>if with_type:<EOL><INDENT>splitted = split('<STR_LIT>', line2)[<NUM_LIT:3>:]<EOL><DEDENT>else:<EOL><INDENT>splitted = split('<STR_LIT>', line2)[<NUM_LIT:2>:]<EOL><DEDENT>ret = dict(zip(splitted[<NUM_LIT:0>::<NUM_LIT:2>], splitted[<NUM_LIT:1>::<NUM_LIT:2>]))<EOL>return ret<EOL>

Return a dict of components in the line.

        key: device name (ex: 'sdc1')
        value: device role number


ret = split('<STR_LIT>', line)[<NUM_LIT:0>]<EOL>if ret.startswith('<STR_LIT>'):<EOL><INDENT>return ret<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Return the md device name from the input line.


def render_to_response(self, context=None, **response_kwargs):

response_kwargs['<STR_LIT>'] = '<STR_LIT:application/json>'<EOL>return self.response_class(self.convert_context_to_json(context or {}), **response_kwargs)<EOL>

Return a JSON response, transforming 'context' to make the payload.


return json.dumps(context, cls=DjangoJSONEncoder)<EOL>

Convert the context dictionary into a JSON object


folder = settings.UPLOADS_ROOT<EOL>ext = filename.split('<STR_LIT:.>')[-<NUM_LIT:1>]<EOL>filename = '<STR_LIT>'.format(str(uuid4()), ext)<EOL>return os.path.join(folder, filename)<EOL>

Generates random filename for uploading file using uuid4 hashes
You need to define UPLOADS_ROOT in your django settings
something like this
UPLOADS_ROOT = rel(MEDIA_ROOT, 'uploads')


filename, ext = os.path.splitext(filename.lower())<EOL>instance_id_hash = hashlib.md5(str(instance.id)).hexdigest()<EOL>filename_hash = '<STR_LIT>'.join(random.sample(hashlib.md5(filename.encode('<STR_LIT:utf-8>')).hexdigest(), <NUM_LIT:8>))<EOL>return '<STR_LIT>'.format(instance_id_hash, filename_hash, ext)<EOL>

Generates likely unique image path using md5 hashes


def get_cache_key(prefix, *args, **kwargs):

hash_args_kwargs = hash(tuple(kwargs.iteritems()) + args)<EOL>return '<STR_LIT>'.format(prefix, hash_args_kwargs)<EOL>

Calculates cache key based on `args` and `kwargs`.
`args` and `kwargs` must be instances of hashable types.


def decorator(func):<EOL><INDENT>@wraps(func)<EOL>def wrapper(self, *args, **kwargs):<EOL><INDENT>cache_key_prefix = prefix or '<STR_LIT>'.format(func.__name__)<EOL>cache_key = get_cache_key(cache_key_prefix, *args, **kwargs)<EOL>if not hasattr(self, cache_key):<EOL><INDENT>setattr(self, cache_key, func(self))<EOL><DEDENT>return getattr(self, cache_key)<EOL><DEDENT>return wrapper<EOL><DEDENT>if func is None:<EOL><INDENT>return decorator<EOL><DEDENT>else:<EOL><INDENT>return decorator(func)<EOL><DEDENT>

Cache result of function execution into the `self` object (mostly useful in models).
Calculate cache key based on `args` and `kwargs` of the function (except `self`).


def decorator(func):<EOL><INDENT>@wraps(func)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>cache_args = args<EOL>if method:<EOL><INDENT>cache_args = args[<NUM_LIT:1>:]<EOL><DEDENT>cache_key = get_cache_key(prefix, *cache_args, **kwargs)<EOL>cached_value = cache.get(cache_key)<EOL>if cached_value is None:<EOL><INDENT>cached_value = func(*args, **kwargs)<EOL>cache.set(cache_key, cached_value)<EOL><DEDENT>return cached_value<EOL><DEDENT>return wrapper<EOL><DEDENT>return decorator<EOL>

Cache result of function execution into the django cache backend.
Calculate cache key based on `prefix`, `args` and `kwargs` of the function.
For using like object method set `method=True`.


def decorator(func):<EOL><INDENT>@wraps(func)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>try:<EOL><INDENT>return func(*args, **kwargs)<EOL><DEDENT>except ObjectDoesNotExist:<EOL><INDENT>if callable(default):<EOL><INDENT>return default()<EOL><DEDENT>else:<EOL><INDENT>return default<EOL><DEDENT><DEDENT><DEDENT>return wrapper<EOL><DEDENT>if func is None:<EOL><INDENT>return decorator<EOL><DEDENT>else:<EOL><INDENT>return decorator(func)<EOL><DEDENT>

Wrapper around Django's ORM `get` functionality.
Wrap anything that raises ObjectDoesNotExist exception
and provide the default value if necessary.
`default` by default is None. `default` can be any callable,
if it is callable it will be called when ObjectDoesNotExist
exception will be raised.


string = re.sub(r"<STR_LIT>", '<STR_LIT>', str(string))<EOL>if not string:<EOL><INDENT>return string<EOL><DEDENT>return lowercase(string[<NUM_LIT:0>]) + re.sub(r"<STR_LIT>", lambda matched: uppercase(matched.group(<NUM_LIT:1>)), string[<NUM_LIT:1>:])<EOL>

Convert string into camel case.

    Args:
        string: String to convert.

    Returns:
        string: Camel case string.


string = str(string)<EOL>if not string:<EOL><INDENT>return string<EOL><DEDENT>return uppercase(string[<NUM_LIT:0>]) + string[<NUM_LIT:1>:]<EOL>

Convert string into capital case.
    First letters will be uppercase.

    Args:
        string: String to convert.

    Returns:
        string: Capital case string.


return uppercase(snakecase(string))<EOL>

Convert string into upper snake case.
    Join punctuation with underscore and convert letters into uppercase.

    Args:
        string: String to convert.

    Returns:
        string: Const cased string.


return str(string).lower()<EOL>

Convert string into lower case.

    Args:
        string: String to convert.

    Returns:
        string: Lowercase case string.


return capitalcase(camelcase(string))<EOL>

Convert string into pascal case.

    Args:
        string: String to convert.

    Returns:
        string: Pascal case string.


string = snakecase(string)<EOL>if not string:<EOL><INDENT>return string<EOL><DEDENT>return re.sub(r"<STR_LIT:_>", "<STR_LIT:/>", string)<EOL>

Convert string into path case.
    Join punctuation with slash.

    Args:
        string: String to convert.

    Returns:
        string: Path cased string.


str1 = re.sub(r"<STR_LIT:_>", r"<STR_LIT:\\>", snakecase(string))<EOL>return str1<EOL>

Convert string into spinal case.
    Join punctuation with backslash.

    Args:
        string: String to convert.

    Returns:
        string: Spinal cased string.


joiner = '<STR_LIT:U+0020>'<EOL>string = re.sub(r"<STR_LIT>", joiner, str(string))<EOL>if not string:<EOL><INDENT>return string<EOL><DEDENT>return capitalcase(trimcase(<EOL>re.sub(r"<STR_LIT>", lambda matched: joiner +<EOL>lowercase(matched.group(<NUM_LIT:0>)), string)<EOL>))<EOL>

Convert string into sentence case.
    First letter capped and each punctuations are joined with space.

    Args:
        string: String to convert.

    Returns:
        string: Sentence cased string.


string = re.sub(r"<STR_LIT>", '<STR_LIT:_>', str(string))<EOL>if not string:<EOL><INDENT>return string<EOL><DEDENT>return lowercase(string[<NUM_LIT:0>]) + re.sub(r"<STR_LIT>", lambda matched: '<STR_LIT:_>' + lowercase(matched.group(<NUM_LIT:0>)), string[<NUM_LIT:1>:])<EOL>

Convert string into snake case.
    Join punctuation with underscore

    Args:
        string: String to convert.

    Returns:
        string: Snake cased string.


return re.sub(r"<STR_LIT:_>", "<STR_LIT:->", snakecase(string))<EOL>

Convert string into spinal case.
    Join punctuation with hyphen.

    Args:
        string: String to convert.

    Returns:
        string: Spinal cased string.


return re.sub(r"<STR_LIT:_>", "<STR_LIT:.>", snakecase(string))<EOL>

Convert string into dot case.
    Join punctuation with dot.

    Args:
        string: String to convert.

    Returns:
        string: Dot cased string.


return '<STR_LIT:U+0020>'.join(<EOL>[capitalcase(word) for word in snakecase(string).split("<STR_LIT:_>")]<EOL>)<EOL>

Convert string into sentence case.
    First letter capped while each punctuations is capitalsed
    and joined with space.

    Args:
        string: String to convert.

    Returns:
        string: Title cased string.


return str(string).strip()<EOL>

Convert string into trimmed string.

    Args:
        string: String to convert.

    Returns:
        string: Trimmed case string


return str(string).upper()<EOL>

Convert string into upper case.

    Args:
        string: String to convert.

    Returns:
        string: Uppercase case string.


return '<STR_LIT>'.join(filter(str.isalnum, str(string)))<EOL>

Cuts all non-alphanumeric symbols,
    i.e. cuts all expect except 0-9, a-z and A-Z.

    Args:
        string: String to convert.

    Returns:
        string: String with cutted non-alphanumeric symbols.


result = ub.AutoOrderedDict()<EOL>algos = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>for n in ub.ProgIter([<NUM_LIT:1>, <NUM_LIT:10>, <NUM_LIT:100>, <NUM_LIT:1000>, <NUM_LIT>, <NUM_LIT>], desc='<STR_LIT:time>'):<EOL><INDENT>for key in algos:<EOL><INDENT>hashtype = _rectify_hasher(key)<EOL>t1 = ub.Timerit(<NUM_LIT:100>, bestof=<NUM_LIT:10>, label=key, verbose=<NUM_LIT:0>)<EOL>for timer in t1:<EOL><INDENT>data = b'<STR_LIT>' * n<EOL>with timer:<EOL><INDENT>hasher = hashtype()<EOL>hasher.update(data)<EOL><DEDENT><DEDENT>result[key][n] = t1.min()<EOL><DEDENT><DEDENT>import pandas as pd<EOL>print(pd.DataFrame(result))<EOL>result = ub.AutoOrderedDict()<EOL>for n in ub.ProgIter([<NUM_LIT:1>, <NUM_LIT:10>, <NUM_LIT:100>, <NUM_LIT:1000>, <NUM_LIT>, <NUM_LIT>], desc='<STR_LIT:time>'):<EOL><INDENT>for key in algos:<EOL><INDENT>hashtype = _rectify_hasher(key)<EOL>t1 = ub.Timerit(<NUM_LIT:100>, bestof=<NUM_LIT:10>, label=key, verbose=<NUM_LIT:0>)<EOL>for timer in t1:<EOL><INDENT>data = b'<STR_LIT>' * n<EOL>hasher = hashtype()<EOL>hasher.update(data)<EOL>with timer:<EOL><INDENT>hasher.hexdigest()<EOL><DEDENT><DEDENT>result[key][n] = t1.min()<EOL><DEDENT><DEDENT>import pandas as pd<EOL>print(pd.DataFrame(result))<EOL>"""<STR_LIT>"""<EOL>

On 64-bit processors sha512 may be faster than sha256

References:
    https://crypto.stackexchange.com/questions/26336/sha512-faster-than-sha256


import ubelt as ub<EOL>ITEM = [<NUM_LIT:0>, <NUM_LIT:1>, '<STR_LIT:a>', '<STR_LIT:b>', ['<STR_LIT>'] * <NUM_LIT:4>]<EOL>HASHERS = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>scales = list(range(<NUM_LIT:5>, <NUM_LIT>))<EOL>results = ub.AutoDict()<EOL>convert = ub.argval('<STR_LIT>', default='<STR_LIT:True>').lower() == '<STR_LIT:True>'<EOL>print('<STR_LIT>'.format(convert))<EOL>ti = ub.Timerit(<NUM_LIT:9>, bestof=<NUM_LIT:3>, verbose=<NUM_LIT:1>, unit='<STR_LIT>')<EOL>for s in ub.ProgIter(scales, desc='<STR_LIT>', verbose=<NUM_LIT:3>):<EOL><INDENT>N = <NUM_LIT:2> ** s<EOL>print('<STR_LIT>'.format(s=s, N=N))<EOL>data = [ITEM] * N<EOL>for hasher in HASHERS:<EOL><INDENT>for timer in ti.reset(hasher):<EOL><INDENT>ub.hash_data(data, hasher=hasher, convert=convert)<EOL><DEDENT>results[hasher].update({N: ti.mean()})<EOL><DEDENT>col = {h: results[h][N] for h in HASHERS}<EOL>sortx = ub.argsort(col)<EOL>ranking = ub.dict_subset(col, sortx)<EOL>print('<STR_LIT>' + ub.repr2(ranking, precision=<NUM_LIT:9>, nl=<NUM_LIT:0>))<EOL>best = next(iter(ranking))<EOL>pairs = [(k, best) for k in ranking]<EOL>ratios = [ranking[k1] / ranking[k2] for k1, k2 in pairs]<EOL>nicekeys = ['<STR_LIT>'.format(k1, k2) for k1, k2 in pairs]<EOL>relratios = ub.odict(zip(nicekeys, ratios))<EOL>print('<STR_LIT>' + ub.repr2(relratios, precision=<NUM_LIT:4>, nl=<NUM_LIT:0>))<EOL><DEDENT>import pandas as pd<EOL>df = pd.DataFrame.from_dict(results)<EOL>df.columns.name = '<STR_LIT>'<EOL>df.index.name = '<STR_LIT:N>'<EOL>ratios = df.copy().drop(columns=df.columns)<EOL>for k1, k2 in [('<STR_LIT>', '<STR_LIT>'), ('<STR_LIT>', '<STR_LIT>'), ('<STR_LIT>', '<STR_LIT>')]:<EOL><INDENT>ratios['<STR_LIT>'.format(k1, k2)] = df[k1] / df[k2]<EOL><DEDENT>print()<EOL>print('<STR_LIT>')<EOL>print(df.to_string(float_format='<STR_LIT>'))<EOL>print()<EOL>print('<STR_LIT>')<EOL>print(ratios.to_string(float_format='<STR_LIT>'))<EOL>print()<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT>'.format(convert))<EOL>print(ratios.mean().sort_values())<EOL>if ub.argflag('<STR_LIT>'):<EOL><INDENT>import netharn.util as kwel<EOL>kwel.autompl()<EOL>xdata = sorted(ub.peek(results.values()).keys())<EOL>ydata = ub.map_vals(lambda d: [d[x] for x in xdata], results)<EOL>kwel.multi_plot(xdata, ydata, xlabel='<STR_LIT:N>', ylabel='<STR_LIT>', title='<STR_LIT>'.format(convert))<EOL>kwel.show_if_requested()<EOL><DEDENT>

CommandLine:
    python ~/code/ubelt/dev/bench_hash.py --convert=True --show
    python ~/code/ubelt/dev/bench_hash.py --convert=False --show


from os.path import dirname, join, exists<EOL>import ast<EOL>_candiates = [<EOL>join(dirname(__file__), package + '<STR_LIT>'),<EOL>join(dirname(__file__), package, '<STR_LIT>'),<EOL>]<EOL>_found = [init_fpath for init_fpath in _candiates if exists(init_fpath)]<EOL>if len(_found) > <NUM_LIT:0>:<EOL><INDENT>init_fpath = _found[<NUM_LIT:0>]<EOL><DEDENT>elif len(_found) > <NUM_LIT:1>:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>elif len(_found) == <NUM_LIT:0>:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>with open(init_fpath, '<STR_LIT:r>') as file_:<EOL><INDENT>sourcecode = file_.read()<EOL><DEDENT>pt = ast.parse(sourcecode)<EOL>class VersionVisitor(ast.NodeVisitor):<EOL><INDENT>def visit_Assign(self, node):<EOL><INDENT>for target in node.targets:<EOL><INDENT>if getattr(target, '<STR_LIT:id>', None) == '<STR_LIT>':<EOL><INDENT>self.version = node.value.s<EOL><DEDENT><DEDENT><DEDENT><DEDENT>visitor = VersionVisitor()<EOL>visitor.visit(pt)<EOL>return visitor.version<EOL>

Statically parse the version number from __init__.py

CommandLine:
    python -c "import setup; print(setup.parse_version('ubelt'))"


from os.path import dirname, join, exists<EOL>readme_fpath = join(dirname(__file__), '<STR_LIT>')<EOL>if exists(readme_fpath):<EOL><INDENT>with open(readme_fpath, '<STR_LIT:r>') as f:<EOL><INDENT>text = f.read()<EOL><DEDENT>return text<EOL><DEDENT>return '<STR_LIT>'<EOL>

Parse the description in the README file

CommandLine:
    pandoc --from=markdown --to=rst --output=README.rst README.md
    python -c "import setup; print(setup.parse_description())"


import requirements<EOL>from os.path import dirname, join, exists<EOL>require_fpath = join(dirname(__file__), fname)<EOL>if exists(require_fpath):<EOL><INDENT>with open(require_fpath, '<STR_LIT:r>') as file:<EOL><INDENT>requires = list(requirements.parse(file))<EOL><DEDENT>packages = [r.name for r in requires]<EOL>return packages<EOL><DEDENT>return []<EOL>

pip install requirements-parser
fname='requirements.txt'


from os.path import dirname, join, exists<EOL>import re<EOL>require_fpath = join(dirname(__file__), fname)<EOL>def parse_line(line):<EOL><INDENT>"""<STR_LIT>"""<EOL>info = {}<EOL>if line.startswith('<STR_LIT>'):<EOL><INDENT>info['<STR_LIT>'] = line.split('<STR_LIT>')[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>pat = '<STR_LIT:(>' + '<STR_LIT:|>'.join(['<STR_LIT>', '<STR_LIT>', '<STR_LIT:>>']) + '<STR_LIT:)>'<EOL>parts = re.split(pat, line, maxsplit=<NUM_LIT:1>)<EOL>parts = [p.strip() for p in parts]<EOL>info['<STR_LIT>'] = parts[<NUM_LIT:0>]<EOL>if len(parts) > <NUM_LIT:1>:<EOL><INDENT>op, rest = parts[<NUM_LIT:1>:]<EOL>if '<STR_LIT:;>' in rest:<EOL><INDENT>version, platform_deps = map(str.strip, rest.split('<STR_LIT:;>'))<EOL>info['<STR_LIT>'] = platform_deps<EOL><DEDENT>else:<EOL><INDENT>version = rest  <EOL><DEDENT>info['<STR_LIT:version>'] = (op, version)<EOL><DEDENT><DEDENT>return info<EOL><DEDENT>if exists(require_fpath):<EOL><INDENT>with open(require_fpath, '<STR_LIT:r>') as f:<EOL><INDENT>packages = []<EOL>for line in f.readlines():<EOL><INDENT>line = line.strip()<EOL>if line and not line.startswith('<STR_LIT:#>'):<EOL><INDENT>info = parse_line(line)<EOL>package = info['<STR_LIT>']<EOL>if not sys.version.startswith('<STR_LIT>'):<EOL><INDENT>platform_deps = info.get('<STR_LIT>')<EOL>if platform_deps is not None:<EOL><INDENT>package += '<STR_LIT:;>' + platform_deps<EOL><DEDENT><DEDENT>packages.append(package)<EOL><DEDENT><DEDENT>return packages<EOL><DEDENT><DEDENT>return []<EOL>

Parse the package dependencies listed in a requirements file but strips
specific versioning information.

TODO:
    perhaps use https://github.com/davidfischer/requirements-parser instead

CommandLine:
    python -c "import setup; print(setup.parse_requirements())"


if xxhash is not None:  <EOL><INDENT>if hasher in {'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'}:<EOL><INDENT>return xxhash.xxh32<EOL><DEDENT>if hasher in {'<STR_LIT>', '<STR_LIT>'}:<EOL><INDENT>return xxhash.xxh64<EOL><DEDENT><DEDENT>if hasher is NoParam or hasher == '<STR_LIT:default>':<EOL><INDENT>hasher = DEFAULT_HASHER<EOL><DEDENT>elif isinstance(hasher, six.string_types):<EOL><INDENT>if hasher not in hashlib.algorithms_available:<EOL><INDENT>raise KeyError('<STR_LIT>'.format(hasher))<EOL><DEDENT>else:<EOL><INDENT>hasher = getattr(hashlib, hasher)<EOL><DEDENT><DEDENT>elif isinstance(hasher, HASH):<EOL><INDENT>return lambda: hasher<EOL><DEDENT>return hasher<EOL>

Convert a string-based key into a hasher class

Notes:
    In terms of speed on 64bit systems, sha1 is the fastest followed by md5
    and sha512. The slowest algorithm is sha256. If xxhash is installed
    the fastest algorithm is xxh64.

Example:
    >>> assert _rectify_hasher(NoParam) is DEFAULT_HASHER
    >>> assert _rectify_hasher('sha1') is hashlib.sha1
    >>> assert _rectify_hasher('sha256') is hashlib.sha256
    >>> assert _rectify_hasher('sha512') is hashlib.sha512
    >>> assert _rectify_hasher('md5') is hashlib.md5
    >>> assert _rectify_hasher(hashlib.sha1) is hashlib.sha1
    >>> assert _rectify_hasher(hashlib.sha1())().name == 'sha1'
    >>> import pytest
    >>> assert pytest.raises(KeyError, _rectify_hasher, '42')
    >>> #assert pytest.raises(TypeError, _rectify_hasher, object)
    >>> if xxhash:
    >>>     assert _rectify_hasher('xxh64') is xxhash.xxh64
    >>>     assert _rectify_hasher('xxh32') is xxhash.xxh32


if base is NoParam or base == '<STR_LIT:default>':<EOL><INDENT>return DEFAULT_ALPHABET<EOL><DEDENT>elif base in [<NUM_LIT>, '<STR_LIT:abc>', '<STR_LIT>']:<EOL><INDENT>return _ALPHABET_26<EOL><DEDENT>elif base in [<NUM_LIT:16>, '<STR_LIT>']:<EOL><INDENT>return _ALPHABET_16<EOL><DEDENT>elif base in [<NUM_LIT:10>, '<STR_LIT>']:<EOL><INDENT>return _ALPHABET_10<EOL><DEDENT>else:<EOL><INDENT>if not isinstance(base, (list, tuple)):<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'.format(<EOL>type(base)))<EOL><DEDENT>return base<EOL><DEDENT>

transforms base shorthand into the full list representation

Example:
    >>> assert _rectify_base(NoParam) is DEFAULT_ALPHABET
    >>> assert _rectify_base('hex') is _ALPHABET_16
    >>> assert _rectify_base('abc') is _ALPHABET_26
    >>> assert _rectify_base(10) is _ALPHABET_10
    >>> assert _rectify_base(['1', '2']) == ['1', '2']
    >>> import pytest
    >>> assert pytest.raises(TypeError, _rectify_base, 'uselist')


if hashlen is NoParam or hashlen == '<STR_LIT:default>':<EOL><INDENT>return DEFAULT_HASHLEN<EOL><DEDENT>else:<EOL><INDENT>return hashlen<EOL><DEDENT>

Example:
    >>> assert _rectify_hashlen(NoParam) is DEFAULT_HASHLEN
    >>> assert _rectify_hashlen(8) == 8


hasher = _HashTracer()<EOL>_update_hasher(hasher, data, types=types)<EOL>return hasher.sequence<EOL>

r"""
    Extracts the sequence of bytes that would be hashed by hash_data

    Example:
        >>> data = [2, (3, 4)]
        >>> result1 = (b''.join(_hashable_sequence(data, types=False)))
        >>> result2 = (b''.join(_hashable_sequence(data, types=True)))
        >>> assert result1 == b'_[_\x02_,__[_\x03_,_\x04_,__]__]_'
        >>> assert result2 == b'_[_INT\x02_,__[_INT\x03_,_INT\x04_,__]__]_'


<EOL>if data is None:<EOL><INDENT>hashable = b'<STR_LIT>'<EOL>prefix = b'<STR_LIT>'<EOL><DEDENT>elif isinstance(data, six.binary_type):<EOL><INDENT>hashable = data<EOL>prefix = b'<STR_LIT>'<EOL><DEDENT>elif isinstance(data, six.text_type):<EOL><INDENT>hashable = data.encode('<STR_LIT:utf-8>')<EOL>prefix = b'<STR_LIT>'<EOL><DEDENT>elif isinstance(data, _intlike):<EOL><INDENT>hashable = _int_to_bytes(data)<EOL>prefix = b'<STR_LIT>'<EOL><DEDENT>elif isinstance(data, float):<EOL><INDENT>a, b = float(data).as_integer_ratio()<EOL>hashable = _int_to_bytes(a) + b'<STR_LIT:/>' +  _int_to_bytes(b)<EOL>prefix = b'<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>hash_func = _HASHABLE_EXTENSIONS.lookup(data)<EOL>prefix, hashable = hash_func(data)<EOL><DEDENT>if types:<EOL><INDENT>return prefix, hashable<EOL><DEDENT>else:<EOL><INDENT>return b'<STR_LIT>', hashable<EOL><DEDENT>

r"""
    Converts `data` into a hashable byte representation if an appropriate
    hashing function is known.

    Args:
        data (object): ordered data with structure
        types (bool): include type prefixes in the hash

    Returns:
        tuple(bytes, bytes): prefix, hashable:
            a prefix hinting the original data type and the byte representation
            of `data`.

    Raises:
        TypeError : if data has no registered hash methods

    Example:
        >>> assert _convert_to_hashable(None) == (b'NULL', b'NONE')
        >>> assert _convert_to_hashable('string') == (b'TXT', b'string')
        >>> assert _convert_to_hashable(1) == (b'INT', b'\x01')
        >>> assert _convert_to_hashable(1.0) == (b'FLT', b'\x01/\x01')
        >>> assert _convert_to_hashable(_intlike[-1](1)) == (b'INT', b'\x01')


def _update_hasher(hasher, data, types=True):

<EOL>if isinstance(data, (tuple, list, zip)):<EOL><INDENT>needs_iteration = True<EOL><DEDENT>else:<EOL><INDENT>needs_iteration = any(check(data) for check in<EOL>_HASHABLE_EXTENSIONS.iterable_checks)<EOL><DEDENT>if needs_iteration:<EOL><INDENT>SEP = b'<STR_LIT>'<EOL>ITER_PREFIX = b'<STR_LIT>'<EOL>ITER_SUFFIX = b'<STR_LIT>'<EOL>iter_ = iter(data)<EOL>hasher.update(ITER_PREFIX)<EOL>try:<EOL><INDENT>for item in iter_:<EOL><INDENT>prefix, hashable = _convert_to_hashable(item, types)<EOL>binary_data = prefix + hashable + SEP<EOL>hasher.update(binary_data)<EOL><DEDENT><DEDENT>except TypeError:<EOL><INDENT>_update_hasher(hasher, item, types)<EOL>for item in iter_:<EOL><INDENT>_update_hasher(hasher, item, types)<EOL>hasher.update(SEP)<EOL><DEDENT><DEDENT>hasher.update(ITER_SUFFIX)<EOL><DEDENT>else:<EOL><INDENT>prefix, hashable = _convert_to_hashable(data, types)<EOL>binary_data = prefix + hashable<EOL>hasher.update(binary_data)<EOL><DEDENT>

Converts `data` into a byte representation and calls update on the hasher
`hashlib.HASH` algorithm.

Args:
    hasher (HASH): instance of a hashlib algorithm
    data (object): ordered data with structure
    types (bool): include type prefixes in the hash

Example:
    >>> hasher = hashlib.sha512()
    >>> data = [1, 2, ['a', 2, 'c']]
    >>> _update_hasher(hasher, data)
    >>> print(hasher.hexdigest()[0:8])
    e2c67675

    2ba8d82b


if base is _ALPHABET_16:<EOL><INDENT>return hexstr<EOL><DEDENT>baselen = len(base)<EOL>x = int(hexstr, <NUM_LIT:16>)  <EOL>if x == <NUM_LIT:0>:<EOL><INDENT>return '<STR_LIT:0>'<EOL><DEDENT>sign = <NUM_LIT:1> if x > <NUM_LIT:0> else -<NUM_LIT:1><EOL>x *= sign<EOL>digits = []<EOL>while x:<EOL><INDENT>digits.append(base[x % baselen])<EOL>x //= baselen<EOL><DEDENT>if sign < <NUM_LIT:0>:<EOL><INDENT>digits.append('<STR_LIT:->')<EOL><DEDENT>digits.reverse()<EOL>newbase_str = '<STR_LIT>'.join(digits)<EOL>return newbase_str<EOL>

r"""
    Packs a long hexstr into a shorter length string with a larger base.

    Args:
        hexstr (str): string of hexidecimal symbols to convert
        base (list): symbols of the conversion base

    Example:
        >>> print(_convert_hexstr_base('ffffffff', _ALPHABET_26))
        nxmrlxv
        >>> print(_convert_hexstr_base('0', _ALPHABET_26))
        0
        >>> print(_convert_hexstr_base('-ffffffff', _ALPHABET_26))
        -nxmrlxv
        >>> print(_convert_hexstr_base('aafffff1', _ALPHABET_16))
        aafffff1

    Sympy:
        >>> import sympy as sy
        >>> # Determine the length savings with lossless conversion
        >>> consts = dict(hexbase=16, hexlen=256, baselen=27)
        >>> symbols = sy.symbols('hexbase, hexlen, baselen, newlen')
        >>> haexbase, hexlen, baselen, newlen = symbols
        >>> eqn = sy.Eq(16 ** hexlen,  baselen ** newlen)
        >>> newlen_ans = sy.solve(eqn, newlen)[0].subs(consts).evalf()
        >>> print('newlen_ans = %r' % (newlen_ans,))
        >>> # for a 26 char base we can get 216
        >>> print('Required length for lossless conversion len2 = %r' % (len2,))
        >>> def info(base, len):
        ...     bits = base ** len
        ...     print('base = %r' % (base,))
        ...     print('len = %r' % (len,))
        ...     print('bits = %r' % (bits,))
        >>> info(16, 256)
        >>> info(27, 16)
        >>> info(27, 64)
        >>> info(27, 216)


def _digest_hasher(hasher, hashlen, base):

<EOL>hex_text = hasher.hexdigest()<EOL>base_text = _convert_hexstr_base(hex_text, base)<EOL>text = base_text[:hashlen]<EOL>return text<EOL>


def hash_data(data, hasher=NoParam, base=NoParam, types=False,<EOL>hashlen=NoParam, convert=False):

if convert and isinstance(data, six.string_types):  <EOL><INDENT>try:<EOL><INDENT>data = json.dumps(data)<EOL><DEDENT>except TypeError as ex:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>base = _rectify_base(base)<EOL>hashlen = _rectify_hashlen(hashlen)<EOL>hasher = _rectify_hasher(hasher)()<EOL>_update_hasher(hasher, data, types=types)<EOL>text = _digest_hasher(hasher, hashlen, base)<EOL>return text<EOL>

Get a unique hash depending on the state of the data.

Args:
    data (object):
        Any sort of loosely organized data

    hasher (str or HASHER):
        Hash algorithm from hashlib, defaults to `sha512`.

    base (str or List[str]):
        Shorthand key or a list of symbols.  Valid keys are: 'abc', 'hex',
        and 'dec'. Defaults to 'hex'.

    types (bool):
        If True data types are included in the hash, otherwise only the raw
        data is hashed. Defaults to False.

    hashlen (int):
        Maximum number of symbols in the returned hash. If not specified,
        all are returned.  DEPRECATED. Use slice syntax instead.

    convert (bool, optional, default=True):
        if True, try and convert the data to json an the json is hashed
        instead. This can improve runtime in some instances, however the
        hash may differ from the case where convert=False.

Notes:
    alphabet26 is a pretty nice base, I recommend it.
    However we default to hex because it is standard.
    This means the output of hashdata with base=sha1 will be the same as
    the output of `sha1sum`.

Returns:
    str: text -  hash string

Example:
    >>> import ubelt as ub
    >>> print(ub.hash_data([1, 2, (3, '4')], convert=False))
    60b758587f599663931057e6ebdf185a...
    >>> print(ub.hash_data([1, 2, (3, '4')], base='abc',  hasher='sha512')[:32])
    hsrgqvfiuxvvhcdnypivhhthmrolkzej