problem with a css cycler

Eric @ Zomething eric at zomething.com
Sat Mar 20 19:50:51 EST 2004


Adriano wrote:

> I am trying to build a css cycler in python, to change the css used in a 
> website every X number of days, using a list of files: the first X days 
> it'd show file1, then file2, then file3, then back to the first one.
> 
> The code I came up with is the following:
> 
> css = ["file1", "file2", "file3"]
> i = 0
> max_i = 3
> today = int(raw_input("today's date: "))
> stored_day = 1
> 
> if 3 <= (today - stored_day):
> 	stored_day = today
> 	i += 1
> 	if i > max_i:
> 		i = 0


Perhaps you would want to look up the day and compare to a pickled list object to determine which css file to use.

You might do something like this, which would cycle through the css files, one per day.  You could change the logic to cycle through the files in the manner you desire.

[sloppy code... sorry.  no functions nor classes, just snippets]

# cssSelect.py

css = ["file1.css", "file2.css", "file3.css"]
cssCount=len(css)
cssRepeats=int(366/cssCount)
cssFlags=[]
for n in range(366):
    cssFlags.append('')

for f in range(cssCount):      
    for d in range(f, 366,cssCount):  # this hops through the list, assigning a file index
        cssFlags[d]=f

for day in cssFlags:  #just to show the assignments
    print css[day]

# Pickle the list to a file
import pickle
fname=file('C:\\cssSequence','w')
savedFlags=pickle.Pickler(fname)
savedFlags.dump(cssFlags)
fname.close()

# Unpickle the item back to a list
fname=file('C:\\cssSequence','r')
getFlags=pickle.Unpickler(fname)
cssFlags=getFlags.load()
fname.close()

# use the Julian date to grab the right list element
import time
t=time.localtime()
dayIndex=t[7]-1
cssFile=cssFlags[dayIndex]


HTH,

Eric Pederson





More information about the Python-list mailing list