def get_workflow_execution_history(domain=None, execution=None,
    nextPageToken=None, maximumPageSize=None, reverseOrder=None):
    """
    Returns the history of the specified workflow execution. The results may be split into multiple pages. To retrieve subsequent pages, make the call again using the nextPageToken returned by the initial call.
    Access Control
    You can use IAM policies to control this action's access to Amazon SWF resources as follows:
    If the caller does not have sufficient permissions to invoke the action, or the parameter values fall outside the specified constraints, the action fails. The associated event attribute's cause parameter will be set to OPERATION_NOT_PERMITTED. For details and example IAM policies, see Using IAM to Manage Access to Amazon SWF Workflows .
    See also: AWS API Documentation

    :example: response = client.get_workflow_execution_history(
        domain='string',
        execution={
            'workflowId': 'string',
            'runId': 'string'
        },
        nextPageToken='string',
        maximumPageSize=123,
        reverseOrder=True|False
    )

    :type domain: string
    :param domain: [REQUIRED]
            The name of the domain containing the workflow execution.

    :type execution: dict
    :param execution: [REQUIRED]
            Specifies the workflow execution for which to return the history.
            workflowId (string) -- [REQUIRED]The user defined identifier associated with the workflow execution.
            runId (string) -- [REQUIRED]A system-generated unique identifier for the workflow execution.

    :type nextPageToken: string
    :param nextPageToken: If a NextPageToken was returned by a previous call, there are more results available. To retrieve the next page of results, make the call again using the returned token in nextPageToken . Keep all other arguments unchanged.
            The configured maximumPageSize determines how many results can be returned in a single call.

    :type maximumPageSize: integer
    :param maximumPageSize: The maximum number of results that will be returned per call. nextPageToken can be used to obtain futher pages of results. The default is 1000, which is the maximum allowed page size. You can, however, specify a page size smaller than the maximum.
            This is an upper limit only; the actual number of results returned per call may be fewer than the specified maximum.

    :type reverseOrder: boolean
    :param reverseOrder: When set to true , returns the events in reverse order. By default the results are returned in ascending order of the eventTimeStamp of the events.

    :rtype: dict
    :return: {
        'events': [
            {
                'eventTimestamp': datetime(2015, 1, 1),
                'eventType': 'WorkflowExecutionStarted'|'WorkflowExecutionCancelRequested'|'WorkflowExecutionCompleted'|'CompleteWorkflowExecutionFailed'|'WorkflowExecutionFailed'|'FailWorkflowExecutionFailed'|'WorkflowExecutionTimedOut'|'WorkflowExecutionCanceled'|'CancelWorkflowExecutionFailed'|'WorkflowExecutionContinuedAsNew'|'ContinueAsNewWorkflowExecutionFailed'|'WorkflowExecutionTerminated'|'DecisionTaskScheduled'|'DecisionTaskStarted'|'DecisionTaskCompleted'|'DecisionTaskTimedOut'|'ActivityTaskScheduled'|'ScheduleActivityTaskFailed'|'ActivityTaskStarted'|'ActivityTaskCompleted'|'ActivityTaskFailed'|'ActivityTaskTimedOut'|'ActivityTaskCanceled'|'ActivityTaskCancelRequested'|'RequestCancelActivityTaskFailed'|'WorkflowExecutionSignaled'|'MarkerRecorded'|'RecordMarkerFailed'|'TimerStarted'|'StartTimerFailed'|'TimerFired'|'TimerCanceled'|'CancelTimerFailed'|'StartChildWorkflowExecutionInitiated'|'StartChildWorkflowExecutionFailed'|'ChildWorkflowExecutionStarted'|'ChildWorkflowExecutionCompleted'|'ChildWorkflowExecutionFailed'|'ChildWorkflowExecutionTimedOut'|'ChildWorkflowExecutionCanceled'|'ChildWorkflowExecutionTerminated'|'SignalExternalWorkflowExecutionInitiated'|'SignalExternalWorkflowExecutionFailed'|'ExternalWorkflowExecutionSignaled'|'RequestCancelExternalWorkflowExecutionInitiated'|'RequestCancelExternalWorkflowExecutionFailed'|'ExternalWorkflowExecutionCancelRequested'|'LambdaFunctionScheduled'|'LambdaFunctionStarted'|'LambdaFunctionCompleted'|'LambdaFunctionFailed'|'LambdaFunctionTimedOut'|'ScheduleLambdaFunctionFailed'|'StartLambdaFunctionFailed',
                'eventId': 123,
                'workflowExecutionStartedEventAttributes': {
                    'input': 'string',
                    'executionStartToCloseTimeout': 'string',
                    'taskStartToCloseTimeout': 'string',
                    'childPolicy': 'TERMINATE'|'REQUEST_CANCEL'|'ABANDON',
                    'taskList': {
                        'name': 'string'
                    },
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'tagList': [
                        'string',
                    ],
                    'taskPriority': 'string',
                    'continuedExecutionRunId': 'string',
                    'parentWorkflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'parentInitiatedEventId': 123,
                    'lambdaRole': 'string'
                },
                'workflowExecutionCompletedEventAttributes': {
                    'result': 'string',
                    'decisionTaskCompletedEventId': 123
                },
                'completeWorkflowExecutionFailedEventAttributes': {
                    'cause': 'UNHANDLED_DECISION'|'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'workflowExecutionFailedEventAttributes': {
                    'reason': 'string',
                    'details': 'string',
                    'decisionTaskCompletedEventId': 123
                },
                'failWorkflowExecutionFailedEventAttributes': {
                    'cause': 'UNHANDLED_DECISION'|'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'workflowExecutionTimedOutEventAttributes': {
                    'timeoutType': 'START_TO_CLOSE',
                    'childPolicy': 'TERMINATE'|'REQUEST_CANCEL'|'ABANDON'
                },
                'workflowExecutionCanceledEventAttributes': {
                    'details': 'string',
                    'decisionTaskCompletedEventId': 123
                },
                'cancelWorkflowExecutionFailedEventAttributes': {
                    'cause': 'UNHANDLED_DECISION'|'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'workflowExecutionContinuedAsNewEventAttributes': {
                    'input': 'string',
                    'decisionTaskCompletedEventId': 123,
                    'newExecutionRunId': 'string',
                    'executionStartToCloseTimeout': 'string',
                    'taskList': {
                        'name': 'string'
                    },
                    'taskPriority': 'string',
                    'taskStartToCloseTimeout': 'string',
                    'childPolicy': 'TERMINATE'|'REQUEST_CANCEL'|'ABANDON',
                    'tagList': [
                        'string',
                    ],
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'lambdaRole': 'string'
                },
                'continueAsNewWorkflowExecutionFailedEventAttributes': {
                    'cause': 'UNHANDLED_DECISION'|'WORKFLOW_TYPE_DEPRECATED'|'WORKFLOW_TYPE_DOES_NOT_EXIST'|'DEFAULT_EXECUTION_START_TO_CLOSE_TIMEOUT_UNDEFINED'|'DEFAULT_TASK_START_TO_CLOSE_TIMEOUT_UNDEFINED'|'DEFAULT_TASK_LIST_UNDEFINED'|'DEFAULT_CHILD_POLICY_UNDEFINED'|'CONTINUE_AS_NEW_WORKFLOW_EXECUTION_RATE_EXCEEDED'|'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'workflowExecutionTerminatedEventAttributes': {
                    'reason': 'string',
                    'details': 'string',
                    'childPolicy': 'TERMINATE'|'REQUEST_CANCEL'|'ABANDON',
                    'cause': 'CHILD_POLICY_APPLIED'|'EVENT_LIMIT_EXCEEDED'|'OPERATOR_INITIATED'
                },
                'workflowExecutionCancelRequestedEventAttributes': {
                    'externalWorkflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'externalInitiatedEventId': 123,
                    'cause': 'CHILD_POLICY_APPLIED'
                },
                'decisionTaskScheduledEventAttributes': {
                    'taskList': {
                        'name': 'string'
                    },
                    'taskPriority': 'string',
                    'startToCloseTimeout': 'string'
                },
                'decisionTaskStartedEventAttributes': {
                    'identity': 'string',
                    'scheduledEventId': 123
                },
                'decisionTaskCompletedEventAttributes': {
                    'executionContext': 'string',
                    'scheduledEventId': 123,
                    'startedEventId': 123
                },
                'decisionTaskTimedOutEventAttributes': {
                    'timeoutType': 'START_TO_CLOSE',
                    'scheduledEventId': 123,
                    'startedEventId': 123
                },
                'activityTaskScheduledEventAttributes': {
                    'activityType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'activityId': 'string',
                    'input': 'string',
                    'control': 'string',
                    'scheduleToStartTimeout': 'string',
                    'scheduleToCloseTimeout': 'string',
                    'startToCloseTimeout': 'string',
                    'taskList': {
                        'name': 'string'
                    },
                    'taskPriority': 'string',
                    'decisionTaskCompletedEventId': 123,
                    'heartbeatTimeout': 'string'
                },
                'activityTaskStartedEventAttributes': {
                    'identity': 'string',
                    'scheduledEventId': 123
                },
                'activityTaskCompletedEventAttributes': {
                    'result': 'string',
                    'scheduledEventId': 123,
                    'startedEventId': 123
                },
                'activityTaskFailedEventAttributes': {
                    'reason': 'string',
                    'details': 'string',
                    'scheduledEventId': 123,
                    'startedEventId': 123
                },
                'activityTaskTimedOutEventAttributes': {
                    'timeoutType': 'START_TO_CLOSE'|'SCHEDULE_TO_START'|'SCHEDULE_TO_CLOSE'|'HEARTBEAT',
                    'scheduledEventId': 123,
                    'startedEventId': 123,
                    'details': 'string'
                },
                'activityTaskCanceledEventAttributes': {
                    'details': 'string',
                    'scheduledEventId': 123,
                    'startedEventId': 123,
                    'latestCancelRequestedEventId': 123
                },
                'activityTaskCancelRequestedEventAttributes': {
                    'decisionTaskCompletedEventId': 123,
                    'activityId': 'string'
                },
                'workflowExecutionSignaledEventAttributes': {
                    'signalName': 'string',
                    'input': 'string',
                    'externalWorkflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'externalInitiatedEventId': 123
                },
                'markerRecordedEventAttributes': {
                    'markerName': 'string',
                    'details': 'string',
                    'decisionTaskCompletedEventId': 123
                },
                'recordMarkerFailedEventAttributes': {
                    'markerName': 'string',
                    'cause': 'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'timerStartedEventAttributes': {
                    'timerId': 'string',
                    'control': 'string',
                    'startToFireTimeout': 'string',
                    'decisionTaskCompletedEventId': 123
                },
                'timerFiredEventAttributes': {
                    'timerId': 'string',
                    'startedEventId': 123
                },
                'timerCanceledEventAttributes': {
                    'timerId': 'string',
                    'startedEventId': 123,
                    'decisionTaskCompletedEventId': 123
                },
                'startChildWorkflowExecutionInitiatedEventAttributes': {
                    'workflowId': 'string',
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'control': 'string',
                    'input': 'string',
                    'executionStartToCloseTimeout': 'string',
                    'taskList': {
                        'name': 'string'
                    },
                    'taskPriority': 'string',
                    'decisionTaskCompletedEventId': 123,
                    'childPolicy': 'TERMINATE'|'REQUEST_CANCEL'|'ABANDON',
                    'taskStartToCloseTimeout': 'string',
                    'tagList': [
                        'string',
                    ],
                    'lambdaRole': 'string'
                },
                'childWorkflowExecutionStartedEventAttributes': {
                    'workflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'initiatedEventId': 123
                },
                'childWorkflowExecutionCompletedEventAttributes': {
                    'workflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'result': 'string',
                    'initiatedEventId': 123,
                    'startedEventId': 123
                },
                'childWorkflowExecutionFailedEventAttributes': {
                    'workflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'reason': 'string',
                    'details': 'string',
                    'initiatedEventId': 123,
                    'startedEventId': 123
                },
                'childWorkflowExecutionTimedOutEventAttributes': {
                    'workflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'timeoutType': 'START_TO_CLOSE',
                    'initiatedEventId': 123,
                    'startedEventId': 123
                },
                'childWorkflowExecutionCanceledEventAttributes': {
                    'workflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'details': 'string',
                    'initiatedEventId': 123,
                    'startedEventId': 123
                },
                'childWorkflowExecutionTerminatedEventAttributes': {
                    'workflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'initiatedEventId': 123,
                    'startedEventId': 123
                },
                'signalExternalWorkflowExecutionInitiatedEventAttributes': {
                    'workflowId': 'string',
                    'runId': 'string',
                    'signalName': 'string',
                    'input': 'string',
                    'decisionTaskCompletedEventId': 123,
                    'control': 'string'
                },
                'externalWorkflowExecutionSignaledEventAttributes': {
                    'workflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'initiatedEventId': 123
                },
                'signalExternalWorkflowExecutionFailedEventAttributes': {
                    'workflowId': 'string',
                    'runId': 'string',
                    'cause': 'UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION'|'SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_RATE_EXCEEDED'|'OPERATION_NOT_PERMITTED',
                    'initiatedEventId': 123,
                    'decisionTaskCompletedEventId': 123,
                    'control': 'string'
                },
                'externalWorkflowExecutionCancelRequestedEventAttributes': {
                    'workflowExecution': {
                        'workflowId': 'string',
                        'runId': 'string'
                    },
                    'initiatedEventId': 123
                },
                'requestCancelExternalWorkflowExecutionInitiatedEventAttributes': {
                    'workflowId': 'string',
                    'runId': 'string',
                    'decisionTaskCompletedEventId': 123,
                    'control': 'string'
                },
                'requestCancelExternalWorkflowExecutionFailedEventAttributes': {
                    'workflowId': 'string',
                    'runId': 'string',
                    'cause': 'UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION'|'REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_RATE_EXCEEDED'|'OPERATION_NOT_PERMITTED',
                    'initiatedEventId': 123,
                    'decisionTaskCompletedEventId': 123,
                    'control': 'string'
                },
                'scheduleActivityTaskFailedEventAttributes': {
                    'activityType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'activityId': 'string',
                    'cause': 'ACTIVITY_TYPE_DEPRECATED'|'ACTIVITY_TYPE_DOES_NOT_EXIST'|'ACTIVITY_ID_ALREADY_IN_USE'|'OPEN_ACTIVITIES_LIMIT_EXCEEDED'|'ACTIVITY_CREATION_RATE_EXCEEDED'|'DEFAULT_SCHEDULE_TO_CLOSE_TIMEOUT_UNDEFINED'|'DEFAULT_TASK_LIST_UNDEFINED'|'DEFAULT_SCHEDULE_TO_START_TIMEOUT_UNDEFINED'|'DEFAULT_START_TO_CLOSE_TIMEOUT_UNDEFINED'|'DEFAULT_HEARTBEAT_TIMEOUT_UNDEFINED'|'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'requestCancelActivityTaskFailedEventAttributes': {
                    'activityId': 'string',
                    'cause': 'ACTIVITY_ID_UNKNOWN'|'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'startTimerFailedEventAttributes': {
                    'timerId': 'string',
                    'cause': 'TIMER_ID_ALREADY_IN_USE'|'OPEN_TIMERS_LIMIT_EXCEEDED'|'TIMER_CREATION_RATE_EXCEEDED'|'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'cancelTimerFailedEventAttributes': {
                    'timerId': 'string',
                    'cause': 'TIMER_ID_UNKNOWN'|'OPERATION_NOT_PERMITTED',
                    'decisionTaskCompletedEventId': 123
                },
                'startChildWorkflowExecutionFailedEventAttributes': {
                    'workflowType': {
                        'name': 'string',
                        'version': 'string'
                    },
                    'cause': 'WORKFLOW_TYPE_DOES_NOT_EXIST'|'WORKFLOW_TYPE_DEPRECATED'|'OPEN_CHILDREN_LIMIT_EXCEEDED'|'OPEN_WORKFLOWS_LIMIT_EXCEEDED'|'CHILD_CREATION_RATE_EXCEEDED'|'WORKFLOW_ALREADY_RUNNING'|'DEFAULT_EXECUTION_START_TO_CLOSE_TIMEOUT_UNDEFINED'|'DEFAULT_TASK_LIST_UNDEFINED'|'DEFAULT_TASK_START_TO_CLOSE_TIMEOUT_UNDEFINED'|'DEFAULT_CHILD_POLICY_UNDEFINED'|'OPERATION_NOT_PERMITTED',
                    'workflowId': 'string',
                    'initiatedEventId': 123,
                    'decisionTaskCompletedEventId': 123,
                    'control': 'string'
                },
                'lambdaFunctionScheduledEventAttributes': {
                    'id': 'string',
                    'name': 'string',
                    'input': 'string',
                    'startToCloseTimeout': 'string',
                    'decisionTaskCompletedEventId': 123
                },
                'lambdaFunctionStartedEventAttributes': {
                    'scheduledEventId': 123
                },
                'lambdaFunctionCompletedEventAttributes': {
                    'scheduledEventId': 123,
                    'startedEventId': 123,
                    'result': 'string'
                },
                'lambdaFunctionFailedEventAttributes': {
                    'scheduledEventId': 123,
                    'startedEventId': 123,
                    'reason': 'string',
                    'details': 'string'
                },
                'lambdaFunctionTimedOutEventAttributes': {
                    'scheduledEventId': 123,
                    'startedEventId': 123,
                    'timeoutType': 'START_TO_CLOSE'
                },
                'scheduleLambdaFunctionFailedEventAttributes': {
                    'id': 'string',
                    'name': 'string',
                    'cause': 'ID_ALREADY_IN_USE'|'OPEN_LAMBDA_FUNCTIONS_LIMIT_EXCEEDED'|'LAMBDA_FUNCTION_CREATION_RATE_EXCEEDED'|'LAMBDA_SERVICE_NOT_AVAILABLE_IN_REGION',
                    'decisionTaskCompletedEventId': 123
                },
                'startLambdaFunctionFailedEventAttributes': {
                    'scheduledEventId': 123,
                    'cause': 'ASSUME_ROLE_FAILED',
                    'message': 'string'
                }
            },
        ],
        'nextPageToken': 'string'
    }

    :returns:
    domain (string) -- [REQUIRED]
    The name of the domain containing the workflow execution.

    execution (dict) -- [REQUIRED]
    Specifies the workflow execution for which to return the history.

    workflowId (string) -- [REQUIRED]The user defined identifier associated with the workflow execution.

    runId (string) -- [REQUIRED]A system-generated unique identifier for the workflow execution.

    nextPageToken (string) -- If a NextPageToken was returned by a previous call, there are more results available. To retrieve the next page of results, make the call again using the returned token in nextPageToken . Keep all other arguments unchanged.
    The configured maximumPageSize determines how many results can be returned in a single call.

    maximumPageSize (integer) -- The maximum number of results that will be returned per call. nextPageToken can be used to obtain futher pages of results. The default is 1000, which is the maximum allowed page size. You can, however, specify a page size smaller than the maximum.
    This is an upper limit only; the actual number of results returned per call may be fewer than the specified maximum.

    reverseOrder (boolean) -- When set to true , returns the events in reverse order. By default the results are returned in ascending order of the eventTimeStamp of the events.

    """
    pass


