Question about Stackless Python

2008-07-13 Thread Fisher
Hi,
  I have some question about Stackless Python.
  What is main tasklet? 
  Usually we call stackless.run() to mainloop all the schedule tasklets. But if 
we just call sometaklet.channel.send(None) in main thread after all the 
necessary tasklets are created,  what would happen? 

 Thanks in advance.--
http://mail.python.org/mailman/listinfo/python-list

Re: what's wrong here? (search script)

2006-07-07 Thread Michael Fisher
Brendan Fay wrote:
> I figured it out.  Is there any way to delete your own posts?
> 
> Brendan Fay wrote:
>> Dear Someone:
>>
>>  I have written a script that accesses the googleAPI through
>> pygoogle and saves each of the ten documents as a .txt file by using a
>> specific function for each respective file type (.doc, .pdf, .html) to
>> convert it to such.  Everything works fine, except that I am trying to
>> make it search and return another result in the event that a file type
>> other than .doc, .pdf, .html, or .txt comes up.  Python is new to me
>> and I'm not sure why my method doesn't work.  I'd really appreciate any
>> advice; here is the relevant code:
>>
>> def searchhelper(words, start=0, max=10):
>>  data = google.doGoogleSearch(words)
>>  objlist = data.results
>>  urllist = map((lambda x: x.URL), objlist)
>>  return urllist
>>
>>
>> def searchhelper2(initwords, urls, counter):
>>  for url in urls:
>>  if findinlink(url, 'pdf'): # all these functions are defined
>> elsewhere,
>>  convertpdf(url)  # fbut they definitely are working 
>> properly
>>  elif findinlink(url, 'htm'):
>>  converthtml(url)
>>  elif findinlink(url, 'txt'):
>>  urllib.urlretrieve(url, parse(x))
>>  elif findinlink(url, 'doc'):
>>  convertdoc(url)
>>  elif not findinlink(url, '.'):
>>  converthtml(url)
>>  else:
>>  urllist = urls[counter + 1:] + searchhelper(initwords, 
>> 11 + counter,
>> 1) # (I'm
>>  searchhelper2(initwords, urllist, counter + 1)# 
>> assuming this is
>> where I have  #
>>   erred; however, I'm not sure)
>>
>>
>> def search(initwords):
>>  urllist = searchhelper(initwords)
>>  searchhelper2(initwords, urllist, 0)
>>
>> Thanks,
>> Brendan
> 

Instead of deleting the previous post, you should post the solution for 
all to learn from.

desNotes
-- 
http://mail.python.org/mailman/listinfo/python-list


OS X install confusion

2007-06-14 Thread John Fisher
Hi Groupies,

I have an Intel Macbook running OS X 10.4.

It came installed with Python 2.3.5. I have since installed MacPython
with version 2.4.4, cool.

When I open a bash terminal session and type python, it brings up
version 2.3.5. If I type IDLE it brings up version 2.4.4. 

My question: what do I have to do to get it to bring up 2.4.4 with the
"python" command?

Thanks for bringing light to my ignorance.

JF
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OS X install confusion

2007-06-14 Thread John Fisher
Ted <[EMAIL PROTECTED]> wrote:

> On Jun 14, 1:31 pm, Kevin Walzer <[EMAIL PROTECTED]> wrote:
> > John Fisher wrote:
> > > Hi Groupies,
> >
> > > I have an Intel Macbook running OS X 10.4.
> >
> > > It came installed with Python 2.3.5. I have since installed MacPython
> > > with version 2.4.4, cool.
> >
> > > When I open a bash terminal session and type python, it brings up
> > > version 2.3.5. If I type IDLE it brings up version 2.4.4.
> >
> > > My question: what do I have to do to get it to bring up 2.4.4 with the
> > > "python" command?
> >
> > > Thanks for bringing light to my ignorance.
> >
> > > JF
> >
> > Sounds like a path problem. Apple's system Python is installed in
> > /usr/bin. Your installation is probably in /usr/local/bin. Edit your
> > profile or use the full path.
> >
> > --
> > Kevin Walzer
> > Code by Kevinhttp://www.codebykevin.com
> 
> The default python on tiger (2.3.5) is sym-linked to /usr/bin/python
> and /usr/bin/pythonw.
> 
> I found it easier to relink to the new installation path. This also
> leaves /usr/bin/python23 and /usr/bin/pythonw23 still linked to the
> original version if you want to quickly check something.
> 
> Cheers,
> Ted

