[Tutor] is there any Python code for spatial tessellation?

2005-10-08 Thread Shi Mu
is there any Python code for spatial tessellation?
thanks a lot!
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is there any Python code for spatial tessellation?

2005-10-09 Thread Shi Mu
There are four points with coordinates:
2,3;4,9;1,6;3,10.
How to use Python to draw one perpendicular bisector between (2,3) and (4,9);
the other perpendicular bisector between (1,6)和(3,10);
then, makes the output like:
l1 a b c
l2 a b c
(Note: l indicates the perpendicular bisector with equation ax + by = c.)
Plus the intersection coordinates of the two perpendicular bisectors:
x,y


On 10/8/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
>
>
> On Sat, 8 Oct 2005, Shi Mu wrote:
>
> > is there any Python code for spatial tessellation?
>
> Are you looking for code to generate voronoi diagrams?
>
>http://en.wikipedia.org/wiki/Voronoi_diagram
>
> From initial Google searches, it appears that there is a package called
> Qhull that pepole use to do Voronoi tesselations.
>
>http://www.qhull.org/
>
> and Chris Myers has written a module around Qhull:
>
>http://www.tc.cornell.edu/~myers/PyXL/
>
>
> Otherwise, I don't think we at Tutor can help you that much; you may want
> to ask on a more math-oriented forum.  Good luck to you!
>
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] line question

2005-10-09 Thread Shi Mu
There are four points with coordinates:
2,3;4,9;1,6;3,10.
How to use Python to draw one perpendicular bisector between (2,3) and (4,9);
the other perpendicular bisector between (1,6)和(3,10);
then, makes the output like:
l1 a b c
l2 a b c
(Note: l indicates the perpendicular bisector with equation ax + by = c.)
Plus the intersection coordinates of the two perpendicular bisectors:
x,y
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] output question

2005-10-14 Thread Shi Mu
After I run the following python code, I expect to have the printing such as:
The year is 2005

However, I got something like:
The year is 2005
Fri Oct 14 17:43:31 2005
Fri Oct 14 17:43:31 2005
The year is 2005

What is the reason?

The code follows:

import time
import now

class today(now.now):
def __init__(self, y = 1970):
now.now.__init__(self)
def update(self,tt):
if len(tt) < 9 :
raise TypeError
if tt[0] < 1970 or tt[0] > 2038:
raise OverflowError
self.t = time.mktime(tt)
self(self.t)

if __name__ == "__main__":
n = today()
print "The year is", n.year
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] output question

2005-10-14 Thread Shi Mu
I found the code for class "now". I got confused by two things:
First, how did the former code I posted know to import tis module of "Now";
Second. what does "\" mean following "self.year,"
Thanks a lot!

class now:
def __init__(self):
self.t = time.time()
self.storetime()
def storetime(self):
self.year, \
self.month, \
self.day, \
self.hour, \
self.minute, \
self.second, \
self.dow, \
self.doy, \
self.dst = time.localtime(self.t)
def __str__(self):
return time.ctime(self.t)
def __repr__(self):
return time.ctime(self.t)
def __call__(self,t=-1.0):
if t < 0.0:
self.t = time.time()
else:
self.t = t
self.storetime()


On 10/14/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Shi Mu wrote:
> > After I run the following python code, I expect to have the printing such 
> > as:
> > The year is 2005
> >
> > However, I got something like:
> > The year is 2005
> > Fri Oct 14 17:43:31 2005
> > Fri Oct 14 17:43:31 2005
> > The year is 2005
> >
> > What is the reason?
>
> Maybe coming from module 'now'? What is that?
>
> Kent
>
> >
> > The code follows:
> >
> > import time
> > import now
> >
> > class today(now.now):
> > def __init__(self, y = 1970):
> >   now.now.__init__(self)
> > def update(self,tt):
> >   if len(tt) < 9 :
> >   raise TypeError
> >   if tt[0] < 1970 or tt[0] > 2038:
> >   raise OverflowError
> >   self.t = time.mktime(tt)
> >   self(self.t)
> >
> > if __name__ == "__main__":
> > n = today()
> > print "The year is", n.year
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] setting

2005-10-19 Thread Shi Mu
I have installed Python 2.3 and I type "help()" and then "Keywords".
I get a list of words. And it says that I can enter any of the words
to get more help.  I enter
"and" and I get the following error message:
"Sorry, topic and keyword documentation is not available because the Python
HTML documentation files could not be found.  If you have installed them,
please set the environment variable PYTHONDOCS to indicate their location."

but I have set both the environment variable, with the path to be
C:\Python23\Doc which includes python23.chm
Why I still got the above error message?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] index and find

2005-10-22 Thread Shi Mu
what is the difference between index and find in the module of string?
for both "find" and "index", I got the position of the letter.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] what does %25f mean?

2005-10-24 Thread Shi Mu
what does %25f mean?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] dictionary

2005-10-24 Thread Shi Mu
I typed:
landUse = {'res': 1, 'com': 2, 'ind': 3, "other" :[4,5,6,7]}
and when i checked landUse, I found it become:
{'ind': 3, 'res': 1, 'other': [4, 5, 6, 7], 'com': 2}
why the order is changed?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] string

2005-10-24 Thread Shi Mu
what does the following sentence mean? does it mean i can not use
double-quoted string?
SyntaxError: EOL while scanning single-quoted string
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] about FIND

2005-10-24 Thread Shi Mu
I got confused by the following information from the help for "FIND":
find(s, *args)
  find(s, sub [,start [,end]]) -> in

what does *args mean (especially the '*')?

also, in the sub, why put a comma before start?

what does 'in' mean?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] about \

2005-10-24 Thread Shi Mu
Is there any difference if I remove the '/'
from the following statement?
intMatrix2 = [[1,1,2,4,1,7,1,7,6,9],\
 [1,2,5,3,9,1,1,1,9,1],\
 [0,0,5,1,1,1,9,7,7,7]]