def samIterator(sam):
    """Creates an iterator over the aligned reads in a sam file, filtering out
    any reads that have no reference alignment.
    """
    for aR in sam:
        if aR.reference_id != -1:
            yield aR


def point_to_js_command(point_coord, point_name):
    """
    Returning a js script.
    :param point_coord:
    :param point_name:
    :return:
    """
    return point_name + ' = Point(' + str(point_coord[0]) + ', 0, ' + str(
        point_coord[1]) + ');\n'


def ERA2Model(data, lon_name='longitude', lat_name='latitude'):
    """
		Invert the direction of latitude, and swap longitude domain
		 from -180,180 to 0,360.
		 Assumes an xr.DataArray or xr.Dataset.

		INPUTS:
			data:      xarray.DataArray or xarray.Dataset to regrid
			lon_name:  name of longitude dimension. Set to None if nothing should be done.
			lat_name:  name of latitude dimension. Set to None if nothing should be done.
		OUTPUTS:
			data:     xarray.DataArray with latitude swapped and
					   longitude from 0 to 360 degrees.
	"""
    if lat_name is not None:
        data = data.interp({lat_name: data[lat_name][::-1]}, method='nearest')
    if lon_name is not None:
        data_low = data.sel({lon_name: slice(0, 180)})
        data_neg = data.sel({lon_name: slice(-180, -0.001)})
        data_neg[lon_name] += 360
    return data.sortby(lon_name)


def is_input_port(prog, inp, tlsa):
    """Set the Tlsa object's port data if the input is 'port-like'.

    Args:
        prog (State): not changed.
        inp (str): the input to check if it is a port number.
        tlsa (Tlsa): the Tlsa object to set with the port number if 'inp'
            is an integer.

    Returns:
        bool: 'True' if the port data in 'tlsa' was set to 'inp', 'False'
            if not.
    """
    try:
        port_num = int(inp)
        if port_num == 0 or port_num > 65535:
            return False
    except ValueError:
        return False
    tlsa.port = inp
    return True


@classmethod
def delete(cls, calendar_id):
    """
        Deletes the calendar specified by calendar_id.

        Parameters
        ----------
        calendar_id : str
            The id of the calendar to delete.
            The requester must have OWNER access for this calendar.

        Raises
        ------
        ClientError
            Raised if an invalid calendar_id is provided.

        Examples
        --------
        .. code-block:: python

            # Deleting with a valid calendar_id
            status_code = dr.CalendarFile.delete(some_calendar_id)
            status_code
            >>> 204
            dr.CalendarFile.get(some_calendar_id)
            >>> ClientError: Item not found
        """
    cls._client.delete(cls._calendar_url.format(calendar_id))


