How does Python call the system/primitive calls ?

2005-04-11 Thread James Yu
Dear Sirs,







I am trying to learn the way Python invokes system or primitive calls.
I tried to locate the function in Python's source that handles such requests, but I found it's like picking a needle in ocean.
Maybe you can let me know where to look for the documents and background readings ?-- James Yu[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

Need help to figure out urllib2.Request()

2008-02-18 Thread James Yu
Hi folks,

I tried to open some web pages with urllib2.Request(url, data, headers), but
it always give me a 404 error.
Eg.
url = 'http://www.whatever.com/somephp.php'
data = {}
data['id'] = account
for i in book2Open:
data['book'] = i
url_data = urllib.urlencode(data)
request = urllib2.Request(url, url_data, headers)
response = urllib2.urlopen(request)
html = response.read()
==> HTTPError: HTTP Error 404: Not Found


However, the page is retrievable when I manually put url and data together.
Eg.
url = 'http://www.whatever.com/somephp.php'
data = {}
data['id'] = account
for i in book2Open:
data['book'] = i
url_data = urllib.urlencode(data)
full_url = url + '?' + url_data
request = urllib2.Request(full_url, "", headers)
response = urllib2.urlopen(request)
html = response.read()
==> works fine

Any idea ?


-- 
This is a UTF-8 formatted mail
---
James C.-C.Yu
-- 
http://mail.python.org/mailman/listinfo/python-list

How to overcome the incomplete download with urllib.urlretrieve ?

2008-02-18 Thread James Yu
This is part of my code that invokes urllib.urlretrieve:

> for i in link2Visit:
> localName = i.split('/')
> i = i.replace(' ', '%20')
> tgtPath = ['d:\\', 'work', 'python', 'grab_n_view']
> localPath = ''
> for j in tgtPath:
> localPath = os.path.join(localPath, j)
> localPath = os.path.join(localPath, localName[-1])
> info = urllib.urlretrieve(i, localPath)
>
link2Visit stores the url to some photos.
After the script finishes running, I got a pile of incomplete JPG files,
each takes only 413 bytes of my disk space.
Did I miss something before using urllib.urlretrieve ?

-- 
This is a UTF-8 formatted mail
---
James C.-C.Yu
-- 
http://mail.python.org/mailman/listinfo/python-list

how to write text into file in ansi encoding ?

2008-02-20 Thread James Yu
I am using *fileHandle = open(srcFile, 'w')* + *fileHandle.write('whatever')
* to put text into a file, and Python uses utf-8 encoding by default.
How do I configure or do anything to make Python writing files in ansi
encoding ?

-- 
This is a UTF-8 formatted mail
---
James C.-C.Yu
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to write text into file in ansi encoding ?

2008-02-20 Thread James Yu
I just figure out the how-to:
# first we open and read in chars as in utf-8 encoding
ansiLine = line.decode('utf-8', 'ignore')
# then writing it into another file in any encoding you like, it's big5 in
my case.
src_handle.write(ansiLine.encode('big5', 'ignore'))

-- 
This is a UTF-8 formatted mail
---
James C.-C.Yu
-- 
http://mail.python.org/mailman/listinfo/python-list

The stange behaviour of Tkinter.Canvas

2008-03-11 Thread James Yu
I tried to update the rectangle on a canvas to get the visual effect of
progressbar.
It works all right if I delete the existing objects (rectangle and text) and
create a new one.
However, if I invoke canvas.itemconfig() to update the existing objects'
options, gui just went nuts.
I am more than happy to receive any help and advise.

My code is provided as follows:

Start of
Code
import Tkinter
import threading
import time

class Progressbar(Tkinter.Frame):
def __init__(self, parent=None, color='white', width=200, height=15):
Tkinter.Frame.__init__(self, parent)
self.pack(expand=Tkinter.YES,
  fill=Tkinter.BOTH)
canv = Tkinter.Canvas(self, bg=color, relief=Tkinter.SUNKEN)
canv.config(width=width, height=height)
canv.pack(side=Tkinter.LEFT, expand=Tkinter.YES, fill=Tkinter.BOTH)
self.canv = canv
self.width = width
self.height = height
progress = -10
rect = canv.create_rectangle(0, 0, 0, height,
 fill='beige')
text = canv.create_text(width/2, height*2/3,
text='0%')
self.rect = rect
self.text = text
self.progress = progress
self.parent = parent
parent.progressbar = self
self.UpdateProgress()

def SetProgress(self, progress=0):
canv = self.canv
width = self.width
height = self.height
rect = self.rect
text = self.text
##canv.delete(rect)
##canv.delete(text)
##rect = canv.create_rectangle(0, 0, progress*width/100, height,
## fill='beige')
##text = canv.create_text(width/2, height*2/3,
##text='%s' %(str(progress)) + '%')
canv.itemconfig(rect, width=width*progress/100)  ##
comment this
canv.itemconfig(text, text='%s' %(str(progress)) + '%')  ##
comment this
##self.rect = rect
##self.text = text

def UpdateProgress(self):
progress = self.progress
progress = progress + 10
self.progress = progress
self.SetProgress(progress)
if progress < 100:
self.after(500, self.UpdateProgress)


if __name__ == '__main__':
root = Tkinter.Tk()
Progressbar(parent=root)
root.mainloop()
End of
Code
uncomment the lines and comment out the itemconfig() lines to see the
difference in gui's behaviours.

Thanks,

-- 
This is a UTF-8 formatted mail
---
James C.-C.Yu
-- 
http://mail.python.org/mailman/listinfo/python-list

How to import custom python file in python server page (psp) ?

2008-03-14 Thread James Yu
Hi folks,

I prepared a python script for dynamically get the absolute paths of the
files in certain folder.
Then I tried to invoke that function from my web server in a .psp file like
this:

  1 
  2 
  3 asdfasdfasdfa
  4 
  5 <%
  6 import glob
  7 import os
  8 *import Helper
*  9
 10 body = ''
 11 top = 'asdfasdfasdfa'
 12 links = {}
 13 *Helper.GetLinks(top=top)
* 14 *paths = Helper.GenLinkPath(links)
* 15 body = paths
 16 %>
 17 <%=body%>
 18 
 19 

However, this is the error message I received when I open the page in a
browser:

> Mod_python error: "PythonHandler mod_python.psp"
>
> Traceback (most recent call last):
>
>   File "/usr/lib/python2.5/site-packages/mod_python/apache.py", line 299,
> in HandlerDispatch
> result = object(req)
>
>   File "/usr/lib/python2.5/site-packages/mod_python/psp.py", line 302, in
> handler
> p.run()
>
>   File "/usr/lib/python2.5/site-packages/mod_python/psp.py", line 213, in
> run
> exec code in global_scope
>
>   File "/var/www/.cyu021/.pic/index.psp", line 8, in
> import Helper
>
> ImportError: No module named Helper


*PS. I put Helper.py and index.psp in the same dir
*
Thanks in advance,
-- 
This is a UTF-8 formatted mail
---
James C.-C.Yu
-- 
http://mail.python.org/mailman/listinfo/python-list

click on hyper link and got 404 error

2008-04-14 Thread James Yu
I am using "mod_python.publisher" to generate my web page that contains
serveral links to local files.
I also replace the path with my domain name like this:

curDir = os.path.dirname(__file__)
link = 'http://' + hostname + '/' + os.path.basename(curDir) + '/'
files = os.listdir(curDir)
for i in files: body = body + '' + i +
'' + ''

However, browser give me 404 error whenever I click on any of the links.
Is there something wrong with how I build my file links ?
Thanks,
-- 
This is a UTF-8 formatted mail
---
James C.-C.Yu
-- 
http://mail.python.org/mailman/listinfo/python-list