print intMatrix2
I removed one '\' and it still works.
So what is the use of '\'?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help

2005-10-24 Thread Shi Mu
I got confused by the following information from the help for "FIND":
find(s, *args)
   find(s, sub [,start [,end]]) -> in

what does *args mean (especially the '*')?

also, in the sub, why put a comma before start?

what does 'in' mean?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Matrix

2005-10-24 Thread Shi Mu
I can not understand the use of "cell in row" for two times in the code:

# convert the matrix to a 1D list
matrix = [[13,2,3,4,5],[0,10,6,0,0],[7,0,0,0,9]]
items = [cell for row in matrix for cell in row]
print items
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tkinter

2005-10-29 Thread Shi Mu
When I run the following code,
script kept running and I have to force it to stop.
Could you check the code to give suggestions how to improve it?
Thanks a lot!

from Tkinter import *
from Tkinter import _cnfmerge

class Dialog(Widget):
  def __init__(self, master=None, cnf={}, **kw):
  cnf = _cnfmerge((cnf, kw))
  self.widgetName = '__dialog__'
  Widget._setup(self, master, cnf)
  self.num = self.tk.getint(
  apply(self.tk.call,
('tk_dialog', self._w,
 cnf['title'], cnf['text'],
 cnf['bitmap'], cnf['default'])
+ cnf['strings']))
  try: Widget.destroy(self)
  except TclError: pass
  def destroy(self): pass

if __name__ == '__main__':

  q = Button(None, {'text': 'How are you',
Pack: {}})
  b1 = Listbox()
  b1.pack()

  c1 = Checkbutton(text="Check")
  c1.pack()

  q.mainloop()

from Tkinter import *
root =Tk()
menu=Menu(root)
root.config(menu=menu)
filemenu=Menu(menu)
menu.add_cascade(label="Test", menu=filemenu)
filemenu.add_command(label="Just Try")
filemenu.add_separator()
mainloop()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter

2005-10-29 Thread Shi Mu
move the bottom codes to the top and try to make
the codes following if __name__ == '__main__':
as few as possible.
also,
i want the main menu come first, and after clicking test/just try, i
can go to the "how are you" interface.

On 10/29/05, Liam Clarke <[EMAIL PROTECTED]> wrote:
> What kind of improvement you looking for?
>
> On 10/29/05, Shi Mu <[EMAIL PROTECTED]> wrote:
> > When I run the following code,
> > script kept running and I have to force it to stop.
> > Could you check the code to give suggestions how to improve it?
> > Thanks a lot!
> >
> > from Tkinter import *
> > from Tkinter import _cnfmerge
> >
> > class Dialog(Widget):
> >   def __init__(self, master=None, cnf={}, **kw):
> >   cnf = _cnfmerge((cnf, kw))
> >   self.widgetName = '__dialog__'
> >   Widget._setup(self, master, cnf)
> >   self.num = self.tk.getint(
> >   apply(self.tk.call,
> > ('tk_dialog', self._w,
> >  cnf['title'], cnf['text'],
> >  cnf['bitmap'], cnf['default'])
> > + cnf['strings']))
> >   try: Widget.destroy(self)
> >   except TclError: pass
> >   def destroy(self): pass
> >
> > if __name__ == '__main__':
> >
> >   q = Button(None, {'text': 'How are you',
> > Pack: {}})
> >   b1 = Listbox()
> >   b1.pack()
> >
> >   c1 = Checkbutton(text="Check")
> >   c1.pack()
> >
> >   q.mainloop()
> >
> > from Tkinter import *
> > root =Tk()
> > menu=Menu(root)
> > root.config(menu=menu)
> > filemenu=Menu(menu)
> > menu.add_cascade(label="Test", menu=filemenu)
> > filemenu.add_command(label="Just Try")
> > filemenu.add_separator()
> > mainloop()
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] compare

2005-10-31 Thread Shi Mu
What does this line 11 mean in the following code?

1 # calc.py
2 def calc(seq):
3   maximum = 0
4   max_item = []
5   for i in seq:
6 product = (i[0]*100 + i[1]*10 + i[2]) * (i[3]*10 + i[4])
7 if product > maximum:
8maximum = product
9max_item = i
   10 elif product == maximum:
   11max_item += ','+i
   12   return max_item, maximum
   13
   14 seq = [ [5,6,7,8,9], [5,6,7,9,8] ]
   15 max_item, maximum = calc(seq)
   16 print "Maximum at", max_item, ",product", maximum
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pack

2005-11-04 Thread Shi Mu
what does pack mean in the following code?

# File: hello1.py

from Tkinter import *

root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pack

2005-11-04 Thread Shi Mu
Why the button of the following code does not work? Thanks!

from Tkinter import *

class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)

self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)

def say_hi(self):
print "hi there, everyone!"

root = Tk()

app = App(root)

root.mainloop()


On 11/4/05, Hugo González Monteverde <[EMAIL PROTECTED]> wrote:
> pack is a method of Tkinter widget that needs to be called in order to
> have it displayed with the current geometry manager(which per default is
>  the pack method)
>
> A geometry manager is just a way for arranging widgets into a window.
> Another geometry manager is "grid"
>
> If you don't specify anything, the widget will simply not be displayed..
> see here:
>
> http://effbot.org/tkinterbook/pack.htm
>
>
> Hugo
>
> Shi Mu wrote:
> > what does pack mean in the following code?
> >
> > # File: hello1.py
> >
> > from Tkinter import *
> >
> > root = Tk()
> >
> > w = Label(root, text="Hello, world!")
> > w.pack()
> >
> > root.mainloop()
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pack

2005-11-05 Thread Shi Mu
when I clicked 'quit' button,
there is no response. I want to close the interface by clicking 'x',
the interface could not be closed.
i had to close PYTHONWIN to get out of the program.