@classmethod
def __jsonDecode__(cls, state):
    """Returns an object of cls based on a given state"""
    return cls(name=state.pop('name'), owner=state.pop('owner'), locations=
        state.pop('locations'))


def transformNode(doc, newTag, node=None, **attrDict):
    """Transform a DOM node into new node and copy selected attributes.

    Creates a new DOM node with tag name 'newTag' for document 'doc'
    and copies selected attributes from an existing 'node' as provided
    in 'attrDict'. The source 'node' can be None. Attribute values will
    be converted to strings.

    E.g.

        n = transformNode(doc, "node1", x="0", y="1")
        -> DOM node for <node1 x="0" y="1"/>

        n = transformNode(doc, "node1", x=0, y=1+1)
        -> DOM node for <node1 x="0" y="2"/>

        n = transformNode(doc, "node1", node0, x="x0", y="x0", zoo=bar())
        -> DOM node for <node1 x="[node0.x0]" y="[node0.y0]" zoo="[bar()]"/>
    """
    newNode = doc.createElement(newTag)
    for newAttr, attr in attrDict.items():
        sattr = str(attr)
        if not node:
            newNode.setAttribute(newAttr, sattr)
        else:
            attrVal = node.getAttribute(sattr)
            newNode.setAttribute(newAttr, attrVal or sattr)
    return newNode


def with_metaclass(meta, *bases):
    """Create a base class with a metaclass."""
    return meta('NewBase', bases, {})


@classmethod
def print_variable_name_and_value(cls, name, value):
    """Prints variables in the execution console"""
    print('{var_name:<24} ==> {var_value}'.format(var_name=name, var_value=
        value))


def get_environ(environ):
    """
    Returns our whitelisted environment variables.
    """
    for key in ('REMOTE_ADDR', 'SERVER_NAME', 'SERVER_PORT'):
        if key in environ:
            yield key, environ[key]


def remove_dupes(iterable):
    """
    Removes duplicate items from iterable object preserving original iterable order
    :param iterable: iterable
    :return: iterable
    """
    unique = set()
    new_iter = iterable.__class__()
    for item in iterable:
        if item not in unique:
            new_iter.append(item)
        unique.add(item)
    return new_iter


def dict_factory(cursor, row):
    """Changes the data returned from the db from a
    tupple to a dictionary"""
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d


def draw(win, y, x, s):
    """ Clip and plot, inverting y """
    rows, cols = win.getmaxyx()
    ix = int(x)
    iy = -int(y) + rows - 1
    if iy >= 0 and iy < rows:
        if ix >= 0 and ix < cols:
            win.addstr(iy, ix, s)


@classmethod
def merge(cls, session, items):
    """Models should override this to perform a merge"""
    return items


def ensure_instance(value, types):
    """
    Ensure value is an instance of a certain type

    >>> ensure_instance(1, [str])
    Traceback (most recent call last):
    TypeError:

    >>> ensure_instance(1, str)
    Traceback (most recent call last):
    TypeError:

    >>> ensure_instance(1, int)
    >>> ensure_instance(1, (int, str))

    :attr types: Type of list of types
    """
    if not isinstance(value, types):
        raise TypeError('expected instance of {}, got {}'.format(types, value))


def mru(oid):
    """ Move the element corresonding to 'oid' to the head.

        o Raise KeyError if no element is found.
        """


def ghost_curves_clear():
    """Clear F-Curve snapshots (Ghosts) for active Graph Editor

    """
    pass


def _strxfrm(s):
    """ strxfrm(string) -> string.
        Returns a string that behaves for cmp locale-aware.
    """
    return s


def always(x):
    """Always return true."""
    return True


def operator_preset_add(name: str='', remove_active: bool=False, operator:
    str=''):
    """Add or remove an Operator Preset

    :param name: Name, Name of the preset, used to make the path name
    :type name: str
    :param remove_active: remove_active
    :type remove_active: bool
    :param operator: Operator
    :type operator: str
    """
    pass


@staticmethod
def _sample_weight(X, X_raw):
    """
        Maybe it will help to handle cases when the parameter is needed in a scorer.
        Introduced this template method just to not forget about it.
        """
    pass


def set_git_repo_metadata_from_path(model, repo_type, path='.', url_attr=
    'url', branch_attr='branch', commit_hash_attr='revision'):
    """ Use Git to set the Git repository URL, branch, and commit hash metadata attributes of a model

    Args:
        model (:obj:`Model`): model whose Git attributes will be set
        repo_type (:obj:`git.RepoMetadataCollectionType`): repo type being set
        path (:obj:`str`, optional): path to file or directory in a clone of a Git repository; default='.'
        url_attr (:obj:`str`, optional): attribute in `model` for the Git URL; default='url'
        branch_attr (:obj:`str`, optional): attribute in `model` for the Git branch; default='branch'
        commit_hash_attr (:obj:`str`, optional): attribute in `model` for the Git commit hash;
            default='revision'

    Returns:
        :obj:`list` of :obj:`str`: list of reasons, if any, that the repo might not be
            suitable for collecting metadata
    """
    from wc_utils.util import git
    md, unsuitable_changes = git.get_repo_metadata(path=path, repo_type=
        repo_type, data_file=path)
    setattr(model, url_attr, md.url)
    setattr(model, branch_attr, md.branch)
    setattr(model, commit_hash_attr, md.revision)
    return unsuitable_changes


def active_frames_delete_all():
    """Delete the active frame(s) of all editable Grease Pencil layers

    """
    pass


def looks_like_issubclass(obj, classname):
    """ Return True if the object has a class or superclass with the given class
    name.

    Ignores old-style classes.
    """
    t = obj
    if t.__name__ == classname:
        return True
    for klass in t.__mro__:
        if klass.__name__ == classname:
            return True
    return False


def urlopen(*args, **kwargs):
    """ Compatibility function for the urlopen function. Raises an
    RuntimeError if urlopen could not be imported (which can occur in
    frozen applications.
    """
    try:
        from urllib.request import urlopen
    except ImportError:
        raise RuntimeError('Could not import urlopen.')
    return urlopen(*args, **kwargs)


def plus_or_dot(pieces):
    """Return a + if we don't already have one, else return a ."""
    if '+' in pieces.get('closest-tag', ''):
        return '.'
    return '+'


def get_public_version():
    """Get the id of the public version.
        """


@staticmethod
def _getTags():
    """returns Tags for dump

        :rtype: list of str"""
    return ['NFA']


@classmethod
def file_unresolve_comment(cls, val):
    """
        Create an instance of this class set to the ``file_unresolve_comment``
        tag with value ``val``.

        :param FileUnresolveCommentType val:
        :rtype: EventType
        """
    return cls('file_unresolve_comment', val)


def timetuple():
    """Return a 9-element tuple of the form returned by time.localtime()."""


def find_module(root, predicate):
    """Find a (sub) module using a predicate.

    :param predicate:
        a callable with arguments ``(name, module)``.
    :returns:
        the first module for which the predicate is true or raises
        a ``RuntimeError``.
    """
    for k, v in root.named_modules():
        if predicate(k, v):
            return v
    else:
        raise RuntimeError('could not find module')


def encoding_name(s):
    """Obtain 'br' out of ' br;q=0.5' or similar."""
    if ';' in s:
        s, q = s.split(';')
        if '=' in q:
            _, q = q.split('=')
            if float(q) == 0.0:
                return None
    return s.strip()


def _set_summary_fields(ctx):
    """Sets document summary fields.

    """
    if len(ctx.meta.external_ids):
        ctx.ext.summary_fields += ctx.meta.external_ids[0].value,


@staticmethod
def copy_with_comments(source_dict, source_key, dest_dict, dest_key,
    delete_source=False, logger=None):
    """Copy dict with comments in config."""
    dest_dict[dest_key] = source_dict[source_key]
    try:
        dest_dict.ca.items[dest_key] = source_dict.ca.items[source_key]
        if logger and not delete_source:
            logger.debug('Copying key: %s -> %s', source_key, dest_key)
    except (KeyError, AttributeError):
        pass
    if delete_source:
        if logger:
            logger.debug('Moving key: %s -> %s', source_key, dest_key)
        del source_dict[source_key]
        source_dict.ca.items.pop(source_key, None)


def fetch(cls, widget, wcls, size, focus):
    """
        Return the cached canvas or None.

        widget -- widget object requested
        wcls -- widget class that contains render() function
        size, focus -- render() parameters
        """
    cls.fetches += 1
    sizes = cls._widgets.get(widget, None)
    if not sizes:
        return None
    ref = sizes.get((wcls, size, focus), None)
    if not ref:
        return None
    canv = ref()
    if canv:
        cls.hits += 1
    return canv


def translate_attach_remove_on_cancel(TRANSFORM_OT_translate=None,
    NODE_OT_attach=None, NODE_OT_insert_offset=None):
    """Move nodes and attach to frame

    :param TRANSFORM_OT_translate: Move, Move selected items
    :param NODE_OT_attach: Attach Nodes, Attach active node to a frame
    :param NODE_OT_insert_offset: Insert Offset, Automatically offset nodes on insertion
    """
    pass


def make_internal():
    """Make active text file internal

    """
    pass


def update_joins(joins, new_downstreams, new_upstreams):
    """
    Update new upstream and downstream segment IDs into joins table.

    Parameters
    ----------
    joins : DataFrame
        contains records with upstream_id and downstream_id representing joins between segments
    new_dowstreams : Series
        Series, indexed on original line ID, with the new downstream ID for each original line ID
    new_upstreams : Series
        Series, indexed on original line ID, with the new upstream ID for each original line ID

    Returns
    -------
    DataFrame
    """
    joins = joins.join(new_downstreams, on='downstream_id').join(new_upstreams,
        on='upstream_id')
    idx = joins.new_downstream_id.notnull()
    joins.loc[idx, 'downstream_id'] = joins[idx].new_downstream_id.astype(
        'uint32')
    idx = joins.new_upstream_id.notnull()
    joins.loc[idx, 'upstream_id'] = joins[idx].new_upstream_id.astype('uint32')
    return joins.drop(columns=['new_downstream_id', 'new_upstream_id'])


def vertex_color_dirt(blur_strength: float=1.0, blur_iterations: int=1,
    clean_angle: float=3.14159, dirt_angle: float=0.0, dirt_only: bool=False):
    """Undocumented contribute <https://developer.blender.org/T51061>

    :param blur_strength: Blur Strength, Blur strength per iteration
    :type blur_strength: float
    :param blur_iterations: Blur Iterations, Number of times to blur the colors (higher blurs more)
    :type blur_iterations: int
    :param clean_angle: Highlight Angle, Less than 90 limits the angle used in the tonal range
    :type clean_angle: float
    :param dirt_angle: Dirt Angle, Less than 90 limits the angle used in the tonal range
    :type dirt_angle: float
    :param dirt_only: Dirt Only, Don’t calculate cleans for convex areas
    :type dirt_only: bool
    """
    pass