OK, please give a little more information how I can accomplish this
"re-link".

Thanks,

JF
-- 
http://mail.python.org/mailman/listinfo/python-list


converting 64-bit fixed-point to float

2007-07-20 Thread John Fisher
Hi Group,

troubles with converting signed 32.32, little-endian, 2's complement
back to floating point. I have been trying to brew it myself. I am
running Python 2.5 on a Mac. Here is the C-code I have been trying to
leverage:

 double FPuint8ArrayToFPDouble(uint8 *buffer, int startIndex) 
{ 
  uint32 resultDec = 0;
  uint32 resultWh = 0;
  int i;

  for(i = 0; i < 4; i++)
  {
resultDec += (uint32)buffer[startIndex + i] * pow(2, (i*8));
resultWh += (uint32)buffer[startIndex + i + 4] * pow(2, (i*8));
  }

  return ( (double)((int)resultWh) + (double)(resultDec)/4294967296.0 );
}


Here is my version in Python, with some test code built in:

from ctypes import *

def conv64(input):
input1=[0]*8
input1[0]=c_ushort(input[0])
input1[1]=c_ushort(input[1])
input1[2]=c_ushort(input[2])
input1[3]=c_ushort(input[3])
input1[4]=c_ushort(input[4])
input1[5]=c_ushort(input[5])
input1[6]=c_ushort(input[6])
input1[7]=c_ushort(input[7])
#print input1[0].value,
input1[1].value,input1[2].value,input1[3].value
#print
input1[4].value,input1[5].value,input1[6].value,input1[7].value
#print 
resultDec=c_ulong(0)
resultWh=c_ulong(0)
for i in range(4):
dec_c=c_ulong(input1[i].value)
Wh_c=c_ulong(input1[i+4].value)
resultDec.value=resultDec.value+dec_c.value*2**(i*8)
resultWh.value=resultWh.value+Wh_c.value*2**(i*8)
   conval=float(int(resultWh.value))+float(resultDec.value)/4294967296.0
#print conval
return conval
#tabs got messed up bringing this into MacSoup

#these are 64-bit fixed point format (signed 32.32, little-endian, 2's
complement)
#should be -1
conv64_0=[0, 0, 0, 255, 255, 255, 255, 255]
#should be 0
conv64_1=[0, 0, 0, 0, 0, 0, 0, 0]
#should be 0.2
conv64_1_2=[51, 51, 51, 51, 0, 0, 0, 0]
#should be 1
conv64_2=[0, 0, 0, 0, 1, 0, 0, 0]
#should be 2
conv64_3=[0, 0, 0, 0, 2, 0, 0, 0]
#should be 298.15
conv64_4=[102, 102, 102, 38, 42, 1, 0, 0]
#should be -0.2
conv64_5=[205,204,204,204,255,255,255,255]
output0=conv64(conv64_0)
print "output should be -1 is "+str(output0)
output1=conv64(conv64_1)
print "output should be 0 is "+str(output1)
output1_2=conv64(conv64_1_2)
print "output should be 0.2 is "+str(output1_2)
output2=conv64(conv64_2)
print "output should be 1 is "+str(output2)
output3=conv64(conv64_3)
print "output should be 2 is "+str(output3)
output4=conv64(conv64_4)
print "output should be 298.15 is "+str(output4)
output5=conv64(conv64_5)
print "output should be -0.2 is "+str(output5)



Finally, here is the output I get from my code:

>>> 
output should be -1 is 4294967296.0
output should be 0 is 0.0
output should be 0.2 is 0.1953
output should be 1 is 1.0
output should be 2 is 2.0
output should be 298.15 is 298.15
output should be -0.2 is 4294967295.8

Thanks for any light you can shed on my ignorance.

wave_man
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: converting 64-bit fixed-point to float