On 11/5/05, Alan Gauld <[EMAIL PROTECTED]> wrote:
> > Why the button of the following code does not work? Thanks!
>
> The program works perfectly for me.
> Which button do you think is not working?
>
> If its the Hello one where do you expect to see the output?
> print always prints to stdout so you need to look in the
> console window not on the GUI.
>
> But otherwise the program worked without modification for me.
>
> HTH,
>
> Alan G.
>
> --
> from Tkinter import *
>
> class App:
>
>def __init__(self, master):
>
>frame = Frame(master)
>frame.pack()
>
>self.button = Button(frame, text="QUIT", fg="red",
> command=frame.quit)
>self.button.pack(side=LEFT)
>
>self.hi_there = Button(frame, text="Hello", command=self.say_hi)
>self.hi_there.pack(side=LEFT)
>
>def say_hi(self):
>print "hi there, everyone!"
>
> root = Tk()
>
> app = App(root)
>
> root.mainloop()
>
>
> On 11/4/05, Hugo González Monteverde <[EMAIL PROTECTED]> wrote:
> > pack is a method of Tkinter widget that needs to be called in order to
> > have it displayed with the current geometry manager(which per default is
> >  the pack method)
> >
> > A geometry manager is just a way for arranging widgets into a window.
> > Another geometry manager is "grid"
> >
> > If you don't specify anything, the widget will simply not be displayed..
> > see here:
> >
> > http://effbot.org/tkinterbook/pack.htm
> >
> >
> > Hugo
> >
> > Shi Mu wrote:
> > > what does pack mean in the following code?
> > >
> > > # File: hello1.py
> > >
> > > from Tkinter import *
> > >
> > > root = Tk()
> > >
> > > w = Label(root, text="Hello, world!")
> > > w.pack()
> > >
> > > root.mainloop()
> > > ___
> > > Tutor maillist  -  Tutor@python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > >
> >
>
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] main

2005-11-05 Thread Shi Mu
It is very hard for me to understand why we need the following line?
if __name__ == "__main__":
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] point and polygon

2005-11-06 Thread Shi Mu
why the following code report error?
"""
Is given point in polygon?
"""


def pointInPoly(point, pointsList):
"Return True if point is contained in polygon (defined by
given list of points.)"

assert len(pointsList) >= 3, 'Not enough points to define a
polygon (I require 3 or more.)'
assert len(point) >= 2, 'Not enough dimensions to define a
point(I require 2 or more.)'

# If given values are ints, code will fail subtly. Force them to floats.
x,y = float(point[0]), float(point[1])
xp = [float(p[0]) for p in pointsList]
yp = [float(p[1]) for p in pointsList]

# Initialize loop
c=False
i=0
npol = len(pointsList)
j=npol-1

while i < npol:
if yp[i]<=y) and (yhttp://mail.python.org/mailman/listinfo/tutor


[Tutor] inside

2005-11-06 Thread Shi Mu
why the following code does not work?

# determine if a point is inside a given polygon or not
# Polygon is a list of (x,y) pairs.

def point_inside_polygon(x,y,poly):

n = len(poly)
inside =False

p1x,p1y = poly[0]
for i in range(n+1):
p2x,p2y = poly[i % n]
if y > min(p1y,p2y):
if y <= max(p1y,p2y):
if x <= max(p1x,p2x):
if p1y != p2y:
xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x,p1y = p2x,p2y

return inside

if __name__ == '__main__':
   print 'Simple pointInPoly() test...'

   poly = [(-1,-1), (6,-1), (5,6), (0,5)]
   point_inside_polygon(3,10,poly)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] color vector

2005-11-06 Thread Shi Mu
why the following code does not work?

"""
Tkinter Color Vector Objects

Just the bare minimum to create re-sizable
and re-usable color icons in tkinter.
"""

import Tkinter as Tk
import math
""" Get points for a unit regular-polygon with n sides. """
def getregpoly(sides):
points = []
ang = 2*math.pi / sides
for i in range(sides):
deg = (i+.5)*ang
points.append(math.sin(deg)/2.0+.5)
points.append(math.cos(deg)/2.0+.5)
return points

def scale(points, scale):
return [x*scale for x in points]