def normalize_comment_text(text: str) ->str:
    """
    Return a consistently formatted comment from the given Comment instance.
    All comments should have a single space between the hash sign and the content.
    """
    normalized_text = text[1:].strip()
    return ('# ' + normalized_text).strip()


def register_task_with_maintenance_window(WindowId=None, Targets=None,
    TaskArn=None, ServiceRoleArn=None, TaskType=None, TaskParameters=None,
    Priority=None, MaxConcurrency=None, MaxErrors=None, LoggingInfo=None,
    ClientToken=None):
    """
    Adds a new task to a Maintenance Window.
    See also: AWS API Documentation

    :example: response = client.register_task_with_maintenance_window(
        WindowId='string',
        Targets=[
            {
                'Key': 'string',
                'Values': [
                    'string',
                ]
            },
        ],
        TaskArn='string',
        ServiceRoleArn='string',
        TaskType='RUN_COMMAND',
        TaskParameters={
            'string': {
                'Values': [
                    'string',
                ]
            }
        },
        Priority=123,
        MaxConcurrency='string',
        MaxErrors='string',
        LoggingInfo={
            'S3BucketName': 'string',
            'S3KeyPrefix': 'string',
            'S3Region': 'string'
        },
        ClientToken='string'
    )

    :type WindowId: string
    :param WindowId: [REQUIRED]
            The id of the Maintenance Window the task should be added to.

    :type Targets: list
    :param Targets: [REQUIRED]
            The targets (either instances or tags). Instances are specified using Key=instanceids,Values=instanceid1,instanceid2. Tags are specified using Key=tag name,Values=tag value.
            (dict) --An array of search criteria that targets instances using a Key,Value combination that you specify. Targets is required if you don't provide one or more instance IDs in the call.
            Key (string) --User-defined criteria for sending commands that target instances that meet the criteria. Key can be tag:Amazon EC2 tagor InstanceIds. For more information about how to send commands that target instances using Key,Value parameters, see Executing a Command Using Systems Manager Run Command .
            Values (list) --User-defined criteria that maps to Key. For example, if you specified tag:ServerRole, you could specify value:WebServer to execute a command on instances that include Amazon EC2 tags of ServerRole,WebServer. For more information about how to send commands that target instances using Key,Value parameters, see Executing a Command Using Systems Manager Run Command .
            (string) --

    :type TaskArn: string
    :param TaskArn: [REQUIRED]
            The ARN of the task to execute

    :type ServiceRoleArn: string
    :param ServiceRoleArn: [REQUIRED]
            The role that should be assumed when executing the task.

    :type TaskType: string
    :param TaskType: [REQUIRED]
            The type of task being registered.

    :type TaskParameters: dict
    :param TaskParameters: The parameters that should be passed to the task when it is executed.
            (string) --
            (dict) --Defines the values for a task parameter.
            Values (list) --This field contains an array of 0 or more strings, each 1 to 255 characters in length.
            (string) --

    :type Priority: integer
    :param Priority: The priority of the task in the Maintenance Window, the lower the number the higher the priority. Tasks in a Maintenance Window are scheduled in priority order with tasks that have the same priority scheduled in parallel.

    :type MaxConcurrency: string
    :param MaxConcurrency: [REQUIRED]
            The maximum number of targets this task can be run for in parallel.

    :type MaxErrors: string
    :param MaxErrors: [REQUIRED]
            The maximum number of errors allowed before this task stops being scheduled.

    :type LoggingInfo: dict
    :param LoggingInfo: A structure containing information about an Amazon S3 bucket to write instance-level logs to.
            S3BucketName (string) -- [REQUIRED]The name of an Amazon S3 bucket where execution logs are stored .
            S3KeyPrefix (string) --(Optional) The Amazon S3 bucket subfolder.
            S3Region (string) -- [REQUIRED]The region where the Amazon S3 bucket is located.

    :type ClientToken: string
    :param ClientToken: User-provided idempotency token.
            This field is autopopulated if not provided.

    :rtype: dict
    :return: {
        'WindowTaskId': 'string'
    }

    """
    pass


@staticmethod
def _get_object_tile_position(tile_set, object_position):
    """

        Return tuple of x1, y1, x2, y2 corresponding to position of object in tileset image.

        :param tile_set: tmx.Tileset where extract positions
        :param object_position: position of wanted object positions
        :return: tuple of x1, y1, x2, y2 corresponding to position of object in tileset image
        :rtype: tuple
        """
    absolute_start_x = tile_set.tilewidth * object_position
    y_decal = absolute_start_x // int(tile_set.image.width)
    start_y = tile_set.tileheight * y_decal
    start_x = absolute_start_x % int(tile_set.image.width)
    return int(start_x), int(start_y), int(start_x + tile_set.tilewidth), int(
        start_y + tile_set.tileheight)


def append_slash(url):
    """Make sure we append a slash at the end of the URL otherwise we
    have issues with urljoin Example:
    >>> urlparse.urljoin('http://www.example.com/api/v3', 'user/1/')
    'http://www.example.com/api/user/1/'
    """
    if url and not url.endswith('/'):
        url = '{0}/'.format(url)
    return url


def rgb2hex(rgb):
    """
    Convertit un tuple RGB en hexadécimal
    """
    return '#%02x%02x%02x' % rgb


def is_num(var):
    """Check if var string is num. Should use it only with strings."""
    try:
        int(var)
        return True
    except ValueError:
        return False


def get_route_file(file_path, file_name):
    """
    This method build the path for a file MEDIA.

    Args:
        file_path (str): File path.
        file_name (str): File name.

    Returns:
        str: Concatenate file path + file name.
    """
    try:
        route_file = file_path + '/' + file_name
    except Exception:
        route_file = ''
    return route_file


@classmethod
def _factory(cls, constraints, op):
    """ Factory for joining constraints with a single conjunction """
    pieces = []
    for i, constraint in enumerate(constraints):
        pieces.append(constraint)
        if i != len(constraints) - 1:
            pieces.append(op)
    return cls(pieces)


def camelcase(string):
    """
    >>> camelcase('get_sms_attributes_now')
    'GetSmsAttributesNow'
    """
    return string.title().replace('_', '')


def iteritems(obj):
    """ Iterator of key, value pairs for obj;
    equivalent to obj.items() on Python 3+ and obj.iteritems() on Python 2 """
    try:
        return obj.iteritems()
    except AttributeError:
        return obj.items()


@staticmethod
def atx_level_1(title):
    """Return a atx level 1 header.

        :param str title: text title.
        :return: a header title of form: ``'\\n#' + title + '\\n'``
        :rtype: str
        """
    return '\n# ' + title + '\n'


def escape_newline(s: str) ->str:
    """Replaces each new line character (\\n) in the input with \\\\n"""
    return s.replace('\n', '\\n')


def _loc(name, size=(0, 0), title_space=0, frame=0.01):
    """Convert loc argument to ``(x, y)`` of bottom left edge"""
    if isinstance(name, str):
        y, x = name.split()
    elif len(name) == 2:
        x, y = name
    else:
        raise NotImplementedError('loc needs to be string or len=2 tuple/list')
    if isinstance(x, str):
        if x == 'left':
            x = frame
        elif x in ['middle', 'center', 'centre']:
            x = 0.5 - size[0] / 2
        elif x == 'right':
            x = 1 - frame - size[0]
        else:
            raise ValueError(x)
    if isinstance(y, str):
        if y in ['top', 'upper']:
            y = 1 - frame - title_space - size[1]
        elif y in ['middle', 'center', 'centre']:
            y = 0.5 - title_space / 2.0 - size[1] / 2.0
        elif y in ['lower', 'bottom']:
            y = frame
        else:
            raise ValueError(y)
    return x, y


def __delattr__(cls, name):
    """ Remove an attribute from the class, clearing `_unbound_fields` if
        needed.
        """
    if not name.startswith('_'):
        cls._unbound_fields = None
    type.__delattr__(cls, name)


def _bitsize(count, chars):
    """helper for bitsize() methods"""
    if chars and count:
        import math
        return int(count * math.log(len(chars), 2))
    else:
        return 0


def repack_blockheader(dic):
    """
    Repack blockheader dic bit flag parameters into status and mode.
    """
    dic['status'] = dic['S_DATA'] * 1 + dic['S_SPEC'] * 2 + dic['S_32'
        ] * 4 + dic['S_FLOAT'] * 8 + dic['S_COMPLEX'] * 16 + dic[
        'S_HYPERCOMPLEX'] * 32 + dic['MORE_BLOCKS'] * 128 + dic['NP_CMPLX'
        ] * 256 + dic['NF_CMPLX'] * 512 + dic['NI_CMPLX'] * 1024 + dic[
        'NI2_CMPLX'] * 2048
    dic['mode'] = dic['NP_PHMODE'] * 1 + dic['NP_AVMODE'] * 2 + dic[
        'NP_PWRMODE'] * 4 + dic['NF_PHMODE'] * 16 + dic['NF_AVMODE'
        ] * 32 + dic['NF_PWRMODE'] * 64 + dic['NI_PHMODE'] * 256 + dic[
        'NI_AVMODE'] * 512 + dic['NI_PWRMODE'] * 1024 + dic['NI2_PHMODE'
        ] * 4096 + dic['NI2_AVMODE'] * 8192 + dic['NI2_PWRMODE'] * 16384
    return dic


def scan_setup_py():
    """Validate the contents of setup.py against Versioneer's expectations."""
    found = set()
    setters = False
    errors = 0
    with open('setup.py', 'r') as f:
        for line in f.readlines():
            if 'import versioneer' in line:
                found.add('import')
            if 'versioneer.get_cmdclass()' in line:
                found.add('cmdclass')
            if 'versioneer.get_version()' in line:
                found.add('get_version')
            if 'versioneer.VCS' in line:
                setters = True
            if 'versioneer.versionfile_source' in line:
                setters = True
    if len(found) != 3:
        print('')
        print('Your setup.py appears to be missing some important items')
        print('(but I might be wrong). Please make sure it has something')
        print('roughly like the following:')
        print('')
        print(' import versioneer')
        print(' setup( version=versioneer.get_version(),')
        print('        cmdclass=versioneer.get_cmdclass(),  ...)')
        print('')
        errors += 1
    if setters:
        print("You should remove lines like 'versioneer.VCS = ' and")
        print("'versioneer.versionfile_source = ' . This configuration")
        print('now lives in setup.cfg, and should be removed from setup.py')
        print('')
        errors += 1
    return errors


def find_catalog_for_glassname(glass_library, glassname):
    """
    Search for the catalog containing a given glass.

    Note that this is not a perfect solution --- it is common for multiple catalogs to share glass
    names, and this function will only return the first one it finds.

    Parameters
    ----------
    glass_library : ZemaxGlassLibrary
        The glass library to search through.
    glassname : str
        The name of the glass to search for.

    Returns
    -------
    catalog : str
        The name of the catalog where the glass is found. If not found, then return None.
    """
    for catalog in glass_library:
        if glassname in glass_library[catalog]:
            return catalog
    return None