2007-07-21 Thread John Fisher
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> On Jul 20, 5:59 pm, [EMAIL PROTECTED] (John Fisher) wrote:
> > Hi Group,
> >
> > troubles with converting signed 32.32, little-endian, 2's complement
> > back to floating point. I have been trying to brew it myself. I am
> > running Python 2.5 on a Mac. Here is the C-code I have been trying to
> > leverage:
> >
> >  double FPuint8ArrayToFPDouble(uint8 *buffer, int startIndex)
> > {
> >   uint32 resultDec = 0;
> >   uint32 resultWh = 0;
> >   int i;
> >
> >   for(i = 0; i < 4; i++)
> >   {
> > resultDec += (uint32)buffer[startIndex + i] * pow(2, (i*8));
> > resultWh += (uint32)buffer[startIndex + i + 4] * pow(2, (i*8));
> >   }
> >
> >   return ( (double)((int)resultWh) + (double)(resultDec)/4294967296.0 );
> >
> > }
> 
> 
> There are a few problem spots in that C code.  I tend to
> think that it "works" because you're on a system that has
> 4-byte int and CHAR_BIT == 8.  When the most-significant-bit (MSB)
> of resultWh is 1, then casting to int makes that a negative
> value (i.e., MSB == the sign bit).
> 
> I presume that somewhere you include  (from C99)
> and that uint32 is really uint32_t, etc.  For that to be
> portable, you should probably cast to int32_t?
> 
> #include 
> #include 
> #include 
> 
> double arr2dbl (uint8_t *buffer, int startIndex)
> {
> uint32_t decimal = 0;
> uint32_t whole = 0;
> size_t i;
> for (i = 0; i < 4; ++i)
> {
> decimal += (buffer[startIndex + i] << (i*8));
> whole += (buffer[startIndex + i + 4] << (i*8));
> }
> return (int32_t)whole + (decimal/(UINT32_MAX+1.0));
> }
> 
> int main (void)
> {
> uint8_t arr[7][8] = {
> {0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff},
> {0, 0, 0, 0, 0, 0, 0, 0},
> {51, 51, 51, 51, 0, 0, 0, 0},
> {0, 0, 0, 0, 1, 0, 0, 0},
> {0, 0, 0, 0, 2, 0, 0, 0},
> {102, 102, 102, 38, 42, 1, 0, 0 },
> {205, 204, 204, 204, 0xff, 0xff, 0xff, 0xff}};
> size_t i;
> double result;
> for (i = 0; i < sizeof arr/sizeof arr[0]; ++i)
> {
> result = arr2dbl(arr[i], 0);
> printf("%f\n", result);
> }
> return 0;
> }
> 
> 
> 
> 
> 
> 
> 
> > Here is my version in Python, with some test code built in:
> >
> > from ctypes import *
> >
> > def conv64(input):
> > input1=[0]*8
> > input1[0]=c_ushort(input[0])
> > input1[1]=c_ushort(input[1])
> > input1[2]=c_ushort(input[2])
> > input1[3]=c_ushort(input[3])
> > input1[4]=c_ushort(input[4])
> > input1[5]=c_ushort(input[5])
> > input1[6]=c_ushort(input[6])
> > input1[7]=c_ushort(input[7])
> > #print input1[0].value,
> > input1[1].value,input1[2].value,input1[3].value
> > #print
> > input1[4].value,input1[5].value,input1[6].value,input1[7].value
> > #print
> > resultDec=c_ulong(0)
> > resultWh=c_ulong(0)
> > for i in range(4):
> > dec_c=c_ulong(input1[i].value)
> > Wh_c=c_ulong(input1[i+4].value)
> > resultDec.value=resultDec.value+dec_c.value*2**(i*8)
> > resultWh.value=resultWh.value+Wh_c.value*2**(i*8)
> >conval=float(int(resultWh.value))+float(resultDec.value)/4294967296.0
> > #print conval
> > return conval
> > #tabs got messed up bringing this into MacSoup
> 
> 
> (snipped)
> 
> 
> >
> > Finally, here is the output I get from my code:
> >
> >
> >
> > output should be -1 is 4294967296.0
> > output should be 0 is 0.0
> > output should be 0.2 is 0.1953
> > output should be 1 is 1.0
> > output should be 2 is 2.0
> > output should be 298.15 is 298.15
> > output should be -0.2 is 4294967295.8
> >
> > Thanks for any light you can shed on my ignorance.
> >
> > wave_man
> 
> 
> This is my translation:
> 
> from ctypes import *
> 
> def conv64(input):
> input1 = [c_uint8(item) for item in input]
> dec = c_uint32(0)
> whl = c_uint32(0)
> for i in xrange(4):
> dec.value += (input1[i].value << (i*8))
> whl.value += (input1[i+4].value << (i*8))
> cast_whl_to_int = c_int32(whl.value)
> return float(cast_whl_to_int.value + dec.value/4294967296.0)
> 
> 
> for arr in [[0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff],
> [0, 0, 0, 0, 0, 0, 0, 0],
> [51, 51, 51, 51, 0, 0, 0, 0],
> [0, 0, 0, 0, 1, 0, 0, 0],
> [0, 0, 0, 0, 2, 0, 0, 0],
> [102, 102, 102, 38, 42, 1, 0, 0],
> [205,204,204,204,255,255,255,255]]:
> print "%f" % conv64(arr)
> 
> 
> 
> However, I've not looked deeply into ctypes so I
> don't know if c_int32 is really C's int, or int32_t, or ???
> 
> --
> Hope this helps,
> Steven