def move(points, x, y):
xy = [x,y]*(len(points)//2)
return [xy+coord for xy, coord in zip(xy,points)]

def translate(obj, x, y, zoom):
p = scale(obj.points, obj.size)
p = move(p, obj.x, obj.y)
p = scale(p, zoom)
return move(p, x, y)

def draw(obj, c, x=0 ,y=0, zoom=1):
p = translate(obj, x, y, zoom)
if obj.obj=='line':
c.create_line( p, fill=obj.fill, width=obj.width, arrow=obj.arrow)
elif obj.obj=='rectangle':
c.create_line( p, fill=obj.fill, outline=obj.outline,width=obj.width)
elif obj.obj=='polygon':
c.create_polygon( p, fill=obj.fill,
outline=obj.outline,width=obj.width,smooth=obj.smooth)
elif obj.obj=='text':
size = int(obj.size*zoom)
font = (obj.font,size,obj.style)
c.create_text(p, text=obj.text, font=font, fill=obj.fill)
elif obj.obj=='oval':
c.create_oval( p, fill=obj.fill, outline=obj.outline,width=obj.width )
elif obj.obj=='arc':
c.create_arc( p, start=obj.start,
extent=obj.extent,style=obj.style, fill=obj.fill,outline=obj.outline,
width=obj.width)

class Shape(object):
size = 1
x = y = 0
def __init__(self, **kwds):
self.__dict__.update(kwds)
def __call__(self, *args, **kwds):
for key in self.__dict__:
if key not in kwds:
kwds[key] = self.__dict__[key]
return self.__class__(*args, **kwds)

def draw(self, c, x=0, y=0, scale=1.0):
draw(self, c, x, y, scale)

class Group(list):
obj = 'group'
def __init__(self, *args, **kwds):
self[:] = args
self.__dict__.update(kwds)
def __call__(self, *args, **kwds):
args = self[:]+list(args)
for key in self.__dict__:
if key not in kwds:# make copies
kwds[key] = self.__dict__[key]()
return self.__class__(*args, **kwds)
def draw(self, c, x=0, y=0, scale=1.0):
for item in self:
item.draw(c, x, y, scale)
for key in self.__dict__:
self.__dict__[key].draw(c, x, y, scale)

# base shapes.
text = Shape( obj='text', text='', fill='black', width=0,font='',
style='', points=[0,0] )
line = Shape( obj='line', arrow='none', fill='black',smooth='false',
width=1, points=[0,0,1,0])
rectangle = Shape( obj='rectangle', fill='', outline='black',width=1,
points=[0,0,1,.5])
polygon = Shape( obj='polygon', fill='grey', outline='',width=0,
points=[0,0], smooth='false' )
oval = Shape( obj='oval', fill='grey', outline='',width=0, points=[0,0,1,.75] )
arc = Shape( obj='arc', fill='grey', outline='', width=0,style='arc',
start='0', extent='90',points=[0,0,1,1])

# shape variations
chord = arc(style='chord')
pie = arc(style='pieslice')
circle = oval(points=[0,0,1,1])
square = rectangle(points=[0,0,1,1])
triangle = polygon( points=getregpoly(3))
octagon = polygon( points=getregpoly(8))

# CAUTION ICON
caution = Group(
triangle(x=6, y=5, size=75),
triangle(size=75, fill='yellow'),
txt = text( text='!',
x=38, y=32, size=30,
font='times', style='bold') )

# ERROR ICON
circlepart = chord( x=15, y=15, size=25, fill='red',
start='140', extent='155' )
error = Group(
octagon(x=6, y=5, size=56),
octagon(size=56, fill='red'),
circle(x=9, y=9, size=37, fill='white'),
circlepart(),
circlepart(start='320') )

# QUESTION & INFO ICONS
bubbletip = polygon(points=[34,42,60,56,50,38])
question = Group(
bubbletip(x=6, y=5),
oval(x=6, y=5, size=60),
bubbletip(fill='lightblue'),
oval(size=60, fill='lightblue'),
txt = text( text='?',
x=31, y=22, size=28,
font='times', style='bold' ) )
info = question()
info.txt.text = 'i'

if __name__ == '__main__':
root = Tk.Tk()
root.title('Resizable Shapes')
c = Tk.Canvas(root)

caution.draw(c,40,20,.5)
error.draw(c,120,20,1)
question.draw(c,210,20,2)
info.draw(c,50,100)

logo = caution() # get a copy
logo.txt = text( text='&', fill='#00bb44',
x=39, y=34, size=30 )
logo.draw(c,135,110,1.3)

message = text( text="What's Your Size?",
size=15, fill='white' )
Group( message( x=1, y=1, fill='grey30'),
message() ).draw(c,190,235,2)

line( width=3, fill='darkgrey', arrow='both').draw(c,20,205,336)

c.pack()
root.mainloop()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] inside

2005-11-06 Thread Shi Mu
just return the
"Simple pointInPoly() test..."
no return from point_inside_polygon(3,10,poly)

On 11/6/05, Glen Wheeler <[EMAIL PROTECTED]> wrote:
> > why the following code does not work?
> > [snip script]
>
>  Give us an error message, please.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] inside

2005-11-06 Thread Shi Mu
there is no error message but in the console
I just see Simple pointInPoly() test...


On 11/6/05, Liam Clarke <[EMAIL PROTECTED]> wrote:
> Shi,
>
> I've just seen 3 queries from you all along the lines of -
>
> why the following code does not work?
>
> 
>
> If English is not your first language, I can understand you're not
> going to write us a novel.
> However, you'll get a better response if you copy and paste the error
> message (the traceback) that you're getting in the console.
>
> Do you know how to copy and paste from the console?
>
> A lot of us aren't anywhere we can run your code; if we did, it may
> work, the error may not be an unhandled exception, just unexpected
> behaviour, and we have no idea what behaviour you're expecting.
>
> So please, in future give us this basic information -
>
> If there's an error message - copy and paste the error message
> If there is no error message - tell us what you expected to happen,
> and what happened instead.
>
> Regards,
>
> Liam Clarke
> On 11/7/05, Shi Mu <[EMAIL PROTECTED]> wrote:
> > why the following code does not work?
> >
> > # determine if a point is inside a given polygon or not
> > # Polygon is a list of (x,y) pairs.
> >
> > def point_inside_polygon(x,y,poly):
> >
> > n = len(poly)
> > inside =False
> >
> > p1x,p1y = poly[0]
> > for i in range(n+1):
> > p2x,p2y = poly[i % n]
> > if y > min(p1y,p2y):
> > if y <= max(p1y,p2y):
> > if x <= max(p1x,p2x):
> > if p1y != p2y:
> > xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
> > if p1x == p2x or x <= xinters:
> > inside = not inside
> > p1x,p1y = p2x,p2y
> >
> > return inside
> >
> > if __name__ == '__main__':
> >print 'Simple pointInPoly() test...'
> >
> >poly = [(-1,-1), (6,-1), (5,6), (0,5)]
> >point_inside_polygon(3,10,poly)
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] overlay

2005-11-06 Thread Shi Mu
Is there any sample code to calculate the overlaid area between two polygons?
Thanks!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] draw lines

2005-11-06 Thread Shi Mu
I have a list of random points:
[[x0,y0],[x1,y1],...,[xn,yn]]
how can I use Tkinter to draw lines to connect them one by one based
on the order in the list?
Thanks a lot!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] click and line

2005-11-06 Thread Shi Mu
I tried to write the follwoing code to draw lines on the anvas based
on the clicked points but retur the message:
>>> Traceback (most recent call last):
  File "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
  File "C:\Python23\test\Q_line.py", line 13, in ?
