Re: SHOCK: WHY None?

2007-09-21 Thread Dave Borne
> else:
> f(i+1,sm+a[i])

Maybe because you are ignoring the return value of the when you recurse...

try this

 else:
 return f(i+1, sm+a[i])


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


Re: Script that Navigates Page needs Javascript Functionality

2007-08-06 Thread Dave Borne
> in order to view the results I need I need python to "navigate to"
> this Javascript link:
> javascript:__doPostBack('ctl00$cpMain$pagerTop','4')  This basically
> translates into "go to page 4."
> I read the posts on this group, and from what I understand, the
> functionality I need is with simplejson? If so, what is the syntax i
> would use to execute that Javascript?
> Or am I completely off base with using simplejson altogether?

I've been meaning to look into this library for interfacing with
Mozilla's spidermonkey javascript engine:
http://wwwsearch.sourceforge.net/python-spidermonkey/

It sounds like it might do what you want, but I'm not sure how much
work it would require.

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


Re: smtp server simulation using Python

2007-06-18 Thread Dave Borne
> I have a (web) development computer w/o an SMTP server and want to test
> form generated e-mail using a dummy SMTP server that delivers the mail
> message to a file, or better yet, to a text editor instead of actually
> sending it.

Here's a quick and dirty script I use this for email testing purposes
- it's windows specific, but that's easy enough to change.

import smtpd, os, time, asyncore

class mailserver(smtpd.SMTPServer):
def __init__(self):
smtpd.SMTPServer.__init__(self, ('',25), None)
print 'Mailsink listening on port 25'

def process_message(self, peer, mailfrom, rcpttos, data):
basepath='c:\\.maildump'

print 'mail from: %s to: %s' %(mailfrom, repr(rcpttos))
for rcpt in rcpttos:
rcpt = rcpt.split('@')[0]
try:
os.mkdir(basepath+'\\'+rcpt)
except OSError:
pass

f =
file(basepath+'\\'+rcpt+'\\'+mailfrom+time.strftime('%Y%m%d%H%M%S'),
'w')
f.write(data)
f.close()

def loop ():
x = mailserver()
try:
asyncore.loop(timeout=2)
except KeyboardInterrupt:
print'interrupt'
x.close()

if __name__=='__main__':
loop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IndentationError: unexpected indent

2007-06-16 Thread Dave Borne
> It would be very helpful when Python would warn you when there are tabs in
> your source.

invoke python with the -t option for warnings about tabs or -tt for errors.

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


Re: FTP/SSL

2007-06-08 Thread Dave Borne
> I'm trying to figure out how to use FTP/SSL (FTPS) - just as a client. Can I
> do this in Python? Is everything I need in ftplib? Where else do I look? And
> - any good newbie references on using FTPS?

Hi, Nancy,
 I'm not sure if ftplib can handle ssh or not, but googling for
"python sftp" turned up this link: http://www.lag.net/paramiko/

It looks like it might do what you want.

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


Re: How do I Extract Attachment from Newsgroup Message

2007-05-31 Thread Dave Borne
> I looked for a solution
> with mimetools (the way I'd approach it for email) but found nothing.
...
> <[EMAIL PROTECTED]>', 'Content-Type: Multipart/Mixed;', '
> boundary="Boundary-00=_A5NJCP3FX6Y5BI3BH890"', 'Date: Thu,
...

Playing with

data = n.article('116431')[3]

and email.message_from_string, there seems to be a problem with the
content type being split up. I was able to get a multipart message by
using

msg = email.message_from_string('\n'.join(data).replace(';\n', ';'))

(and adding an ending boundary to your sample data).
This is a bit hackish and  could cause problems if there are
semicolons inside the message body (no warranties expressed or
implied, etc.)

Hope this helps,
-Dave
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTML Form/Page and Navigation with multiple buttons

2007-05-31 Thread Dave Borne
> How can I identify which button has been pressed.  Do I need a
> separate form for each button and hide all the relevant session fields
> in each form or is there a way of identifying which button has been
> pressed on the page.

Hi, Richard,
 Just give each button (or input) tag a distinct name attribute and a
value attribute. Also make sure the button is inside the form. When
the button is used to submit the form, FieldStorage will return the
name:value pair.

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


Re: Why isn't this query working in python?

2007-05-25 Thread Dave Borne
> I'm trying to run the following query:
...
> member_id=%s AND expire_date > NOW() AND completed=1 AND (product_id

Shouldn't you be using the bind variable '?' instead of '%s' ?
(I'm asking because I'm not entirely sure how the execute command is
doing the substitution)

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


Re: Multi-Page login WITH Cookies (POST Data)

2007-05-18 Thread Dave Borne
> After we are able to get a succussful login, i need a way that i can browse
> my site always including this cookie, like if i went to open up a page, it
> would use the cookie we got from logging in.

You need something like this:
import cookielib,urllib2
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))

more info here: http://docs.python.org/lib/module-urllib2.html
and here: http://docs.python.org/lib/module-cookielib.html

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


Re: A best approach to a creating specified http post body

2007-05-18 Thread Dave Borne
> I need to build a special http post body that consists of :
> name=value +\r\n strings.
> Problem is that depending on operations the number of  name,value
> pairs can increase and decrease.
> Values need to be initialized at runtime, so storing premade text
> files is not possible.