@classmethod
def generate_name(cls, arguments_dict):
    """
        Generate a name for the corresponding :py:class:`~user_tasks.models.UserTaskStatus` model instance.

        Should be implemented by each subclass to generate a meaningful name
        from the task parameters.  Defaults to the name of the task function.
        """
    return cls.__name__.split('.')[-1]


def cont_cat_split(df, max_card=20, dep_var=None):
    """Helper function that returns column names of cont and cat variables from given `df`."""
    cont_names, cat_names = [], []
    for label in df:
        if label == dep_var:
            continue
        if df[label].dtype == int and df[label].unique().shape[0
            ] > max_card or df[label].dtype == float:
            cont_names.append(label)
        else:
            cat_names.append(label)
    return cont_names, cat_names


def arm_data_formatting(arm_data, values_list, number_of_flights,
    flight_data_list):
    """Takes in arm data and formats it correctly"""
    arm_plot_data = None
    if arm_data is True:
        arm_plot_data = []
        arm_info = [['id', 'ev'], ['time', 'ev']]
        for data_set in range(number_of_flights):
            arm_plot_data_temp = []
            for index in arm_info:
                values_list_index = 0
                for values_list_data in values_list[data_set]:
                    if index[1] == values_list_data[0].lower():
                        index.append(str(values_list_index))
                        for column in values_list[data_set][int(
                            values_list_index)][1:]:
                            if column[0].lower() == index[0]:
                                arm_label = flight_data_list[data_set]
                                arm_plot_data_temp.append([arm_label, column])
                                break
                    values_list_index += 1
            arm_plot_data.append(arm_plot_data_temp)
    arm_data_list = []
    if arm_data is True:
        for data_set in range(number_of_flights):
            if len(arm_plot_data[data_set][1]) != 0:
                arm_data_list.append(True)
            else:
                arm_data_list.append(False)
                print('Arm data is not present for data set {}'.format(str(
                    flight_data_list[data_set])))
        if all(arm_data_list) is True:
            arm_data = True
        else:
            arm_data = False
            print(
                'Arm data is not present or has not been imported properly. Make sure "EV" is present in the Data_sources.txt file, then run autoflpy again to re-generate the data.'
                )
    return arm_data, arm_plot_data


def selective_len(str, max):
    """Return the length of str, considering only characters below max."""
    res = 0
    for c in str:
        if ord(c) < max:
            res += 1
    return res


@classmethod
def to_result_topic(cls, invocation_topic):
    """Takes an Action invocation MQTT topic and returns the related result topic."""
    topic_split = invocation_topic.split('/')
    servient_id, thing_name, action_name = topic_split[-5], topic_split[-2
        ], topic_split[-1]
    return '{}/action/result/{}/{}'.format(servient_id, thing_name, action_name
        )


@classmethod
def legacy_device_session(cls, val):
    """
        Create an instance of this class set to the ``legacy_device_session``
        tag with value ``val``.

        :param LegacyDeviceSessionLogInfo val:
        :rtype: LinkedDeviceLogInfo
        """
    return cls('legacy_device_session', val)


def _class_instance_from_name(class_name, *arg, **kwarg):
    """
    class_name is of the form modA.modB.modC.class module_path splits on "."
    and the import_path is then ['modA','modB','modC'] the __import__ call is
    really annoying but essentially it reads like:

    import class from modA.modB.modC

    - Then the module variable points to modC
    - Then you get the class from the module.

    """
    module_path = class_name.split('.')
    import_path = module_path[0:-1]
    module = __import__('.'.join(import_path), fromlist=[module_path[-1]])
    class_ = getattr(module, module_path[-1])
    instance = class_(*arg, **kwarg)
    return instance


def __repr__(A):
    """For `print` and `pprint`"""
    return A.to_str()


def _describe_attr_present(attr, die, section_offset):
    """ Some forms may simply mean that an attribute is present,
        without providing any value.
    """
    return '1'


def unmap_with_copy(indices, src_tokens, vocab):
    """
    Unmap a list of list of indices, by optionally copying from src_tokens.
    """
    result = []
    for ind, tokens in zip(indices, src_tokens):
        words = []
        for idx in ind:
            if idx >= 0:
                words.append(vocab.id2word[idx])
            else:
                idx = -idx - 1
                words.append(tokens[idx])
        result += [words]
    return result


def replace_u_start_day(day):
    """Find the earliest legitimate day."""
    day = day.lstrip('-')
    if day == 'uu' or day == '0u':
        return '01'
    if day == 'u0':
        return '10'
    return day.replace('u', '0')


def connect_to(tub, got_connection):
    """
        Attempt to establish and maintain a connection to the server.

        :param Tub tub: A Foolscap Tub from which the connection is to
            originate.

        :param got_connection: A one-argument callable which is called with a
            Foolscap ``RemoteReference`` when a connection is established.
            This may be called multiple times if the connection is lost and
            then re-established.

        :return foolscap.reconnector.Reconnector: An object which manages the
            connection and reconnection attempts.
        """


def nonzero_columns(H):
    """
    Counts the number of columns in H not identically zero.

    Parameters
    ----------
    H : :py:class:`~sympy.matrices.dense.MutableDenseMatrix`
        A sympy matrix.

    Returns
    -------
    nonzero_cols : int
        The number of columns of A not indentically zero.

    Examples
    ---------
    >>> from sympy import Matrix, diag
    >>> A = Matrix([[0, 2],
    ...             [0, 4]])
    >>> nonzero_columns(A)
    1
    >>> nonzero_columns(Matrix.eye(5))
    5
    >>> nonzero_columns(diag(0,1,0,3,5,0))
    3
    """
    m, n = H.shape
    nonzero_cols = 0
    for j in range(0, n):
        for i in range(0, m):
            if H[i, j] != 0:
                nonzero_cols += 1
                break
    return nonzero_cols


def cancel_steps(ClusterId=None, StepIds=None):
    """
    Cancels a pending step or steps in a running cluster. Available only in Amazon EMR versions 4.8.0 and later, excluding version 5.0.0. A maximum of 256 steps are allowed in each CancelSteps request. CancelSteps is idempotent but asynchronous; it does not guarantee a step will be canceled, even if the request is successfully submitted. You can only cancel steps that are in a PENDING state.
    See also: AWS API Documentation

    :example: response = client.cancel_steps(
        ClusterId='string',
        StepIds=[
            'string',
        ]
    )

    :type ClusterId: string
    :param ClusterId: The ClusterID for which specified steps will be canceled. Use RunJobFlow and ListClusters to get ClusterIDs.

    :type StepIds: list
    :param StepIds: The list of StepIDs to cancel. Use ListSteps to get steps and their states for the specified cluster.
            (string) --

    :rtype: dict
    :return: {
        'CancelStepsInfoList': [
            {
                'StepId': 'string',
                'Status': 'SUBMITTED'|'FAILED',
                'Reason': 'string'
            },
        ]
    }

    """
    pass


def modify_snapshot_attribute(DryRun=None, SnapshotId=None, Attribute=None,
    OperationType=None, UserIds=None, GroupNames=None,
    CreateVolumePermission=None):
    """
    Adds or removes permission settings for the specified snapshot. You may add or remove specified AWS account IDs from a snapshot's list of create volume permissions, but you cannot do both in a single API call. If you need to both add and remove account IDs for a snapshot, you must use multiple API calls.
    For more information on modifying snapshot permissions, see Sharing Snapshots in the Amazon Elastic Compute Cloud User Guide .
    See also: AWS API Documentation

    Examples
    This example modifies snapshot snap-1234567890abcdef0 to remove the create volume permission for a user with the account ID 123456789012. If the command succeeds, no output is returned.
    Expected Output:
    This example makes the snapshot snap-1234567890abcdef0 public.
    Expected Output:

    :example: response = client.modify_snapshot_attribute(
        DryRun=True|False,
        SnapshotId='string',
        Attribute='productCodes'|'createVolumePermission',
        OperationType='add'|'remove',
        UserIds=[
            'string',
        ],
        GroupNames=[
            'string',
        ],
        CreateVolumePermission={
            'Add': [
                {
                    'UserId': 'string',
                    'Group': 'all'
                },
            ],
            'Remove': [
                {
                    'UserId': 'string',
                    'Group': 'all'
                },
            ]
        }
    )

    :type DryRun: boolean
    :param DryRun: Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation . Otherwise, it is UnauthorizedOperation .

    :type SnapshotId: string
    :param SnapshotId: [REQUIRED]
            The ID of the snapshot.

    :type Attribute: string
    :param Attribute: The snapshot attribute to modify.
            Note
            Only volume creation permissions may be modified at the customer level.

    :type OperationType: string
    :param OperationType: The type of operation to perform to the attribute.

    :type UserIds: list
    :param UserIds: The account ID to modify for the snapshot.
            (string) --

    :type GroupNames: list
    :param GroupNames: The group to modify for the snapshot.
            (string) --

    :type CreateVolumePermission: dict
    :param CreateVolumePermission: A JSON representation of the snapshot attribute modification.
            Add (list) --Adds a specific AWS account ID or group to a volume's list of create volume permissions.
            (dict) --Describes the user or group to be added or removed from the permissions for a volume.
            UserId (string) --The specific AWS account ID that is to be added or removed from a volume's list of create volume permissions.
            Group (string) --The specific group that is to be added or removed from a volume's list of create volume permissions.

            Remove (list) --Removes a specific AWS account ID or group from a volume's list of create volume permissions.
            (dict) --Describes the user or group to be added or removed from the permissions for a volume.
            UserId (string) --The specific AWS account ID that is to be added or removed from a volume's list of create volume permissions.
            Group (string) --The specific group that is to be added or removed from a volume's list of create volume permissions.

    :return: response = client.modify_snapshot_attribute(
        Attribute='createVolumePermission',
        OperationType='remove',
        SnapshotId='snap-1234567890abcdef0',
        UserIds=[
            '123456789012',
        ],
    )

    print(response)

    """
    pass


def weight_set():
    """Fill the active vertex group with the current paint weight

    """
    pass