l5=c.create_line(m1,n1,event.x,event.y,fill='blue',width='50')
NameError: name 'm1' is not defined

The ocde migth be naive,...
from Tkinter import *

root = Tk()

c = Canvas(root, bg='#0e2e0e', height=500, width=1000)

def click(event):
print event.x, event.y
m1=event.x
n1=event.y
frame = c
c.bind('',click)
l5=c.create_line(m1,n1,event.x,event.y,fill='blue',width='50')

c.pack()

root.mainloop()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] click and line

2005-11-06 Thread Shi Mu
based on the following rewritten code, why the lines still can not be
drawn? (there is no error report and the canvas appears).

from Tkinter import *

root = Tk()

c = Canvas(root, bg='#0e2e0e', height=500, width=1000)

lastX=""
lastY=""
def click(event):
  global lastX, lastY
  if lastX != "":
 c.create_line(lastX,lastY,event.x,event.y,fill="white")
  lastX = event.x
  lastY = event.y

frame = c
c.bind('',click)

c.pack()

root.mainloop()

On 11/6/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
>
> Hi Shi Mu,
>
> Let's look at click():
>
> ##
> def click(event):
>print event.x, event.y
>m1=event.x
>n1=event.y
> ##
>
> By default, variable names in functions are local.  Do you know about
> local vs global variables?  The assignments to m1 and n1 won't be
> remembered once we come out of the click() function.
>
>
> It sounds like you want a function-like thing that remembers the very last
> event we passed to it before.  One way to do this is to use globals,
> because globals last beyond function calls.
>
> For example:
>
> ##
> >>> n = None
> >>> def lastNumber(x):
> ... global n
> ... print "I last saw", n
> ... n = x
> ...
> >>> lastNumber(42)
> I last saw None
> >>> lastNumber(17)
> I last saw 42
> >>> lastNumber(3)
> I last saw 17
> ##
>
> So for something very simple, using a global will probably be easiest.
>
>
> However, there are other approaches that don't use globals. One that fits
> Python's model well is to use a class instance, since instances can store
> state:
>
> ##
> >>> class LastNumber:
> ... def __init__(self):
> ... self.n = None
> ... def getAndSet(self, x):
> ... value = self.n
> ... self.n = x
> ... return value
> ...
> >>> last = LastNumber()
> >>> last.getAndSet(42)
> >>> last.getAndSet(3)
> 42
> >>> last.getAndSet(7)
> 3
> >>> last.getAndSet(9)
> 7
> ##
>
>
> Does this make sense?  Please feel free to ask questions here.
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] click and line

2005-11-06 Thread Shi Mu
it works now.

from Tkinter import *

root = Tk()

c = Canvas(root, bg='#0e2e0e', height=500, width=1000)
frame = c
lastX=""
lastY=""
def click(event):
  global lastX, lastY
  if lastX != "":
 c.create_line(lastX,lastY,event.x,event.y,fill="white")
  lastX = event.x
  lastY = event.y


c.bind('',click)

c.pack()

root.mainloop()

On 11/6/05, Shi Mu <[EMAIL PROTECTED]> wrote:
> based on the following rewritten code, why the lines still can not be
> drawn? (there is no error report and the canvas appears).
>
> from Tkinter import *
>
> root = Tk()
>
> c = Canvas(root, bg='#0e2e0e', height=500, width=1000)
>
> lastX=""
> lastY=""
> def click(event):
>  global lastX, lastY
>  if lastX != "":
> c.create_line(lastX,lastY,event.x,event.y,fill="white")
>  lastX = event.x
>  lastY = event.y
>
> frame = c
> c.bind('',click)
>
> c.pack()
>
> root.mainloop()
>
> On 11/6/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> >
> > Hi Shi Mu,
> >
> > Let's look at click():
> >
> > ##
> > def click(event):
> >print event.x, event.y
> >m1=event.x
> >n1=event.y
> > ##
> >
> > By default, variable names in functions are local.  Do you know about
> > local vs global variables?  The assignments to m1 and n1 won't be
> > remembered once we come out of the click() function.
> >
> >
> > It sounds like you want a function-like thing that remembers the very last
> > event we passed to it before.  One way to do this is to use globals,
> > because globals last beyond function calls.
> >
> > For example:
> >
> > ##
> > >>> n = None
> > >>> def lastNumber(x):
> > ... global n
> > ... print "I last saw", n
> > ... n = x
> > ...
> > >>> lastNumber(42)
> > I last saw None
> > >>> lastNumber(17)
> > I last saw 42
> > >>> lastNumber(3)
> > I last saw 17
> > ##
> >
> > So for something very simple, using a global will probably be easiest.
> >
> >
> > However, there are other approaches that don't use globals. One that fits
> > Python's model well is to use a class instance, since instances can store
> > state:
> >
> > ##
> > >>> class LastNumber:
> > ... def __init__(self):
> > ... self.n = None
> > ... def getAndSet(self, x):
> > ... value = self.n
> > ... self.n = x
> > ... return value
> > ...
> > >>> last = LastNumber()
> > >>> last.getAndSet(42)
> > >>> last.getAndSet(3)
> > 42
> > >>> last.getAndSet(7)
> > 3
> > >>> last.getAndSet(9)
> > 7
> > ##
> >
> >
> > Does this make sense?  Please feel free to ask questions here.
> >
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] click and line

2005-11-06 Thread Shi Mu
frame = c needs to be put before the click function or the lines can
not be drawn.