Actually this was very helpful, thanks. 

Rgds,

wave_man
-- 
http://mail.python.org/mailman/listinfo/python-list


(sort of) deterministic timing in Python

2007-08-13 Thread John Fisher
I am working on a framework for data acquisition in Python 2.5, am
trying to get a structure going more like this:


mark start time
start event
event finishes
count time until next interval
start second event…

rather than this:

start event
event finishes
sleep for interval
start second event

Do you see the difference? I get a true fixed interval from the first,
including the time to accomplish the event task(s). In the second case,
the sleep just gets tacked on at the end of the events, not very
deterministic (timing-wise).

So how do I accomplish this in Python with a minimum of labour?

Thanks for any light you can shed on my darkness...

wave_man
-- 
http://mail.python.org/mailman/listinfo/python-list

Structure of packages

2008-01-09 Thread Ben Fisher
I am trying to learn the best way to do intra-package references. My
package looks like this:

PackageName
__init__.py
/a
__init__.py
   a.py
...
/b
__init__.py
...
/c
__init__.py
...
/d
__init__.py
...
I have layered the dependencies so that a depends on b, b depends on
c, and c depends on d. There are no circular references.

Now I would like to be able to refer to the subpackage b from inside
the subpackage a. In effect, I would like to say "from '../b' import
*"

I had thought that "from PackageName.b import *" would work. This
works for a file in the directory /PackageName, but not for a file in
the directory /PackageName/a.  It's like when you are running a Python
file in the directory /PackageName/a it doesn't know about PackageName
- No module named "PackageName".

Is there a solution to this, or a better way to structure the directories?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another dumb scope question for a closure.

2008-01-09 Thread Ben Fisher
One way to get this to work is:

def inc(jj):
def dummy(jj = jj):
jj = jj + 1
return jj
return dummy

h = inc(33)
print h()

It's not very pretty though, especially when you have many variables
you want to have in the inner scope.

-Ben

