[Tutor] how best to implement paginated data in CLI
Rance Hall
ranceh at gmail.com
Tue Sep 14 22:11:50 CEST 2010
I'm using python 3.1 with py-postgresql (located at
http://python.projects.postgresql.org/
I need to work through how best to paginate larger sql result sets.
my SELECT statement returns a list, I can get its len() to find out
that X records were returned
What I want to do is print the first N records and include a
Previous/Next Choice at the bottom.
I know I'll be making some use of the start and stop optional
parameters in the for loop that processes the list.
I think that I'll need variables for the page size (#of records per
screen) and a start position for where to begin in the list.
I want to make this a function so that its reusable code
Here is a first draft of the idea: (untested)
def paginate_stuff(list,start)
pagesize = 10
for row in list[start:start+pagesize]
print(row["column"])
print ("\n")
message = ""
prompt = ""
if not start == 0:
message = message + "Previous"
prompt = prompt + "P"
if not start ==0 and not start+pagesize >= len(list):
message = message + " or "
prompt = prompt + " or "
if not start+pagesize >= len(list):
message = message + "Next"
prompt = prompt + "N"
print((message+"\n")
choice = input(prompt+"? ")
if choice == "n' or "N":
paginate_stuff(list,start + pagesize)
if choice == "p" or "P":
paginate_stuff(list,start - pagesize)
return
I'm not certain at this writing of the proper syntax of the if
statement conditions, I will look that up to be sure the syntax is
right.
What I want to ask about is the logic flow, does this make sense? Am
I thinking about the problem the right way? Is there a better way to
pull this off?
More information about the Tutor
mailing list