On 11/6/05, Chris F.A. Johnson <[EMAIL PROTECTED]> wrote:
> On Sun, 6 Nov 2005, Shi Mu wrote:
>
> > based on the following rewritten code, why the lines still can not be
> > drawn? (there is no error report and the canvas appears).
>
> It works for me.
>
>
> > from Tkinter import *
> >
> > root = Tk()
> >
> > c = Canvas(root, bg='#0e2e0e', height=500, width=1000)
> >
> > lastX=""
> > lastY=""
> > def click(event):
> >  global lastX, lastY
> >  if lastX != "":
> > c.create_line(lastX,lastY,event.x,event.y,fill="white")
> >  lastX = event.x
> >  lastY = event.y
> >
> > frame = c
> > c.bind('',click)
> >
> > c.pack()
> >
> > root.mainloop()
>
> --
> Chris F.A. Johnson <http://cfaj.freeshell.org>
> ==
> Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
> <http://www.torfree.net/~chris/books/cfaj/ssr.html>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] no error no result

2005-11-06 Thread Shi Mu
when I read some python sample codes.
I found that several of them need "python -i" to run them or you will
not get any result though the program does not report any error.
I really can not understand it.
Thanks!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] image

2005-11-07 Thread Shi Mu
why the follwoing code report the error:
Traceback (most recent call last):
  File "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
  File "C:\Python23\Examples\AppB\text.py", line 24, in ?
text.image_create(END, image=photo)
  File "C:\Python23\lib\lib-tk\Tkinter.py", line 2882, in image_create
return self.tk.call(
TclError: image "pyimage4" doesn't exist

from Tkinter import *

root = Tk()
root.option_readfile('optionDB')
root.title('Text')
text = Text(root, height=26, width=50)
scroll = Scrollbar(root, command=text.yview)
text.configure(yscrollcommand=scroll.set)
text.tag_configure('bold_italics', font=('Verdana', 12, 'bold', 'italic'))
text.tag_configure('big', font=('Verdana', 24, 'bold'))
text.tag_configure('color', foreground='blue', font=('Tempus Sans ITC', 14))
text.tag_configure('groove', relief=GROOVE, borderwidth=2)
text.tag_bind('bite', '<1>',
  lambda e, t=text: t.insert(END, "I'll bite your legs off!"))

text.insert(END, 'Something up with my banter, chaps?\n')
text.insert(END, 'Four hours to bury a cat?\n', 'bold_italics')
text.insert(END, 'Can I call you "Frank"?\n', 'big')
text.insert(END, "What's happening Thursday then?\n", 'color')
text.insert(END, 'Did you write this symphony in the shed?\n', 'groove')
button = Button(text, text='I do live at 46 Horton terrace')
text.window_create(END, window=button)
photo=PhotoImage(file='img52.gif')
text.image_create(END, image=photo)
text.insert(END, 'I dare you to click on this\n', 'bite')
text.pack(side=LEFT)
scroll.pack(side=RIGHT, fill=Y)

root.mainloop()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] click and line

2005-11-07 Thread Shi Mu
Danny,
You are right.
Thanks for your great patience and kindness.
Shi

On 11/7/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
>
>
> > it works now.
>
> Hi Shi Mu,
>
> Can you describe how you fixed it?  The only diff between the two programs
> that I see is that you repositioned the setting of the frame variable.
>
>
> #
> mumak:~ dyoo$ diff t1.txt  t2.txt
> 6c6
> <
> ---
> > frame = c
> 16c16
> < frame = c
> ---
> >
> #
>
> What makes the "fixed" code suspicious is that the 'frame' variable is
> useless in the sense that it doesn't do anything.  And even if it did do
> something, the fix --- moving a variable definition around --- doesn't
> make sense to me.  *grin*
>
> Try changing it back and see if the problem recurs.  (I suspect that your
> first version also worked, but that you may have forgotten to save your
> file before testing it.)
>
>
> Best of wishes!
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] geometry

2005-11-07 Thread Shi Mu
any python code for middle school geometry teaching use?
for instance, calculate the polygon area size, overlaid area size?
Thanks.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] geometry

2005-11-07 Thread Shi Mu
Because i am also teaching some introduction courses of programming in
middle school, I prefer some 2D geometry codes.


On 11/7/05, Chris or Leslie Smith <[EMAIL PROTECTED]> wrote:
>
> | any python code for middle school geometry teaching use?
> | for instance, calculate the polygon area size, overlaid area size?
> | Thanks.
> |
>
> Yes, the module I refered to in response to the email with subject "Overlay" 
> will handle these calculations.
>
> Again, see ( 
> http://www.dezentral.de/warp.html?http://www.dezentral.de/soft/Polygon/ ).
>
> /c
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] image

2005-11-08 Thread Shi Mu
any python module to calculate sin, cos, arctan?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] any code to draw parabola or curve?

2005-11-08 Thread Shi Mu
any code to draw parabola or curve?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] triangulation

2005-11-09 Thread Shi Mu
is there any sample code of triangulation? many thanks!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] triangulation

2005-11-10 Thread Shi Mu
the Internet is down for one day and so wonderful to have so many
responses. i have checked all the links you guys mentioned. what i
want is delaunay triangulation and the available ones online are
written in C, Java and FORTRAN. I want to see some in Python because
it is hard for me to figure out using python to do Fortune's sweeping
line algorithm. Is python is not good in doing that kind of
computation or some other reason?
Thanks a lot for all of your responses!!!

