[Tutor] Questions come and questions go, but I will email forever

Jacob S. keridee at jayco.net
Thu Oct 28 04:41:51 CEST 2004


Okay,

Danny Yoo said...
>   You're writing an email: it doesn't have to be double spaced.  *grin*

I thought that "\r\n" is the escape sequence for a hard return. Maybe that's
only true for Windows?

> Hmmm.  I'm still slightly confused about this.  Can you show us what those
> two functions look like?  There might be a way of disentangling them so
> that they don't reference each other.  And if not, at least we'll be able
> to see why.

This is a killer, but I went gung ho into attacking the code and I don't
have an original copy of the code. However, I can give you a non-working
example.

## Beginning of non-working code

from visual import *
from time import localtime
from __future__ import division
import math
import string
import TimeConversion
import winsound
from visual.text import *
import sys
import os

day = time.strftime("%A, %B %d, %Y")  ## Displays the date that you see
first
print day
global scenegoon
global scene2goon
scenegoon = 0
scene2goon = 0

dirlist = os.listdir("c:\\")
if 'Jacob Laptop' in dirlist:
    variable = 594
elif 'Home Computer' in dirlist:
    variable = 370
elif 'Sissy Computer' in dirlist:
    variable = 365
elif 'Michael Laptop' in dirlist:
    variable = 594
elif 'Office Computer' in dirlist:
    variable = 365
elif 'School Auction Laptop' in dirlist:
    variable = 594
else:
    print 'Hey you need to put a folder in this computer!. '
    print '''Folders include:
    Jacob Laptop
    Home Computer
    Sissy Computer
    Michael Laptop
    Office Computer
    School Auction Laptop
    '''
    folder = raw_input('Which computer is this? ')
    folder = "C:\\"+folder
    os.mkdir(folder)
    ask = input('[365,594] Please give me one.')
    variable = ask

alarm = raw_input('What should the alarm be set to? ')
if alarm != '':
    alarm = TimeConversion.standtomil(alarm)
    alarm = string.split(alarm, ":")
    alarmhour = int(alarm[0])
    alarmmin = int(alarm[1])
else:
    alarmhour = ''
    alarmmin = ''

def makeanalog():
    scenegoon = 1
    ##     Default values     ##
    scene = display()

    scene.title = 'Analog Clock by Jacob\'s Clockworks, Inc.'
    scene.userspin = 1
    scene.userzoom = 1

    r = 5 # Radius of the clock

    handsradius = 0.05
    clockfacecolor = color.white
    clockframecolor = color.blue
    hhandcolor = color.black
    mhandcolor = color.black
    shandcolor = color.red
    fourthscolor = color.purple
    hourcolor = color.orange
    mincolor = color.green

    ############################

    scene.x = variable
    scene.exit=0
    scene.range=(r+2, r+2, r+2)
    scene.visible = 1

    clockface = cylinder(pos=(0,0,0), color=clockfacecolor, radius=r,
axis=(0,0,0.1))
    clockframe = ring(pos=(0,0,0),axis=clockface.axis, radius=r,
color=clockframecolor, thickness=0.3)

    noonmark =
ring(pos=(0,r-0.15,0),axis=(1,0,0),radius=0.3,thickness=0.3,color=fourthscol
or)
    threequartermark =
ring(pos=(-r+0.15,0,0),axis=(0,1,0),radius=0.3,thickness=0.3,color=fourthsco
lor)
    onequartermark =
ring(pos=(r-0.15,0,0),axis=(0,1,0),radius=0.3,thickness=0.3,color=fourthscol
or)
    halfmark =
ring(pos=(0,-r+0.15,0),axis=(1,0,0),radius=0.3,thickness=0.3,color=fourthsco
lor)

    list=[1,2,4,5,7,8,10,11]
    for n in list:
        mark =
sphere(pos=(r*sin(math.pi*n/6),r*cos(math.pi*n/6),0.3),color=hourcolor,radiu
s=0.25)

    list=range(60)
    for n in list:
        if n % 5 == 0:
            list.pop(list.index(n))
    for n in list:
        mark =
sphere(pos=(r*sin(math.pi*n/30),r*cos(math.pi*n/30),0.3),color=mincolor,radi
us=0.125)

    x=''
    y=''
    hhand=''
    mhand=''
    shand=''


def makedigital():
    scene2goon == 1
    ##     Default values     ##
    scene2 = display()
    scene2.select()
    scene2.title = 'Digital Clock by Jacob\'s Clockworks, Inc.'
    scene2.userspin = 1
    scene2.userzoom = 1

    clockbgcolor = color.lcd
    clockframecolor = color.blue
    digitcolor = color.black

    ############################

    clocklength = 10
    clockheight = 3
    scene2.x = variable
    scene2.y=430
    scene2.height = 200
    scene2.autoscale = 0
    scene2.exit=0
    scene2.range = [clocklength/2+1,clocklength/2+1,clocklength/2+1]
    scene2.visible = 1
    clockbg =