def render_git_describe_long(pieces):
    """TAG-DISTANCE-gHEX[-dirty].

    Like 'git describe --tags --dirty --always -long'.
    The distance/hash is unconditional.

    Exceptions:
    1: no tags. HEX[-dirty]  (note: no 'g' prefix)
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        rendered += '-%d-g%s' % (pieces['distance'], pieces['short'])
    else:
        rendered = pieces['short']
    if pieces['dirty']:
        rendered += '-dirty'
    return rendered


def pot_for_component(pot, q, component=1, reverse=False):
    """
    Handle converting the potential into the star's frame of reference.

    Note: `q` should already be in the frame of the requested star.  If necessary,
    pass the output from <phoebe.distortions.roche.q_for_component>.

    If `component` is 1:
        * `pot` is returned as is

    If `component` is 2:
        * if `reverse` is True: `pot/q + 0.5 * (q-1)/q`
        * if `reverse` is False: `q*pot - 0.5 * (q-1)`

    Arguments
    ------------
    * `pot` (float): the equipotential of the star as defined as if it were the
        primary component.
    * `q` (float): the mass-ratio in the frame of `component`.  If necessary,
        call <phoebe.distortions.roche.q_for_component> first.
    * `component` (int, optional, default=1): whether the requested component
        is the primary (1) or secondary (2) component.
    * `reverse` (bool, optional, default=False): whether to do the reverse operation.
        If True, then `pot` is assumed to be in the reference frame of `component`
        and is returned as if in the reference from of the primary component.

    Returns
    ----------
    * (float): the converted equipotential.

    Raises
    ------------
    * NotImplementedError: if `component` is neither 1 or 2.)
    """
    if component == 1:
        return pot
    elif component == 2:
        if reverse:
            return pot / q + 0.5 * (q - 1) / q
        else:
            return q * pot - 0.5 * (q - 1)
    else:
        raise NotImplementedError


def _n2_setup_parser(parser):
    """
    Set up the openmdao subparser for the 'openmdao n2' command.

    Parameters
    ----------
    parser : argparse subparser
        The parser we're adding options to.
    """
    parser.add_argument('file', nargs=1, help=
        'Python script or recording containing the model.')
    parser.add_argument('-o', default='n2.html', action='store', dest=
        'outfile', help='html output file.')
    parser.add_argument('--no_browser', action='store_true', dest=
        'no_browser', help="don't display in a browser.")
    parser.add_argument('--embed', action='store_true', dest='embeddable',
        help='create embeddable version.')
    parser.add_argument('--title', default=None, action='store', dest=
        'title', help='diagram title.')
    parser.add_argument('--use_declare_partial_info', action='store_true',
        dest='use_declare_partial_info', help=
        'use declare partial info for internal connectivity.')


def duplicate_move(MBALL_OT_duplicate_metaelems=None,
    TRANSFORM_OT_translate=None):
    """Make copies of the selected metaelements and move them

    :param MBALL_OT_duplicate_metaelems: Duplicate Metaelements, Duplicate selected metaelement(s)
    :param TRANSFORM_OT_translate: Translate, Translate (move) selected items
    """
    pass


def is_hidden(resource, route_doc=None):
    """
    Determine whether a Resource has been hidden from Swagger documentation
    i.e. by using Api.doc(False) decorator
    """
    if route_doc is False:
        return True
    else:
        return hasattr(resource, '__apidoc__') and resource.__apidoc__ is False


def place(cls, inst):
    """Place a circuit instance in this definition"""
    cls._context_.placer.place(inst)


def _center(txt, width):
    """
    Return a string of length `width` with the string `txt` centered

    >>> _center('abc', 14)
    '     abc      '
    """
    left = ' ' * int((width - len(txt)) / 2)
    right = ' ' * (width - len(txt) - len(left))
    return '%s%s%s' % (left, txt, right)


def filter_intron(df_in, min_size):
    """Filter intron regions to remove those smaller than min_size."""
    df_out = df_in[df_in.end - df_in.start >= min_size].copy()
    return df_out


def decode_edges(edges):
    """
    Decodes the specified integer representing edges in Cortex graph. Returns
    a tuple (forward, reverse) which contain the list of nucleotides that we
    append to a kmer and its reverse complement to obtain other kmers in the
    Cortex graph.
    """
    bases = ['A', 'C', 'G', 'T']
    fwd = []
    for j in range(4):
        if 1 << j & edges != 0:
            fwd.append(bases[j])
    rev = []
    bases.reverse()
    for j in range(4):
        if 1 << j + 4 & edges != 0:
            rev.append(bases[j])
    return fwd, rev


@classmethod
def fromflat(cls, flat):
    """
        Create an instance of this from new Bernoulli input

        :param flat: ([float]) the Bernoulli input data
        :return: (ProbabilityDistribution) the instance from the given Bernoulli input data
        """
    return cls(flat)


def __get_num_parent_instantiations__(parents, num_values):
    """
    Gets the number of parent instantiations.

    :param parents: List of parent indices.
    :param num_values: List of the number of values per node.
    :return: Number of parent instantiations.
    """
    num_pa_instantiations = 1
    for pa in parents:
        num_pa_values = num_values[pa]
        num_pa_instantiations *= num_pa_values
    return num_pa_instantiations


@classmethod
def from_bytes(cls, byte_seq, num_items, mode=None, endianness=None):
    """Create a :py:class:`SFFVertices` object using a bytes object

        :param byte_seq: the data as a base64-encoded sequence; can be bytes or unicode
        :type byte_seq: bytes or unicode
        :param int num_vertices: the number of vertices contained (for validation)
        :param mode: the size of each voxel; valid values are: `int8`, `uint8`, `int16`, `uint16`, `int32`, `uint32`, `int64`, `uint64``float32`, `float64`
        :type mode: bytes or str or unicode
        :param endianness: byte ordering: ``little`` (default) or ``big``
        :type endianness: bytes or str or unicode
        :return: a :py:class:`SFFVertices` object
        :rtype: :py:class:`SFFVertices`
        """
    if mode is None:
        mode = cls.default_mode
    if endianness is None:
        endianness = cls.default_endianness
    kwargs = {cls.num_items_kwarg: num_items, 'mode': mode, 'endianness':
        endianness, 'data': byte_seq}
    obj = cls(**kwargs)
    return obj


def get_managed_field_names(fieldmodule):
    """
    Get names of managed fields in fieldmodule.
    """
    field_names = []
    field_iter = fieldmodule.createFielditerator()
    field = field_iter.next()
    while field.isValid():
        if field.isManaged():
            field_names.append(field.getName())
        field = field_iter.next()
    return field_names


def get_commands_config(obj, refresh=False):
    """Experimental function. Not part of the official API"""
    return obj._helper.get_cmd_cache(refresh=refresh)


def size_to_kb_mb_string(data_size: int, as_additional_info: bool=False) ->str:
    """Returns human-readable string with kilobytes or megabytes depending on the data_size range.

		:param data_size: data size in bytes to convert
		:param as_additional_info:
		if True, the dynamic data appear in round bracket after the number in bytes. e.g. '12345678 bytes (11.7 MB)'
		if False, only the dynamic data is returned e.g. '11.7 MB' """
    if data_size < 1024:
        as_additional_info = False
        dynamic = f'{data_size} bytes'
    elif data_size < 1048576:
        dynamic = f'{data_size / 1024:0.1f} kB'
    else:
        dynamic = f'{data_size / 1048576:0.1f} MB'
    if as_additional_info:
        return f'{data_size} bytes ({dynamic})'
    else:
        return dynamic


@classmethod
def calculate_amr(cls, is_extended, from_id, to_id, rtr_only=False, rtr_too
    =True):
    """
        Calculates AMR using CAN-ID range as parameter.

        :param bool is_extended: If True parameters from_id and to_id contains 29-bit CAN-ID.
        :param int from_id: First CAN-ID which should be received.
        :param int to_id: Last CAN-ID which should be received.
        :param bool rtr_only: If True only RTR-Messages should be received, and rtr_too will be ignored.
        :param bool rtr_too: If True CAN data frames and RTR-Messages should be received.
        :return: Value for AMR.
        :rtype: int
        """
    return (from_id ^ to_id) << 3 | (7 if rtr_too and not rtr_only else 3
        ) if is_extended else (from_id ^ to_id) << 21 | (2097151 if rtr_too and
        not rtr_only else 1048575)


def get_key(iterable, key):
    """
    Get elements with key from list of dicts

    Parameters
    ----------
    iterable : iterable
        iterable with dicts
    key : Hashable
        key to extract elements
    Returns
    -------
    iterable
        iterable of values
    """
    return list(map(lambda x: x[key], iterable))


def format_obsid_as_calendar_date(obsid):
    """Format an obsid as a UTC calendar date, returning the date as a string.

    This makes only sense because our obsids are actually times, expressed as
    integer GPS seconds. The return value will look like `2016-07-24`. This,
    of course, truncates all time-of-day information contained in the JD. Note
    that we convert from the GPS to the UTC timescale, requiring an accurate
    leap-second table.

    """
    from astropy.time import Time
    t = Time(obsid, format='gps')
    return t.utc.iso[:10]


@classmethod
def load(cls, buffer_or_path, check_required=True, check_length=True,
    idd_or_version=None):
    """
        Load Epm from a file.

        Parameters
        ----------
        buffer_or_path: str or typing.StringIO
            idf buffer or path
        check_required: bool
            If True (default), will raise an exception if a required field is missing.
            If False, do not perform any checks.
        check_length: bool
            If True (default), will raise an exception if a field has a bigger length than authorized.
            If False, will not check.

        Other Parameters
        ----------------
        idd_or_version: tuple or Idd
            If you want to use a specific idd, you can require a specific version (x.x.x),
            or directly provide an IDD object.

        Returns
        -------
        Epm
        """
    return cls().from_idf(buffer_or_path, check_required=check_required,
        check_length=check_length, idd_or_version=idd_or_version)


def phdiff2fmap(in_file, delta_te, newpath=None):
    """
    Convert the input phase-difference map into a fieldmap in Hz.

    Uses eq. (1) of [Hutton2002]_:

    .. math::

        \\Delta B_0 (\\text{T}^{-1}) = \\frac{\\Delta \\Theta}{2\\pi\\gamma \\Delta\\text{TE}}

    In this case, we do not take into account the gyromagnetic ratio of the
    proton (:math:`\\gamma`), since it will be applied inside TOPUP:

    .. math::

        \\Delta B_0 (\\text{Hz}) = \\frac{\\Delta \\Theta}{2\\pi \\Delta\\text{TE}}

    References
    ----------
    .. [Hutton2002] Hutton et al., Image Distortion Correction in fMRI: A Quantitative
      Evaluation, NeuroImage 16(1):217-240, 2002. doi:`10.1006/nimg.2001.1054
      <https://doi.org/10.1006/nimg.2001.1054>`_.

    """
    import math
    import numpy as np
    import nibabel as nb
    from nipype.utils.filemanip import fname_presuffix
    out_file = fname_presuffix(in_file, suffix='_fmap', newpath=newpath)
    image = nb.load(in_file)
    data = image.get_fdata(dtype='float32') / (2.0 * math.pi * delta_te)
    nii = nb.Nifti1Image(data, image.affine, image.header)
    nii.set_data_dtype(np.float32)
    nii.to_filename(out_file)
    return out_file


def info(info, quote=True, style=True):
    """
    Returns the given string ``info`` as a set of echo arguments.

    Optionally provides quoting and terminal styling.

    :param string info: Info string to add to script
    :param bool quote: By default quote given sting in single quotes
    :param bool style: By default add
    :returns: A flat list of strings
    """
    info = 'pyqubes|{0}'.format(info)
    if style:
        info = '\\e[36m{0}\\e[39m'.format(info)
    if quote:
        info = "'{0}'".format(info)
    return ['echo', '-e', info]


def branchmap():
    """Return a mapping of branch to heads in that branch."""


def unlist(given_list):
    """Convert the (possibly) single item list into a single item"""
    list_size = len(given_list)
    if list_size == 0:
        return None
    elif list_size == 1:
        return given_list[0]
    else:
        raise ValueError(list_size)


@staticmethod
def next_left(direction: int) ->int:
    """
        Returns the next right direction
        :param direction: the specified direction
        :return: the right direction
        """
    return (direction + 5) % 4


def cutnorm_sets(uis, vjs):
    """
    Generates the cutnorm sets from the rounded SDP solutions

    Args:
        uis: ndarray, (n+1, ) shaped array of rounded +- 1 solution
        vis: ndarray, (n+1, ) shaped array of rounded +- 1 solution
    Returns:
        (S, T) Reconstructed S and T sets that are {1, 0}^n

        S: Cutnorm set axis = 0

        T: Cutnorm set axis = 1
    """
    S = -1 * uis[-1] * uis[:-1]
    T = -1 * vjs[-1] * vjs[:-1]
    S = (S + 1) / 2
    T = (T + 1) / 2
    return S, T


@staticmethod
def _negotiate(client_value, server_value):
    """Return the negotiated value between what the client has requested
        and the server has requested for how the two will communicate.

        :param int client_value:
        :param int server_value:
        :return: int

        """
    return min(client_value, server_value) or (client_value or server_value)


def render_git_describe(pieces):
    """TAG[-DISTANCE-gHEX][-dirty].

    Like 'git describe --tags --dirty --always'.

    Exceptions:
    1: no tags. HEX[-dirty]  (note: no 'g' prefix)
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance']:
            rendered += '-%d-g%s' % (pieces['distance'], pieces['short'])
    else:
        rendered = pieces['short']
    if pieces['dirty']:
        rendered += '-dirty'
    return rendered