On 11/9/05, Gregor Lingl <[EMAIL PROTECTED]> wrote:
>
>
> Alex Hunsley schrieb:
> > Shi Mu wrote:
> >
> >
> >>is there any sample code of triangulation? many thanks!
> >>
> >>
> >
> > Yes, probably.
> >
>
>
> Indeed, there is.
> See attachment
>
> regards
> Gregor
>
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
> --
> Gregor Lingl
> Reisnerstrasse 3/19
> A-1030 Wien
>
> Telefon: +43 1 713 33 98
> Mobil:   +43 664 140 35 27
>
> Website: python4kids.net
>
>
> ## Run this program and create a polygon without
> ## intersecting edges by clicking the vertices with the mouse
>
> from Tkinter import *
>
> def area2(A,B,C):
> """2 * Flaeche des 2D-Dreiecks ABC"""
> return (A[0]-C[0])*(B[1]-C[1]) - (A[1]-C[1])*(B[0]-C[0])
>
> def insideTriangle(A, B, C, P):
> """Liegt P im Dreieck ABC?,
> ABC sind im Gegenuhrzeigersinn angenommen!"""
> return area2(A,B,P)>=0 and area2(B,C,P)>=0 and area2(C,A,P)>=0
>
> def triangulate(poly):
> tri = []
> while len(poly) > 2:
> triaFound = False
> count = 0
> while not triaFound and count < len(poly): #n:
> count += 1
> A, B, C = poly[:3]
> if area2(A, B, C) >= 0:
> for P in poly[3:]:
> if insideTriangle(A,B,C,P):
> break
> else:
> tri.append( (A,B,C) )
> poly.remove(B)
> triaFound = True
> poly.append(poly.pop(0))
> if count == len(poly): # n:
> print "Not a simple polygon"
> return None
> return tri
>
>
> class CvDefPoly(Canvas):
> def __init__(self, root):
> Canvas.__init__(self, root, bg="white", cursor="crosshair")
> self.v = ()
> self.rWidth, self.rHeight = 10.0, 7.5
>
> self.bind("", self.repaint)
> self.bind("", self.mousePressed)
>
> self.done = False
>
> def mousePressed(self, event):
> if self.done:
> self.done = False
> self.v = ()
> x ,y = self.fx(event.x), self.fy(event.y)
> if self.v:
> x0, y0 = self.v[:2]
> if abs(x-x0) self.done = True
> if not self.done:
> self.v += (x,y)
> self.repaint(None)
>
> def iX(self,x):
> return self.centerX + x/self.pixelSize
> def iY(self,y):
> return self.centerY - y/self.pixelSize
> def fx(self,x):
> return (x-self.centerX)*self.pixelSize
> def fy(self,y):
> return (self.centerY - y)*self.pixelSize
>
> def repaint(self, event):
> items = self.find_all()
> for item in items:
> self.delete(item)
> if event:
> self.w, self.h = event.width, event.height
> w,h = self.w, self.h
> self.minX = self.minY = 2.0
> self.maxX, self.maxY = w-3.0, h-3.0
> dx, dy = self.maxX-self.minX, self.maxY-self.minY
> self.centerX = 2.0 + dx/2.0
> self.centerY = 2.0 + dy/2.0
> self.pixelSize = max(self.rWidth/dx, self.rHeight/dy)
>
> left = self.iX(-self.rWidth/2.0)
> right = self.iX(self.rWidth/2.0)
> bottom = self.iY(-self.rHeight/2.0)
> top = self.iY(self.rHeight/2.0)
>
> self.create_rectangle(left,top,right,bottom, outline="red")
> if len(self.v) > 1:
> x,y = self.v[:2]
> self.create_rectangle(self.iX(x)-2, self.iY(y)-2,
>   self.iX(x)+2, self.iY(y)+2, outline="green")
> if len(self.v) > 3:
> if self.done:
> v = []
> sv = self.v[:]
> while sv:
> a,b=sv[:2]
> v.ex

Re: [Tutor] triangulation

2005-11-10 Thread Shi Mu
I see. maybe some websites are blocked to me.

On 11/10/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
>
> Hi Shi Mu,
>
> There are some direct ways you can find a delaunay triangulation
> implementation in Python.  In fact, the first Google hit to "triangulation
> python" should come up with something relevant.
>
> Also, try the search "triangulation" in:
>
>http://www.python.org/pypi?%3Aaction=search_form
>
> Are you having trouble accessing Google or the Web?  I mean this in all
> seriousness;  do you have little experience with web search engines, or
> does your ISP restrict you from accessing them?
>
> I'm just trying to construct some reasonable explanation why you're having
> difficulty finding these utilities and tools.  I'm trying to treat it the
> same way I treat a software bug, by probing for causes and reasons.
>
> Good luck to you.
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] two ways

2005-11-10 Thread Shi Mu
what is the difference between the two ways of assigning the list?
p=a vs. p=a[:]
>>> a=range(5)
>>> a
[0, 1, 2, 3, 4]
>>> p=a
>>> p
[0, 1, 2, 3, 4]
>>> p=a[:]
>>> p
[0, 1, 2, 3, 4]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] two ways

2005-11-10 Thread Shi Mu
thanks!

On 11/10/05, Liam Clarke <[EMAIL PROTECTED]> wrote:
> Hi Shi,
>
> For what you're doing, nothing at all.
>
> When you use a colon, slice syntax, it defaults to [start:end] so p =
> a[:] is the same as
> p = a[0:len(a)]
>
> Regards,
>
> Liam Clarke
>
> On 11/11/05, Shi Mu <[EMAIL PROTECTED]> wrote:
> > what is the difference between the two ways of assigning the list?
> > p=a vs. p=a[:]
> > >>> a=range(5)
> > >>> a
> > [0, 1, 2, 3, 4]
> > >>> p=a
> > >>> p
> > [0, 1, 2, 3, 4]
> > >>> p=a[:]
> > >>> p
> > [0, 1, 2, 3, 4]
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] two ways

2005-11-11 Thread Shi Mu
thanks!