I'm not completely understanding your problems here. Can you explain
why urllib.urlencode wouldn't work?
(http://docs.python.org/lib/module-urllib.html)

Thanks,
-Dave
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How safe is a set of floats?

2007-05-09 Thread Dave Borne
On 4 May 2007 07:21:49 -0700, Thomas Nelson <[EMAIL PROTECTED]> wrote:
> I want to generate all the fractions between 1 and limit (with
> limit>1) in an orderly fashion, without duplicates.

Might I suggest the Stern-Brocot tree
(http://en.wikipedia.org/wiki/Stern-Brocot_tree)
It will eliminate the need for sets as the algorithm gurantees: "Every
positive rational number can be found in this tree exactly once and in
lowest terms". The order will be different than your algorithm,
though.

#An overly simplified fraction class for this example:
class Fraction:
  def __init__ (self, num, den):
self.num = num
self.den = den
  def __repr__ (self):
  return '%(num)d/%(den)d' % self.__dict__

def all_ratios(limit):
seq = [Fraction(1,1), Fraction(limit,1)]
while True:
newseq = seq[:1]
pairs = [seq[x:x+2] for x in range(len(seq)-1)]
for pair in pairs:
#find the mediant value between each pair in the series
newval = Fraction(pair[0].num+pair[1].num, pair[0].den+pair[1].den)
yield newval
newseq.append(newval)
newseq.append(pair[1])
seq = newseq


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


Re: How to replace the last (and only last) character in a string?

2007-05-03 Thread Dave Borne
> Let's suppose
> s='12345 4343 454'
> How can I replace the last '4' character?

If the last '4' will not always be the last character in the string,
you could do:
'X'.join(s.rsplit('4',1))

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


Re: Writing a nice formatted csv file

2007-05-02 Thread Dave Borne
> Whereas what I'd like to get is:
> 1,2,3,
> 10,  20,   30

(without trying this myself first...)
You might  try setting up a csv dialect with a delimiter of ',\t' then
using a reader that can set tab stops for display.

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


Re: Cgi File Upload without Form

2007-04-30 Thread Dave Borne
> Since I want to upload the data programmatically, a form based
> solution is not good.

Karsten,
 Could you explain this statement? When I want to move data to a
server in a CGI environment, a form post is the easiest way I can
think of. What are the specific restrictions making forms a problem?

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


Re: Reading From an Excel Sheet

2007-04-30 Thread Dave Borne
>   I want to write a python script which reads in data from the
> excel sheet .Can any one help out in this ...any help will be
> appreciated.

Try here: http://cheeseshop.python.org/pypi/xlrd/0.5.2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing Log CSV (Efficiently)

2007-04-16 Thread Dave Borne
On 4/16/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> Dave> Python has built in logging support. It's pretty flexible as far
> Dave> as formatting output. I can get a bit complicated to set up, but
> Dave> it will handle traffic well.
>
> Really?  I've found it to be a dog in heavy logging situations.
>
> Skip

Well I've never flogged the logging system very hard, so listen to
Skip here if you're concerned about performance.

I also don't think logging will integrate easily with the built in csv
module.  There's always ','.join(column_list)...

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


Re: Writing Log CSV (Efficiently)

2007-04-16 Thread Dave Borne
On 4/16/07, Robert Rawlins - Think Blue
<[EMAIL PROTECTED]> wrote:
> I'm looking to write a Log file which will be CSV based, and there is a good
> possibility that it'll get quite busy once its up and running, so I'm
> looking for the most efficient way to achieve it. Whilst I'm sure i could do
> something like this.

Python has built in logging support. It's pretty flexible as far as
formatting output. I can get a bit complicated to set up, but it will
handle traffic well.

more info here http://docs.python.org/lib/module-logging.html

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


Re: Formatting a string to be a columned block of text

2006-12-26 Thread Dave Borne
Thanks, Paul. I didn't know about textwrap, that's neat.

Leon,
so in my example change
> data1= [testdata[x:x+colwidth] for x in range(0,len(testdata),colwidth)]
to
> data1 = textwrap.wrap(testdata,colwidth)
> data1 = [x.ljust(colwidth) for x in data1]

oh and I made a mistake that double spaces it. the "print '\n'"
line needs to be either
print ''
or
print '\n',
(with a comma)

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


Re: Formatting a string to be a columned block of text

2006-12-26 Thread Dave Borne
On 26 Dec 2006 04:14:27 -0800, Leon <[EMAIL PROTECTED]> wrote:
> I'm creating a python script that can take a string and print it to the
> screen as a simple multi-columned block of mono-spaced, unhyphenated
> text based on a specified character width and line hight for a column.

Hi, Leon,
 For putting the columns together zip is your friend. Let me lay out an example:

# get your text and strip the newlines:
testdata = file('something').read().replace('\n','')
# set some parameters (these are arbitrary, pick what you need)::
colwidth = 35
colheight = 20
numcol = 2
rowperpage = colheight * numcol
# first split into lines (this ignores word boundaries
# you might want to use somehting more like placid posted for this)
data1 = [testdata[x:x+colwidth] for x in range(0,len(testdata),colwidth)]
# next pad out the list to be an even number of rows - this will give
# a short final column. If you want them balanced you're on your own ;)
data1.extend(['' for x in range(rowsperpage - len(data1) % rowsperpage)])
# then split up the list based on the column length you want:
data2 = [data1[x:x+colheight] for x in range(0,len(data1),colheight)]
# then use zip to transpose the lists into columns
pages = [zip(*data2[x:x+numcol]) for x in range(0,len(data2),numcol)]
# finally unpack this data with some loops and print:
for page in pages:
for line in page:
for column in line:
print ' %s ' % column, #<- note the comma keeps newlines out
print '\n'
print '\f'


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