Hey guys, first time poster, relatively long time reader -- I am struggling creating a buffer class in python for my class. (relatively new to the language).

Here is the basic idea I want to do:

Implement a class that buffers text. The write() method adds a string to the buffer. If the added data contains a '*', print the buffer contents to the console up until and including the '*'.

Example use:

buffer = Buffer()
buffer.write ('abcd') # prints nothing
buffer.write ('ef*gh') # prints abcdef*
buffer.flush () # prints gh

I get the basic idea, of how to call this stuff up, but I am definitely struggling getting in concatenating the strings within the script itself -- and having the program do what it requested.

Here is my script as it stands, with real and psuedo code. I know it's something really simple and I've run around this for probably twice as long as I should and I just need to move on. Any pushes or help in the right direction would be GREATLY appreciated! Thanks so much! (For whatever reason, the forum is not indenting the code properly, but its set up properly in my code, so no worries about that.)

class StringBuffer:
def write(self, input):
s = input
t = s
if '*' in input:
input += t
# input = input.'do not print anything that comes after the *, but retain it)
print input
else:
s += t
#just keep concatenating until we have a * call
def flush(self):

print input.#'print anything after the *'
del input

Use the code tag

so that your code can look like this:

class StringBuffer:
    def write(self, input):
        s = input
        t = s
        if '*' in input:
            input += t
            # input = input.'do not print anything that comes after the *, but retain it)
            print input
        else:
            s += t
            #just keep concatenating until we have a * call
    def flush(self):
        print input.#'print anything after the *'
        del input

also don't use input as a variable

class StringBuffer(object):
    def __init__(self):  #OnInit
        self._buffered = ""
    def write(self, text):
        self._buffered += text
        if "*" in self._buffered:
            iasterisk = self._buffered.rfind("*") + 1
            print self._buffered[0:iasterisk]
            self._buffered = self._buffered[iasterisk:]
    def flush(self):
        print self._buffered
        self._buffered = ""
    def __len__(self):  #This is a trick that I will add
        return len(self._buffered)

buffer = StringBuffer()  #a new StringBuffer is made
buffer.write("apple")
buffer.write("sauce*drum")  #prints applesauce
buffer.write("sticks")

print len(buffer)  #is 10

buffer.write("*leftovers")  #prints drumsticks
buffer.flush()  #prints leftovers

print len(buffer)  #after flush, is 0

try that.

Wow! That's great and a bit more than I expected. I'll definitely keep it around for study. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.