On 11/11/05, Gregor Lingl <[EMAIL PROTECTED]> wrote:
> Liam Clarke schrieb:
>
> >Hi Shi,
> >
> >For what you're doing, nothing at all.
> >
> >When you use a colon, slice syntax, it defaults to [start:end] so p =
> >a[:] is the same as
> >p = a[0:len(a)]
> >
> >
> >
> But in fact there is a difference. I'll show you:
>
>  >>> a=range(5)### creates a list-object
>  >>> id(a) ### which has an identity
> 14716520
>  >>> p=a  ### creates the new name p for the same object
>  >>> id(p)### let's see
> 14716520   ### o.k. really the same
>  >>> q=a[:]  ### this names a *copy* of a q
>  >>> id(q)### identity?
> 14714840   ### different!
>  >>> a is p   ### check if objects named a and p are identical
> True### yes
>  >>> a is q  ### check if objects named a and q are identical
> False  ### no!
>  >>> a == q
> True   ### but have the same "content", i. e. two
> different list-objects with the same elements
>  >>>
>  >>>### Beware, lists are mutable:
>  >>>
>  >>> a
> [0, 1, 2, 3, 4]
>  >>> a[1]=1001
>  >>> a
> [0, 1001, 2, 3, 4]
>  >>> p
> [0, 1001, 2, 3, 4]### object named a *and* p is changed
>  >>> q ### copy q of a is not changed!°
> [0, 1, 2, 3, 4]
>  >>>
> regards
> Gregor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionary question

2005-11-11 Thread Shi Mu
On 11/11/05, Eric Walker <[EMAIL PROTECTED]> wrote:
> All,
> I have a dictionary say:
> d = {'first':[]}
> I am going through another list and depending on whats going on,
> I want to add to the empty list. I have tried the following to noavail.
>
> d['first'] = d['first'].append("string")
>
> I would think this would result in the dictionary key first's empty list  to
> become ["string"].  can anyone help clear my head?
>
> Thanks...

I guess d['first'].append("string"), instead of, d['first'] =
d['first'].append("string")
or you will make d['first'] NoneType
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] output question

2005-11-12 Thread Shi Mu
I got the output as:
>>>
Yes!!! This line will always print
from the code:
try:
fsock = open("c:/TEMP/hello.txt")
except IOError:
print "The file does not exist, exiting gracefully"
print "Yes!!! This line will always print"

it means the code found there is hello.txt,
why it does not show on the output.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] output question

2005-11-12 Thread Shi Mu
On 11/12/05, Liam Clarke <[EMAIL PROTECTED]> wrote:
> Sorry Shi Mu, don't understand the question.
>
> So, you're successfull opening a file. Now, you need to read it
>
> for line in fsock:
>print line
>
> or
>
> fsockData = fsock.read()
>
> or
>
> nextLine = fsock.readline()
>
> Have a play with those.
it works! thanks!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] output question

2005-11-12 Thread Shi Mu
On 11/12/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> > > So, you're successfull opening a file. Now, you need to read it
> > >
> > > for line in fsock:
> > >print line
> > >
> > > Have a play with those.
> > it works! thanks!
>
>
> Hi Shi Mu,
>
> Ok, but let's go back to your original question now, just to make sure
> we've learned something.  You asked earlier:
>
>
> > from the code:
> > try:
> > fsock = open("c:/TEMP/hello.txt")
> > except IOError:
> > print "The file does not exist, exiting gracefully"
> > print "Yes!!! This line will always print"
> >
> > it means the code found there is hello.txt, why it does not show on the
> > output.
>
> Do you know now why the original code didn't do what you want?  Can you
> explain why?  I know it's seems like a silly question, but we are trying
> to avoid a cargo-cult attitude here.  What knowledge did you learn?
>
>
> Again, I'm really not getting any sense whatsoever of knowing why you had
> difficulty here.  Personally, if I run into difficulty with a problem,
> even if I hack out something that "works", I'm not satisified until I
> understand the reasons why I got stuck.  I've been bitten too many times
> by being satisifed by the "right" answer for the wrong reasons.  *grin*
Hi Danny,
I thought if the except situation does not happen, that means the try
section works. At that time, I was a little confused that since the
open statement works, why there is nothing showing. Now i know if i
want to see a file's content on the screen, open statement is not
enough.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] output question

2005-11-12 Thread Shi Mu
On 11/12/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> > > > from the code:
> > > > try:
> > > > fsock = open("c:/TEMP/hello.txt")
> > > > except IOError:
> > > > print "The file does not exist, exiting gracefully"
> > > > print "Yes!!! This line will always print"
>
> > I thought if the except situation does not happen, that means the try
> > section works. At that time, I was a little confused that since the open
> > statement works, why there is nothing showing. Now i know if i want to
> > see a file's content on the screen, open statement is not enough.
>
> Hi Shi Mu,
>
> Ok, that sounds good.  I second Alan's recommendation to go through his
> tutorial; it'll probably help introduce you to more of the programming
> constructs that you're not familiar with yet.
>
> One of the things that we all need to work on is identifying presumptions
> and figuring out how to recognize when those assumptions aren't being
> followed.  If it looks like a function like open() doesn't do what you
> expect, it's a good idea to double check a tutorial that introduces that
> function, just to see if it's the assumption that needs revision.  Are you
> going through a tutorial now?
>
>
> By the way, the exception handling above is probably not necessary; it's
> probably ok to start with:
>
> ###
> fsock = open("c:/TEMP/hello.txt")
> ...
> ###
>
> without the explicit exception handling.  If it fails, it will fail
> gracefully with good debugging output anyway.  If you're going to handle
> an exception by exiting, just let the exception escape out: it'll usually
> maintain very useful information like the stack trace.
>
>
> Best of wishes!
>
>
Thanks a lot for your advice! i am reading the tutorial
(http://www.freenetpages.co.uk/hp/alan.gauld/)now.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] about global definition

2005-11-21 Thread Shi Mu
I have a code here. I understand i can not draw lines without the
global definition of lastX and lastY. But still confused by its use.
when should we use global definition?

from Tkinter import *

root = Tk()

c = Canvas(root, bg='#0e2e0e', height=500, width=1000)
frame = c
lastX=""
lastY=""
def click(event):
 global lastX, lastY
 if lastX != "":
c.create_line(lastX,lastY,event.x,event.y,fill="white")
 lastX = event.x
 lastY = event.y


c.bind('<1>',click)

c.pack()

root.mainloop()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor