<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _write_base(buf, value, bits_per_octet, end_bit=0, sign_bit=0, is_signed=False):
 """Write a field to the provided buffer. Args: buf (Sequence):
 The buffer into which the UInt will be written in the form of integer octets. value (int):
 The value to write as a UInt. bits_per_octet (int):
 The number of value bits (i.e. exclusive of the end bit, but inclusive of the sign bit, if applicable) per octet. end_bit (Optional[int]):
 The end bit mask. sign_bit (Optional[int]):
 The sign bit mask. Returns: int: The number of octets written. """

    if value == 0:
        buf.append(sign_bit | end_bit)
        return 1
    num_bits = bit_length(value)
    num_octets = num_bits // bits_per_octet
    # 'remainder' is the number of value bits in the first octet.
    remainder = num_bits % bits_per_octet
    if remainder != 0 or is_signed:
        # If signed, the first octet has one fewer bit available, requiring another octet.
        num_octets += 1
    else:
        # This ensures that unsigned values that fit exactly are not shifted too far.
        remainder = bits_per_octet
    for i in range(num_octets):
        octet = 0
        if i == 0:
            octet |= sign_bit
        if i == num_octets - 1:
            octet |= end_bit
        # 'remainder' is used for alignment such that only the first octet
        # may contain insignificant zeros.
        octet |= ((value >> (num_bits - (remainder + bits_per_octet * i))) & _OCTET_MASKS[bits_per_octet])
        buf.append(octet)
    return num_octets