@classmethod
def delete_states(cls, pattern: str):
    """
        Delete the states from redis that matches the pattern
        :param pattern: pattern
        :return:
        """
    r = cls().get_redis()
    if not r:
        raise Exception('redis connection not established')
    redis_state_keys = r.keys(pattern)
    for key in redis_state_keys:
        r.delete(key)


@staticmethod
def read_context(context_file):
    """
        Read the context file
        :param context_file: Absolute path of the file
        :type context_file: string
        :return: Contents of the file
        :rtype: string
        """
    with open(context_file, 'r') as reader:
        return reader.read()


def remove_many(original, many):
    """Remove a list of items from the original list."""
    for one in many:
        if one in original:
            original.remove(one)


def format_validator(data):
    """
    accept only json and yaml
    """
    try:
        if data == 'json' or data == 'text':
            return data
        else:
            raise ValueError
    except ValueError:
        return 'Bad timeout value'


def noop():
    """Empty function to invoke CPython ceval loop."""
    return


def dtype(element):
    """
        Return the dtype of an array or the type of the element.
    """
    if hasattr(element, 'dtype'):
        return element.dtype
    else:
        return type(element)


@classmethod
def similarity(cls, node, target, include_substituents=True,
    include_modifications=True, include_children=False, exact=True,
    ignore_reduction=False, ignore_ring=False, treat_null_as_wild=True,
    match_attachement_positions=False, short_circuit_after=None, visited=None):
    """A heuristic comparison for measuring similarity between monosaccharides.

        Compares:
            1. ring_start and ring_end
            2. superclass
            3. configuration
            4. stem
            5. anomer
            6. If `include_modifications`, each modification
            7. If `include_substituents`, each substituent
            8. If `include_children`, each child |Monosaccharide|

        The result is two numbers, the observed similarity between `node` and `target`,
        and the similarity between `target` and itself. `expected - observed` is there
        number of differences observed between the two monosaccharides, which can be useful
        for expressing how far apart two monosaccharides are in feature space. For more distant
        similarity testing, especially when considering children, the ratio `observed / expected`
        might be used instead.

        Similarity is not symmetric, e.g. ``a -> b != b -> a``. A commutative version of similarity
        can be used by calculating both directions, and taking the result with the smallest error.

        Parameters
        ----------
        node : :class:`~.Monosaccharide`
            The reference monosaccharide
        target : :class:`~.Monosaccharide`
            The monosaccharide to compare against
        include_substituents: bool
            Include substituents in comparison (Defaults |True|)
        include_modifications: bool
            Include modifications in comparison (Defaults |True|)
        include_children: bool
            Include children in comparison (Defaults |False|)
        exact: bool
            Penalize for having unmatched attachments (Defaults |True|)
        ignore_reduction: bool
            Whether or not to include differences in reduction state as a
            mismatch
        ignore_ring: bool
            Whether or not to include differences in ring coordinates as
            a mismatch
        treat_null_as_wild: bool
            Whether or not to treat traits with a value of :const:`None` or
            :const:`~.UnknownPosition` as always matching when the null
            value is on the *target* residue (the residue that traits are being
            matched to).
        short_circuit_after: None or Number
            Controls whether to quit comparing nodes if the difference
            becomes too large, useful for speeding up pessimistic
            comparisons
        visited: set
            Tracks which node pairs have already been compared to break
            cycles. This carries state across multiple calls to :meth:`compare`
            and must be reset by calling :meth:`reset` before reusing an
            instance on new structures.

        Returns
        -------
        :class:`int`: observed
            The number of observed features that matched
        :class:`int`: expected
            The number of features that could have been matched
        """
    inst = cls(include_substituents=include_substituents,
        include_modifications=include_modifications, include_children=
        include_children, exact=exact, ignore_reduction=ignore_reduction,
        ignore_ring=ignore_ring, treat_null_as_wild=treat_null_as_wild,
        match_attachement_positions=match_attachement_positions,
        short_circuit_after=short_circuit_after, visited=visited)
    return inst.compare(node, target)


def set_testmode(testing):
    """Sets the package testing mode.
    """
    global testmode
    testmode = testing


def _(translate):
    """Identity function to mark strings for translation."""
    return translate


def get_weight_by_name(module_weights, name):
    """
    Retrieve parameter weight values from result of Module.get_weights()
    :param module_weights: all weights of a module, returned by Module.get_weights()
    :param name:
    :return:
    """
    for w, w_name in module_weights:
        if name == w_name:
            return w
    return None


def reject_remaining_kwargs(name, kwargs):
    """
    Helper function to emulate Python 3 keyword-only arguments.

    Use as::

        def func(x1, **kwargs):
            a = kwargs.pop('a', 1)
            b = kwargs.pop('b', 2)
            reject_remaining_kwargs('func', kwargs)
            ...

    To emulate the Python 3 syntax::

        def func(x1, *, a=1, b=2):
            ...
    """
    if kwargs:
        bad_arg = next(iter(kwargs))
        raise TypeError('{}() got an unexpected keyword argument {!r}'.
            format(name, bad_arg))


def sort_matrix(matrix, col=0):
    """Sort a 2D matrix by a specific column.

    Args:
        matrix (ndarray): 2-column matrix
        col (int): column to sort by

    Returns:
        2-column matrix sorted by the given column

    """
    idx = matrix[:, (col)].argsort()
    return matrix[idx]


def _nonpolarized_magnetic(R):
    """Convert magnetic reflectivity to unpolarized representation.

    Unpolarized magnetic data adds the cross-sections of the magnetic
    data incoherently and divides by two.
    """
    return sum(R) / 2


def finalize_client(client):
    """Stops and closes a client, even if it wasn't started."""
    client.stop()
    client.close()


@classmethod
def get_pseudopotential_type(cls, calc, **kwargs):
    """Return a list of pseudopotential types.

        List MUST be sorted by atom types.
        """
    types = []
    raw_types = cls._get_raw_pseudopotential_type(calc, **kwargs)
    for element in sorted(raw_types):
        if raw_types[element] is None or raw_types[element
            ] in cls._pseudopotential_types:
            types.append(raw_types[element])
        else:
            types.append('other')
    return types


def cutout_value_fn_constant(image, value):
    """
    Replace all image as a constant value
    """
    image[:] = value


def _net_debt(column):
    """
    Uses balance sheet to get company net debt
    """
    long_term_debt = column['Long-term debt']
    short_long_debt = column['Short-term debt']
    cash = column['Cash and cash equivalents']
    short_investments = column['Short-term investments']
    return long_term_debt + short_long_debt - cash - short_investments


def generate_sku(apps, schema_editor):
    """Simply use the pk as the SKU."""
    Product = apps.get_model('products', 'Product')
    for product in Product.objects.all():
        product.sku = f'{product.pk}'
        product.save()


def _encodedir(path):
    """
    >>> _encodedir(b'data/foo.i')
    'data/foo.i'
    >>> _encodedir(b'data/foo.i/bla.i')
    'data/foo.i.hg/bla.i'
    >>> _encodedir(b'data/foo.i.hg/bla.i')
    'data/foo.i.hg.hg/bla.i'
    >>> _encodedir(b'data/foo.i\\ndata/foo.i/bla.i\\ndata/foo.i.hg/bla.i\\n')
    'data/foo.i\\ndata/foo.i.hg/bla.i\\ndata/foo.i.hg.hg/bla.i\\n'
    """
    return path.replace(b'.hg/', b'.hg.hg/').replace(b'.i/', b'.i.hg/'
        ).replace(b'.d/', b'.d.hg/')


def disp_search_help():
    """ Display searchbar usage. """
    from x84.bbs import getterminal, echo
    term = getterminal()
    enter = term.yellow(u'Enter U.S.')
    postal = term.bold_yellow(u'postal code')
    or_nearest = term.yellow(u', or nearest')
    int_city = term.bold_yellow(u'international citY.')
    keyhelp = (
        u'{t.bold_yellow}({t.normal}{t.underline_yellow}Escape{t.normal}{t.bold_white}:{t.normal}{t.yellow}exit{t.normal}{t.bold_yellow}){t.normal}'
        .format(t=term))
    echo(u'\r\n\r\n' + term.normal)
    echo(u''.join(term.wrap(
        u'{enter} {postal}{or_nearest} {int_city} {keyhelp}'.format(enter=
        enter, postal=postal, or_nearest=or_nearest, int_city=int_city,
        keyhelp=keyhelp), term.width)))


def genome_info(genome, info):
    """
    return genome info for choosing representative

    if ggKbase table provided - choose rep based on SCGs and genome length
        - priority for most SCGs - extra SCGs, then largest genome

    otherwise, based on largest genome
    """
    try:
        scg = info['#SCGs']
        dups = info['#SCG duplicates']
        length = info['genome size (bp)']
        return [scg - dups, length, genome]
    except:
        return [False, False, info['genome size (bp)'], genome]


def get_model_from_sql():
    """
    该函数将存储在数据库中的决策树模型提取出来
    """
    import pickle
    with open('../MYSQL/model.pkl', 'rb') as f:
        model = pickle.load(f)
    return model


@classmethod
def voice(cls):
    """A factory method that creates a :class:`Permissions` with all
        "Voice" permissions from the official Discord UI set to True."""
    return cls(66060288)


def page(request):
    """
    Adds the current page to the template context and runs its
    ``set_helper`` method. This was previously part of
    ``PageMiddleware``, but moved to a context processor so that
    we could assign these template context variables without
    the middleware depending on Django's ``TemplateResponse``.
    """
    context = {}
    page = getattr(request, 'page', None)
    if page:
        context = {'request': request, 'page': page, '_current_page': page}
        page.set_helpers(context)
    return context


def add_tags(Tags=None, ResourceId=None, ResourceType=None):
    """
    Adds one or more tags to an object, up to a limit of 10. Each tag consists of a key and an optional value. If you add a tag using a key that is already associated with the ML object, AddTags updates the tag's value.
    See also: AWS API Documentation

    :example: response = client.add_tags(
        Tags=[
            {
                'Key': 'string',
                'Value': 'string'
            },
        ],
        ResourceId='string',
        ResourceType='BatchPrediction'|'DataSource'|'Evaluation'|'MLModel'
    )

    :type Tags: list
    :param Tags: [REQUIRED]
            The key-value pairs to use to create tags. If you specify a key without specifying a value, Amazon ML creates a tag with the specified key and a value of null.
            (dict) --A custom key-value pair associated with an ML object, such as an ML model.
            Key (string) --A unique identifier for the tag. Valid characters include Unicode letters, digits, white space, _, ., /, =, +, -, %, and @.
            Value (string) --An optional string, typically used to describe or define the tag. Valid characters include Unicode letters, digits, white space, _, ., /, =, +, -, %, and @.

    :type ResourceId: string
    :param ResourceId: [REQUIRED]
            The ID of the ML object to tag. For example, exampleModelId .

    :type ResourceType: string
    :param ResourceType: [REQUIRED]
            The type of the ML object to tag.

    :rtype: dict
    :return: {
        'ResourceId': 'string',
        'ResourceType': 'BatchPrediction'|'DataSource'|'Evaluation'|'MLModel'
    }

    """
    pass


def extract_wav(filename):
    """
    Extracts audio from video into a .wav file via ffmpeg.

    Parameters
    ----------
    - filename : str

        Path to the video file from which the audio track shall be extracted.

    Outputs
    -------
    - `filename`.wav

    Returns
    -------
    - str

        The path to the output audio file.
    """
    import os
    of = os.path.splitext(filename)[0]
    fex = os.path.splitext(filename)[1]
    cmds = ' '.join(['ffmpeg', '-i', filename, '-acodec', 'pcm_s16le', of +
        '.wav'])
    os.system(cmds)
    return of + '.wav'


def reorder_coefficients(occupations, coefficients):
    """
    Reorder the coefficients according to occupations. Occupated orbitals will be grouped at the beginning
    non occupied will be attached at the end

    :param occupations: list on integers (0 or 1) or list of Boolean
    :param coefficients:  dictionary containing the molecular orbitals coefficients {'alpha': coeff, 'beta:' coeff}.
                          coeff should be a list of lists (Norb x NBas)
    :return:
    """
    alpha_coefficients = []
    non_occupied = []
    for occ, coeff in zip(occupations['alpha'], coefficients['alpha']):
        if occ:
            alpha_coefficients.append(coeff)
        else:
            non_occupied.append(coeff)
    alpha_coefficients += non_occupied
    if not 'beta' in coefficients:
        coefficients['beta'] = coefficients['alpha']
    beta_coefficients = []
    non_occupied = []
    for occ, coeff in zip(occupations['beta'], coefficients['beta']):
        if occ:
            beta_coefficients.append(coeff)
        else:
            non_occupied.append(coeff)
    beta_coefficients += non_occupied
    return {'alpha': alpha_coefficients, 'beta': beta_coefficients}


def get_children_count(cur, node):
    """
    Get amount of immediate children.

    :param node: Node
    :type node: Node or uuid4
    """
    sql = """
      SELECT
        COUNT(*)
      FROM
        nodes
      WHERE
        parent=%s;
    """
    cur.execute(sql, (str(node),))
    result = cur.fetchone()
    return result['count']


def get_sample_weight(samples, metric):
    """
    Positional Arguements:
        samples: Numpy Array. Required.
        metric: String. 'l1' or 'l2'. Required.
    """
    assert metric in ['l1', 'l2']
    if metric == 'l1':
        sample_weight = samples.apply(lambda y: 1 / y)
    elif metric == 'l2':
        sample_weight = samples.apply(lambda y: 1 / y ** 2)
    sample_weight = sample_weight / sample_weight.sum()
    sample_weight = sample_weight.get_values()
    return sample_weight


@classmethod
def make_request(cls, memory_location):
    """
        Generates a request for ReadMemoryByAddress

        :param memory_location: The address and the size of the memory block to read.
        :type memory_location: :ref:`MemoryLocation <MemoryLocation>`

        :raises ValueError: If parameters are out of range, missing or wrong type
        """
    from udsoncan import Request, MemoryLocation
    if not isinstance(memory_location, MemoryLocation):
        raise ValueError(
            'Given memory location must be an instance of MemoryLocation')
    request = Request(service=cls)
    request.data = b''
    request.data += memory_location.alfid.get_byte()
    request.data += memory_location.get_address_bytes()
    request.data += memory_location.get_memorysize_bytes()
    return request


def mean_irrad(arr):
    """Calc the annual irradiance at a site given an irradiance timeseries.

    Parameters
    ----------
    arr : np.ndarray | pd.Series
        Annual irradiance array in W/m2. Row dimension is time.

    Returns
    -------
    mean : float | np.ndarray
        Mean irradiance values in kWh/m2/day. Float if the input array is
        1D, 1darray if the input array is 2D (multi-site).
    """
    mean = arr.mean(axis=0) / 1000 * 24
    return mean


@staticmethod
def get_output_of_stage():
    """Result of processing input with current control params."""
    raise NotImplementedError


def _obtain_weights_CSVLogger_filenames(body_backbone_CNN, image_backbone_CNN):
    """Obtains the polished filenames for the weights and the CSVLogger of the model.

    # Arguments
        model_name: String to declare the name of the model

    # Returns
        Two strings that will serve as the filenames for the weights and the CSVLogger respectively.
    """
    prefix = 'trained_models/emotic_vad_'
    suffix = '_weights_tf_dim_ordering_tf_kernels.h5'
    weights_filename = prefix + body_backbone_CNN + suffix
    CSVLogger_filename = 'emotic_vad_' + body_backbone_CNN + '_training.csv'
    return weights_filename, CSVLogger_filename


def _pre_gen(it, first):
    """A generator that returns first first if it exists."""
    if first:
        yield first
    while it:
        try:
            yield next(it)
        except StopIteration:
            return


def get_relation_fields_from_model(model_class):
    """ Get related fields (m2m, FK, and reverse FK) """
    relation_fields = []
    all_fields_names = model_class._meta.get_all_field_names()
    for field_name in all_fields_names:
        field = model_class._meta.get_field_by_name(field_name)
        if field_name[-3:] == '_id' and field_name[:-3] in all_fields_names:
            continue
        if field[3] or not field[2] or hasattr(field[0], 'related'):
            field[0].field_name = field_name
            relation_fields += [field[0]]
    return relation_fields


def transaction_open():
    """open transaction scope"""
    return '    @transaction'


def diag_item_is_appl_appl4(item):
    """Returns if an item is APPL or APPL4

    :param item: item to look a
    :type item: :class:`SAPDiagItem`

    :return: whether the item is a APPL or APPL4 item
    :rtype: ``bool``
    """
    return item.item_type in [16, 18]


def freestyle_lineset_copy():
    """Copy the active line set to a buffer

    """
    pass


def scan_setup_py():
    """Validate the contents of setup.py against Versioneer's expectations."""
    found = set()
    setters = False
    errors = 0
    with open('setup.py', 'r') as f:
        for line in f.readlines():
            if 'import versioneer' in line:
                found.add('import')
            if 'versioneer.get_cmdclass()' in line:
                found.add('cmdclass')
            if 'versioneer.get_version()' in line:
                found.add('get_version')
            if 'versioneer.VCS' in line:
                setters = True
            if 'versioneer.versionfile_source' in line:
                setters = True
    if len(found) != 3:
        print('')
        print('Your setup.py appears to be missing some important items')
        print('(but I might be wrong). Please make sure it has something')
        print('roughly like the following:')
        print('')
        print(' import versioneer')
        print(' setup( version=versioneer.get_version(),')
        print('        cmdclass=versioneer.get_cmdclass(),  ...)')
        print('')
        errors += 1
    if setters:
        print("You should remove lines like 'versioneer.VCS = ' and")
        print("'versioneer.versionfile_source = ' . This configuration")
        print('now lives in setup.cfg, and should be removed from setup.py')
        print('')
        errors += 1
    return errors


def is_frozen(G):
    """Return True if graph is frozen.

        Parameters
        ----------
            G : graph
                A DyNetx graph

        See Also
        --------
        freeze
        """
    try:
        return G.frozen
    except AttributeError:
        return False


def rgb2cmyk(r, g, b):
    """one way to get cmyk from rgb"""
    c = 1 - r
    m = 1 - g
    y = 1 - b
    k = min(c, m, y)
    c = min(1, max(0, c - k))
    m = min(1, max(0, m - k))
    y = min(1, max(0, y - k))
    k = min(1, max(0, k))
    return c, m, y, k


def not_junk(line):
    """Return False on newlines and C-style comments."""
    return line[0] != '#' and line != '\n'


def _gcd(a, b):
    """Euclidean algorithm for calculating the GCD of two numbers a, b.
    This algoritm also works for real numbers:
    Find the greatest number h such that a and b are integer multiples of h.
    """
    while a > 1e-05:
        a, b = b % a, a
    return b


@classmethod
def get_package_root(cls, manifest_resource, codebase):
    """
        Return the Resource for the package root given a `manifest_resource`
        Resource object that represents a manifest in the `codebase` Codebase.

        Each package type and instance have different conventions on how a
        package manifest relates to the root of a package.

        For instance, given a "package.json" file, the root of an npm is the
        parent directory. The same applies with a Maven "pom.xml". In the case
        of a "xyz.pom" file found inside a JAR META-INF/ directory, the root is
        the JAR itself which may not be the direct parent

        Each package type should subclass as needed. This deafult to return the
        same path.
        """
    return manifest_resource


def calc_points_made(row):
    """
    function to calculate the points earned by a team with each shot made

    Inputs:
    row - row of pbp dataframe

    Outputs - value of shot made
    """
    if row['is_three'] == 1 and row['shot_made'] == 1:
        return 3
    elif row['is_three'] == 0 and row['shot_made'] == 1 and row['eventmsgtype'
        ] != 3:
        return 2
    elif row['eventmsgtype'] == 3 and row['shot_made'] == 1:
        return 1
    else:
        return 0


def _pop_extras_kwargs(kwargs):
    """
    Remove the keywords used in the extras section (if present), and return them.

    Returns:
        tuple made of (extras_kwargs, kwargs), where extras_kwargs are keywords for the extras and kwargs are the others that were provided.

    """
    extras_kwargs = {}
    if 'visualizations' in kwargs:
        extras_kwargs['visualizations'] = kwargs.pop('visualizations')
    return extras_kwargs