On 1/9/08, Mike Meyer <[EMAIL PROTECTED]> wrote:
> On Wed, 9 Jan 2008 13:47:30 -0500 (EST) "Steven W. Orr" <[EMAIL PROTECTED]> 
> wrote:
>
> > So sorry because I know I'm doing something wrong.
> >
> > 574 > cat c2.py
> > #! /usr/local/bin/python2.4
> >
> > def inc(jj):
> >  def dummy():
> >  jj = jj + 1
> >  return jj
> >  return dummy
> >
> > h = inc(33)
> > print 'h() = ', h()
> > 575 > c2.py
> > h() =
> > Traceback (most recent call last):
> >File "./c2.py", line 10, in ?
> >  print 'h() = ', h()
> >File "./c2.py", line 5, in dummy
> >  jj = jj + 1
> > UnboundLocalError: local variable 'jj' referenced before assignment
> >
> > I could have sworn I was allowed to do this. How do I fix it?
>
> Nope. This is one of the things that makes lisper's complain that
> Python doesn't have "real closures": you can't rebind names outside
> your own scope (except via global, which won't work here).
>
> Using a class is the canonical way to hold state. However, any of the
> standard hacks for working around binding issues work. For instance:
>
> >>> def inc(jj):
> ...   def dummy():
> ... box[0] = box[0] + 1
> ... return box[0]
> ...   box = [jj]
> ...   return dummy
> ...
> >>> h = inc(33)
> >>> h()
> 34
>
>
> --
> Mike Meyer <[EMAIL PROTECTED]>  
> http://www.mired.org/consulting.html
> Independent Network/Unix/Perforce consultant, email for more information.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: print >> to a derived file

2008-01-15 Thread Ben Fisher
This might have something to do with the class being derived from file.

I've written it so that it doesn't derive from file, and it works.

class File_and_console():
def __init__(self, *args):
self.fileobj = open(*args)
def write(self, s):
self.fileobj.write(s)
print s,

f = File_and_console('testout.tmp','w')
f.write('hello')
print >>f,'hello',


On 1/15/08, iu2 <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm trying to write data to both a file and the console, so I did:
>
> class File_and_console(file):
>def write(self, s):
>file.write(self, s)
>print s,
>
> >>> f = File_and_console('1.txt', 'w')
> >>> f.write('hello')
> hello
> >>> print >>f, 'world'
> >>>
>
> the 'write' method works, but 'print >>' doesn't, it writes only to
> the file. It doesn't actually call File_and_console.write
>
> Why? How can I fix it?
> Thanks
> iu2
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


keyboard "interrupt"

2008-03-18 Thread John Fisher
Hi Group,

I have been absent a while, mainly because I have been getting better at
figuring out my own Python problems. But not this one...

I have a timed loop performing certain tasks until a total period of
time has elapsed. I would like to be able to interrupt the loop or set
various flags during execution via keyboard input. raw_input seems to
just stop everything cold. I want the ability to just sacn the keyboard
buffer and see if something is there, then proceed normally in the loop
if there is no input in the buffer. Make sense? Totally easy? Let me
know...

wave_man
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: keyboard "interrupt"

2008-03-27 Thread John Fisher
Arnaud Delobelle <[EMAIL PROTECTED]> wrote:

> John Fisher wrote:
> > Hi Group,
> 
> Hi John
> 
> > I have been absent a while, mainly because I have been getting better at
> > figuring out my own Python problems. But not this one...
> >
> > I have a timed loop performing certain tasks until a total period of
> > time has elapsed. I would like to be able to interrupt the loop or set
> > various flags during execution via keyboard input. raw_input seems to
> > just stop everything cold. I want the ability to just sacn the keyboard
> > buffer and see if something is there, then proceed normally in the loop
> > if there is no input in the buffer. Make sense? Totally easy? Let me
> > know...
> 
> If you are on a UNIX platform, you can use the curses module.
> 
> http://docs.python.org/lib/module-curses.html
> 
> There is a link there to a tutorial, but it seems to be broken.  A
> quick search finds it there:
> 
> http://www.amk.ca/python/howto/curses/
> 
> HTH
> 
> --
> Arnaud
Thanks for your reply Arnaud. I am using an Intel mac, running Leopard
10.5.2, so that's Posix I warrant. I will check this out.

JF
-- 
http://mail.python.org/mailman/listinfo/python-list


Problems with running Python CGI Scripts

2008-09-03 Thread Edward FISHER
Hey guys.
Im having problems running a python cgi. Im using the example code from:
http://www.python.org/doc/essays/pp...east/sld041.htm as writen by Van Rossum 
himself

I can get the script to run the python script but all that happens is that the 
black python box appears then disapears, the html that the python scripts 
should generate is never output.

My html is:



A Typical HTML form

A Typical HTML form
*Test CGI script and HTML to
test server for correct running of python cgi scripting

Your First Name:

Your Last Name: 
Click here to submit form: 



This is calling the pyc file of the python script. I dont understand why if i 
call the py file itself all i get returned is the plain text of the python file.

The python script is:

#!/usr/local/bin/python
import cgi

def main():
print "Content-type: text/html\n"
form = cgi.FieldStorage()
if form.has_key("firstname") and form["firstname"].value != "":
#if form has an object called firstname and the value is not an empty string
print "Hello", form["firstname"].value, ""
else:
print "Error! Please enter first name."

main()

If you need to see what happens then follow this link:
http://www.fisherphotographics.co.uk/testhtml1.htm

The python file has come directly from the example so i must me doing something 
wrong. I have all the correct permissions etc
Thanks very much
Ed Fisher 
--
http://mail.python.org/mailman/listinfo/python-list