box(pos=(0,0,0),width=0.1,height=clockheight,length=clocklength,color=clockb
gcolor)
    clockframe =
box(pos=(0,0,-0.1),width=0.1,height=clockheight+0.25,length=clocklength+0.25
,color=clockframecolor)
    scenegoon = 1
    scene2goon = 1

def analogcode():
    t = time.localtime()
    hour = t[3]
    min = t[4]
    sec = t[5]
    if hour == alarmhour:
        if min == alarmmin:
            if sec < 15 and sec >= 0:
                winsound.Beep(1000,250)
    if hour > 12:
        hour = hour-12
    if hour == 0:
        hour = 12

    if scene.kb.keys != 0:
        key = scene.kb.getkey()
        if key == 'f12':
            scenegoon = 0
        if key == 'f11':
            clockface.color = input('Give me a tuple of the new face color.
')
        if key == 'q':
            scenegoon = 0
            scene2goon = 0
    try:
        del x
        del y
        hhand.visible=0
        del hhand
        mhand.visible=0
        del mhand
        shand.visible=0
        del shand
    except:
        pass
    scene.select()
    newmin = min+(sec/60)
    newhour = hour+(newmin/60)+(sec/3600)
    y = 0.5*r*sin((math.pi*newhour)/6)
    x = 0.5*r*cos((math.pi*newhour)/6)
    hhand = curve(color=hhandcolor,radius=handsradius)
    hhand.append(pos=(0,0,0.3))
    hhand.append(pos=(y,x,0.3))
    y = 0.75*r*sin((math.pi*newmin)/30)
    x = 0.75*r*cos((math.pi*newmin)/30)
    mhand = curve(color=mhandcolor,radius=handsradius)
    mhand.append(pos=(0,0,0.3))
    mhand.append(pos=(y,x,0.3))
    y = 0.75*r*sin((math.pi*sec)/30)
    x = 0.75*r*cos((math.pi*sec)/30)
    shand = curve(color=shandcolor,radius=handsradius)
    shand.append(pos=(0,0,0.3))
    shand.append(pos=(y,x,0.3))

def digitalcode():
    scene2.select()
    if scene2.kb.keys != 0:
        key = scene2.kb.getkey()
        if key == 'f12':
            scene2goon = 0
        if key == 'f11':
            clockbg.color = input('Give tuple for background color. ')
        if key == 'q':
            scenegoon = 0
            scene2goon = 0
    try:
        text.makeinvisible(hrmin)
        text.makeinvisible(secdigit)
        del hrdigit
        del mindigit
        del secdigit
    except:
        pass
    hour = str(hour)
    min = str(min)
    sec = str(sec)
    min = string.zfill(min,2)
    sec = string.zfill(sec,2)
    hrmin = string.join([hour,min],":")
    hrmin =
text(pos=(-0.9,-1,0.11),color=digitcolor,string=hrmin,justify='center',heigh
t=2,display=scene2)
    secdigit =
text(pos=(3.35,-1,0.11),color=digitcolor,string=sec,justify='center',height=
1,display=scene2)


while 1:
    rate(1)
    if scenegoon:
        analogcode()
    if scene2goon:
        digitalcode()
    if not scenegoon:
        try:
            scene.visible = 0
            del scene
        except:
            pass
    if not scene2goon:
        try:
            scene2.visible = 0
            del scene2
        except:
            pass
sys.exit()

if __name__ == "__main__":
    makeanalog()

## End of code

I guess I had better go into more detail. If you read the code closely
enough, you will notice that this program displays (or displayed) two
clocks, analog on top, digital on bottom. (It's pretty impressive for me)
Before I messed with the code, the while loop contained both updates for the
analog and the digital. I told myself that I didn't like the digital clock
as much, and didn't want it displayed when __name__ == "__main__":  So, in
order to split the code apart, I wanted to put the two clocks into seperate
functions. So, say, the variables for analog clock would be in
makeanalog():, whereas the variables for the digital clock would be in
makedigital(). Obviously, this would mean that: a) I would have to redefine
the shared variables b) I would have to put each shared variable in the call
of the function ex. makeanalog(clockfacecolor, handcolor, *args) or whatever
*args means -- it means etc. in my book. OR c) I define all of the future
variables global at the beginning of the script.
Now, having said that, I am lazy, and though I like to look through and edit
the code some of the time, this looked like a huge overhauling. I chose c.
because it seems the easiest way to make all of the variables available to
all of the functions.
Since I have started working on it, I believe that I might try using classes
instead. Even so, I am not stable and secure using classes, so any sort of
generic help that would explain the basics of classes, but thoroughly and to
the point. It would include things like the fact that classes have a root
class and whatever they do to help the class that you define.
Anyway, I would appreciate any help that you can give. I'm sorry if the
explaination is long, but I have seen alot of posts to the tutor that you
whizzes replied to with:

> Hmmm.  I'm still slightly confused about this.
or
"Can you give us anymore information?"

HYCH & HTH,
Jacob Schmidt



More information about the Tutor mailing list