You could use a Queue as some kind of buffer, though, e.g.
import random
from Queue
import Queue, Full
q = Queue(100)
for i in range(10):
#fill queue
while True:
try:
q.put_nowait(random.randint(1, 10))
except Full:
break
#take some values from queue
print "Round", i,
number_of_values_to_get = random.randint(0, 20)
print "getting %i values." % number_of_values_to_get
for j in range(number_of_values_to_get):
value = q.get()
print " got value", value
For example:
import random
from collections
import deque
def test_queue(max_size = 10):
d = deque([], max_size)
for i in xrange(1, 2 * max_size):
r = random.randint(0, 100000)
d.append(r)
print 'i=%d, len=%d r=%d' % (i, len(d), r)
while d:
print d.popleft()
The above function will make sure that the deque
object you create will not exceed max_size
elements. If you add more elements to the deque
, the oldest elements will automatically be removed. Here is an example run of the above function:
i = 1, len = 1 r = 83870 i = 2, len = 2 r = 12432 i = 3, len = 3 r = 87084 i = 4, len = 4 r = 3485 i = 5, len = 5 r = 12237 i = 6, len = 6 r = 81401 i = 7, len = 7 r = 24990 i = 8, len = 8 r = 21391 i = 9, len = 9 r = 34153 i = 10, len = 10 r = 63651 i = 11, len = 10 r = 96305 i = 12, len = 10 r = 46671 i = 13, len = 10 r = 19288 i = 14, len = 10 r = 40170 i = 15, len = 10 r = 45399 i = 16, len = 10 r = 94032 i = 17, len = 10 r = 57749 i = 18, len = 10 r = 68440 i = 19, len = 10 r = 59726 63651 96305 46671 19288 40170 45399 94032 57749 68440 59726
Contrary to most data types exposed by the Python interpreter, buffers are not PyObject pointers but rather simple C structures. This allows them to be created and copied very simply. When a generic wrapper around a buffer is needed, a memoryview object can be created.,Buffers are usually obtained by sending a buffer request to an exporting object via PyObject_GetBuffer(). Since the complexity of the logical structure of the memory can vary drastically, the consumer uses the flags argument to specify the exact buffer type it can handle.,Buffer structures (or simply “buffers”) are useful as a way to expose the binary data from another object to the Python programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a block of memory, it is possible to expose any data to the Python programmer quite easily. The memory could be a large, constant array in a C extension, it could be a raw block of memory for manipulation before passing to an operating system library, or it could be used to pass around structured data in its native, in-memory format.,This is for use internally by the exporting object. For example, this might be re-cast as an integer by the exporter and used to store flags about whether or not the shape, strides, and suboffsets arrays must be freed when the buffer is released. The consumer MUST NOT alter this value.
ptr = (char * ) buf + indices[0] * strides[0] + ...+indices[n - 1] * strides[n - 1]; item = * ((typeof(item) * ) ptr);
def verify_structure(memlen, itemsize, ndim, shape, strides, offset): "" "Verify that the parameters represent a valid array within the bounds of the allocated memory: char * mem: start of the physical memory block memlen: length of the physical memory block offset: (char * ) buf - mem "" " if offset % itemsize: return False if offset < 0 or offset + itemsize > memlen: return False if any(v % itemsize for v in strides): return False if ndim <= 0: return ndim == 0 and not shape and not strides if 0 in shape: return True imin = sum(strides[j] * (shape[j] - 1) for j in range(ndim) if strides[j] <= 0) imax = sum(strides[j] * (shape[j] - 1) for j in range(ndim) if strides[j] > 0) return 0 <= offset + imin and offset + imax + itemsize <= memlen
void * get_item_pointer(int ndim, void * buf, Py_ssize_t * strides,
Py_ssize_t * suboffsets, Py_ssize_t * indices) {
char * pointer = (char * ) buf;
int i;
for (i = 0; i < ndim; i++) {
pointer += strides[i] * indices[i];
if (suboffsets[i] >= 0) {
pointer = * ((char ** ) pointer) + suboffsets[i];
}
}
return (void * ) pointer;
}
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 '*'., 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):, 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.
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…
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
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
You want to define a buffer with a fixed size, so that when it fills up, adding another element must overwrite the first (oldest) one. This kind of data structure is particularly useful for storing log and history information. ,A ring buffer is a buffer with a fixed size. When it fills up, adding another element overwrites the oldest one that was still being kept. It’s particularly useful for the storage of log information and history. There is no direct support in Python for this kind of structure, but it’s easy to construct one. The implementation in this recipe is optimized for element insertion. ,This recipe changes the buffer object’s class on the fly, from a non-full buffer class to a full-buffer class, when it fills up: ,O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.
class RingBuffer: "" " class that implements a not-yet-full buffer " "" def _ _init_ _(self, size_max): self.max = size_max self.data = [] class _ _Full: "" " class that implements a full buffer " "" def append(self, x): "" " Append an element overwriting the oldest one. " "" self.data[self.cur] = x self.cur = (self.cur + 1) % self.max def get(self): "" " return list of elements in correct order " "" return self.data[self.cur: ] + self.data[: self.cur] def append(self, x): "" "append an element at the end of the buffer" "" self.data.append(x) if len(self.data) == self.max: self.cur = 0 # Permanently change self 's class from non-full to full self._ _class_ _ = self._ _Full def get(self): "" " Return a list of elements from the oldest to the newest. " "" return self.data # sample usage if _ _name_ _ == '_ _main_ _': x = RingBuffer(5) x.append(1); x.append(2); x.append(3); x.append(4) print x._ _class_ _, x.get() x.append(5) print x._ _class_ _, x.get() x.append(6) print x.data, x.get() x.append(7); x.append(8); x.append(9); x.append(10) print x.data, x.get()
The simple example below just runs the Buffer tool, but it allows the user to enter the path of the input and output datasets as well as the distance of the buffer. The user-supplied parameters make their way into the script with the arcpy.GetParameterAsText() function.,Examine the script below carefully, but don't try to run it yet. You'll do that in the next part of the lesson.,Think about the previous example where you ran some map algebra on an elevation raster. If you wanted to change the value of your cutoff elevation to 2500 instead of 3500, you had to open the script itself and change the value of the cutoffElevation variable in the code.,The John A. Dutton e-Education Institute is the learning design unit of the College of Earth and Mineral Sciences at The Pennsylvania State University.
Examine the script below carefully, but don't try to run it yet. You'll do that in the next part of the lesson.
# This script runs the Buffer tool.The user supplies the input # and output paths, and the buffer distance. import arcpy arcpy.env.overwriteOutput = True try: # Get the input parameters for the Buffer tool inPath = arcpy.GetParameterAsText(0) outPath = arcpy.GetParameterAsText(1) bufferDistance = arcpy.GetParameterAsText(2) # Run the Buffer tool arcpy.Buffer_analysis(inPath, outPath, bufferDistance) # Report a success message arcpy.AddMessage("All done!") except: # Report an error messages arcpy.AddError("Could not complete the buffer") # Report any error messages that the Buffer tool might have generated arcpy.AddMessage(arcpy.GetMessages())