예제 #1
0
파일: pairs.py 프로젝트: xmnr/atk
def replacePairs(pairs, indexes, name=None, value=None):
    """
    Interates through the pairs, alterPairing those at the supplied indexes. A
    generator of (name, value) pairs is returned.

    If the name and/or value parameters are not None, the pair is updated with
    the new value. If both are None, the pair is deleted. name and value must
    be strings, and all indexes must be positive integers & not outside of pairs.
    Duplicates in indexes are ignored.
    """

    if name is not None and not isinstance(name, str):
        raise TypeError("name must be a string or None.")
    if value is not None and not isinstance(value, str):
        raise TypeError("value must be a string or None.")

    indexes = set(indexes)

    for i, p in zip(range(len(pairs)), pairs):
        if i in indexes:
            indexes.remove(i)

            if name is None and value is None: continue
            else:
                p = (
                    valueOrDefault(name, pairs[i][NAME_IDX]),
                    valueOrDefault(value, pairs[i][VALUE_IDX])
                )

        yield p

    if indexes:
        raise IndexError("Not all supplied indexes were valid.")
예제 #2
0
파일: messages.py 프로젝트: xmnr/atk
def request(method=None, path=None, headers=None, version=None, entity=None):

    return dict(
        method = valueOrDefault(method, "GET"),
        path = valueOrDefault(path, "/"),
        headers = valueOrDefault(headers, tuple()),
        version = valueOrDefault(version, "HTTP/1.0"),
        entity = valueOrDefault(entity, "")
    )
예제 #3
0
파일: messages.py 프로젝트: xmnr/atk
def response(status=None, reason=None, headers=None, version=None, entity=None):

    return dict(
        status = valueOrDefault(status, "200"),
        reason = valueOrDefault(reason, "OK"),
        headers = valueOrDefault(headers, (
            ("Content-length", "0"),
            ("Content-type", "text/plain", ), 
        )),
        version = valueOrDefault(version, "HTTP/1.0"),
        entity = valueOrDefault(entity, "")
    )