Re: Can't get around "IndexError: list index out of range"

2006-10-06 Thread erikcw
I ended up using len(sys.argv) > 1 for this particular problem.  But I
think slicing is closer to the tool I was looking for.

I found a.has_key(k) or "k in a" for dictionaries - but haven't found
anything similar for lists.  Does it exist?

I guess my example from php would technically be a dictionary in python
and not a list, it would nice to be able to quickly tell if a list key
existed or not.

Thanks!
Erik


Steve Holden wrote:
> [EMAIL PROTECTED] wrote:
> > Hi all,
> >
> > I'm sorry about the newbie question, but I've been searching all
> > afternoon and can't find the answer!
> >
> > I'm trying to get this bit of code to work without triggering the
> > IndexError.
> >
> > import shutil, os, sys
> >
> > if sys.argv[1] != None:
> > ver = sys.argv[1]
> > else:
> > ver = '2.14'
> >
> > Of course, whenever I run it, I get list index out of range.
> >
> > I'm coming from the php world where I can do:
> > if $_GET['var'] != Null {
> >   $ver = $_GET['var'];
> > } else {
> >   $ver = '2.14';
> > }
> >
> > Can anyone tell me how to make this work in python?
> >
> Well all the advice you've had so far seems good, but of course the
> simplest way is just to test the length of the sequence before you try
> to address its second element:
>
> if len(sys.argv) > 1:
>ver = sys.argv[1]
> else:
>ver = "2.14"
>
> regards
>   Steve
> --
> Steve Holden   +44 150 684 7255  +1 800 494 3119
> Holden Web LLC/Ltd  http://www.holdenweb.com
> Skype: holdenweb   http://holdenweb.blogspot.com
> Recent Ramblings http://del.icio.us/steve.holden

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


Dive Into Java?

2006-10-08 Thread erikcw
DiveIntoPython.org was the first book I read on python, and I really
got a lot out of it.  I need to start learning Java (to maintain a
project I've inherited), and was wondering if anyone knew of any
similar books for Java?

Maybe once I know my way around the language, I can sneak Jython in...
:-)

Thanks!
Erik

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


Having trouble with file modes

2006-11-03 Thread erikcw
Hi all,

I've created a script that reads in a file, replaces some data (regex),
then writes the new data back to the file.

At first I was convinced that "w+" was the tool for the job.  But now
I'm finding that the contents of the file are deleted (so I can't read
the data in).

f = open('_i_defines.php', 'w+')
data = f.read()
#patterns removed for brevity.
patterns = [(tuples)(blah blah)]

#loop through patterns list and find/replace data
for o, r in patterns:
data = data.replace(o, r)
print "Replaced %s with %s" % (o, r)
f.write(data)
f.close()

This results in an empty file.  All of the modes I've tried either
produce an empty file or append the data onto the end of the file.  How
Can I:

Open
Read
Truncate
...process data
Write
Close

Do I need to open and close the file twice? (once in 'r' and then again
in 'w')

Thanks!
Erik

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


Re: Having trouble with file modes

2006-11-03 Thread erikcw
To make it write over the data, I ended up adding, which seems to work
fine.

f = open('_i_defines.php', 'w+')
data = f.read()
f.seek(0)
f.truncate(0)
...process data, write file, close...

Now that I look at it, I could probably take out the f.seek().

Thanks for your help!


On Nov 3, 4:00 pm, "martdi" <[EMAIL PROTECTED]> wrote:
> > At first I was convinced that "w+" was the tool for the job.  But now
> > I'm finding that the contents of the file are deleted (so I can't read
> > the data in).Possible File Modes:
> a : Append -- Add data at the end of the file
> r : Read -- Read from the file
> w : write -- Flush contents of the file and put data in it
> r+ : read and write -- Make changes to the file
> any of the above modes with b added at the end of the mode sets the
> file in binary mode
>
> you might want to check section 7.2 fromhttp://docs.python.org/tut/node9.html
>
> Ofen when doing file operations you might prefer to read the file to
> modify, make changes in memory then save back in an other file, so you
> can make sure the changes are ok before you replace the old one

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


Programmatically finding "significant" data points

2006-11-14 Thread erikcw
Hi all,

I have a collection of ordered numerical data in a list.  The numbers
when plotted on a line chart make a low-high-low-high-high-low (random)
pattern.  I need an algorithm to extract the "significant" high and low
points from this data.

Here is some sample data:
data = [0.10, 0.50, 0.60, 0.40, 0.39, 0.50, 1.00, 0.80, 0.60, 1.20,
1.10, 1.30, 1.40, 1.50, 1.05, 1.20, 0.90, 0.70, 0.80, 0.40, 0.45, 0.35,
0.10]

In this data, some of the significant points include:
data[0]
data[2]
data[4]
data[6]
data[8]
data[9]
data[13]
data[14]


How do I sort through this data and pull out these points of
significance?

Thanks for your help!

Erik

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


SMTPlib Sender Refused?

2007-06-02 Thread erikcw
Hi,

I'm trying to send an email message with python, and I'm getting this
error:

Traceback (most recent call last):
  File "wa.py", line 430, in ?
main()
  File "wa.py", line 425, in main
smtp.sendmail(fromaddr, to, msg.encode('utf-8'))
  File "/usr/local/lib/python2.4/smtplib.py", line 680, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (503, 'sender already given',
'[EMAIL PROTECTED]')

What is causing this?

Thanks!
Erik

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


Is 1 large Regex faster than 3 smaller ones?

2007-06-03 Thread erikcw
Hi,

I need to match 3 small strings in a small text file (about 200 words
of text).

Would it be faster to write 1 compiled regex that matches all 3
substrings in one go, or to use 3 separate regular expressions to do
the same job?

Thanks!
Erik

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


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

2007-06-25 Thread erikcw
On May 27, 11:06 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
>
> > On May 27, 2007, at 4:01 PM, Steve Holden wrote:
>
> >>erikcwwrote:
> >>> On May 26, 8:21 pm, John Machin <[EMAIL PROTECTED]> wrote:
> >>>> On May 27, 5:25 am,erikcw<[EMAIL PROTECTED]> wrote:
>
> >>>>> On May 25, 11:28 am, Carsten Haese <[EMAIL PROTECTED]> wrote:
> >>>>>> On Fri, 2007-05-25 at 09:51 -0500, Dave Borne wrote:
> >>>>>>>> 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' ?
> >>>>>> The parameter placeholder for MySQLdb is, indeed and
> >>>>>> unfortunately, %s.
> >>>>>> The OP is using parameter substitution correctly, though in an
> >>>>>> obfuscated fashion. 'sql' is a misnamed tuple containing both the
> >>>>>> query
> >>>>>> string *and* the parameters, which is being unpacked with '*' into
> >>>>>> two
> >>>>>> arguments to the execute call.
> >>>>>> The only problem I see is that the parameters should be a
> >>>>>> sequence, i.e.
> >>>>>> (self.uid,) instead of just (self.uid).
> >>>>>> HTH,
> >>>>>> --
> >>>>>> Carsten Haesehttp://informixdb.sourceforge.net
> >>>>> I tried adding the comma to make it a sequence - but now change.
> >>>>> ('SELECT payment_id FROM amember_payments WHERE member_id=%s AND
> >>>>> expire_date > NOW() AND completed=1 AND (product_id >11 AND product_id
> >>>>> <21)', (1608L,))
> >>>>> ()
> >>>>> What else could it be?
> >>>> Possibly a type mismatch. How is member_id declared in the CREATE
> >>>> TABLE? For diagnostic purposes, try passing in (1608,) and ('1608',).
>
> >>> Here is a copy of the table schema and the first 2 rows.
>
> >>> -- phpMyAdmin SQL Dump
> >>> -- version 2.9.0.2
> >>> --http://www.phpmyadmin.net
> >>> --
> >>> -- Host: localhost
> >>> -- Generation Time: May 27, 2007 at 11:29 AM
> >>> -- Server version: 5.0.27
> >>> -- PHP Version: 4.4.2
> >>> --
> >>> -- Database: `lybp_lybp`
> >>> --
>
> >>> -- 
>
> >>> --
> >>> -- Table structure for table `amember_payments`
> >>> --
>
> >>> CREATE TABLE `amember_payments` (
> >>>   `payment_id` int(11) NOT NULL auto_increment,
> >>>   `member_id` int(11) NOT NULL default '0',
> >>>   `product_id` int(11) NOT NULL default '0',
> >>>   `begin_date` date NOT NULL default '-00-00',
> >>>   `expire_date` date NOT NULL default '-00-00',
> >>>   `paysys_id` varchar(32) NOT NULL default '',
> >>>   `receipt_id` varchar(32) NOT NULL default '',
> >>>   `amount` decimal(12,2) NOT NULL default '0.00',
> >>>   `completed` smallint(6) default '0',
> >>>   `remote_addr` varchar(15) NOT NULL default '',
> >>>   `data` text,
> >>>   `time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
> >>> CURRENT_TIMESTAMP,
> >>>   `aff_id` int(11) NOT NULL default '0',
> >>>   `payer_id` varchar(255) NOT NULL default '',
> >>>   `coupon_id` int(11) NOT NULL default '0',
> >>>   `tm_added` datetime NOT NULL default '-00-00 00:00:00',
> >>>   `tm_completed` datetime default NULL,
> >>>   `tax_amount` decimal(12,2) NOT NULL default '0.00',
> >>>   PRIMARY KEY  (`payment_id`),
> >>>   KEY `member_id` (`member_id`),
> >>>   KEY `payer_id` (`payer_id`),
> >>>   KEY `coupon_id` (`coupon_id`),
> >>>   KEY `tm_added` (`tm_added`,`product_id`),
> >>>   KEY `tm_completed` (`tm_completed`,`product_id`)
> >>> ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11020 ;
>
> >>> --
> >&g

Creating Graphs for the Web

2007-05-23 Thread erikcw
Hi,

I'm working on a django powered website, and need to dynamically
generate some graphs (bar, pie, and line) from users' data stored in
MySQL.

Can anyone recommend a good library I can use for this?

Thanks!
Erik

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


Why isn't this query working in python?

2007-05-25 Thread erikcw
Hi all,

I'm trying to run the following query:

amember_db = MySQLdb.connect(host="localhost", user="**",
passwd="*", db="***")
# create a cursor
self.amember_cursor = amember_db.cursor()
# execute SQL statement
sql = """SELECT payment_id FROM amember_payments WHERE
member_id=%s AND expire_date > NOW() AND completed=1 AND (product_id
>11 AND product_id <21)""", (self.uid)
print sql
self.amember_cursor.execute(*sql)
amember_result = self.cursor.fetchall()
print amember_result

When I manually run the SQL query in mysql, I get a result, but for
some reason I get an empty result in python.  Here are some notes that
may be of importance.

-I have 2 connections open to MySQL in this script (they are both in
seperate objects of course)
- self.uid = 1972L
-print amember_result = ()

Any ideas?

Thanks!
Erik

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


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

2007-05-25 Thread erikcw
On May 25, 10:51 am, "Dave Borne" <[EMAIL PROTECTED]> wrote:
> > 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

Hi Dave,

I'm not sure.  I've been using this format for all of my other queries
without issue.  What would the query look like with the bind variable
instead?

Erik

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


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

2007-05-26 Thread erikcw
On May 25, 11:28 am, Carsten Haese <[EMAIL PROTECTED]> wrote:
> On Fri, 2007-05-25 at 09:51 -0500, Dave Borne wrote:
> > > 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' ?
>
> The parameter placeholder for MySQLdb is, indeed and unfortunately, %s.
> The OP is using parameter substitution correctly, though in an
> obfuscated fashion. 'sql' is a misnamed tuple containing both the query
> string *and* the parameters, which is being unpacked with '*' into two
> arguments to the execute call.
>
> The only problem I see is that the parameters should be a sequence, i.e.
> (self.uid,) instead of just (self.uid).
>
> HTH,
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

I tried adding the comma to make it a sequence - but now change.

('SELECT payment_id FROM amember_payments WHERE member_id=%s AND
expire_date > NOW() AND completed=1 AND (product_id >11 AND product_id
<21)', (1608L,))
()

What else could it be?

Thanks!
Erik

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


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

2007-05-27 Thread erikcw
On May 26, 8:21 pm, John Machin <[EMAIL PROTECTED]> wrote:
> On May 27, 5:25 am, erikcw <[EMAIL PROTECTED]> wrote:
>
>
>
> > On May 25, 11:28 am, Carsten Haese <[EMAIL PROTECTED]> wrote:
>
> > > On Fri, 2007-05-25 at 09:51 -0500, Dave Borne wrote:
> > > > > 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' ?
>
> > > The parameter placeholder for MySQLdb is, indeed and unfortunately, %s.
> > > The OP is using parameter substitution correctly, though in an
> > > obfuscated fashion. 'sql' is a misnamed tuple containing both the query
> > > string *and* the parameters, which is being unpacked with '*' into two
> > > arguments to the execute call.
>
> > > The only problem I see is that the parameters should be a sequence, i.e.
> > > (self.uid,) instead of just (self.uid).
>
> > > HTH,
>
> > > --
> > > Carsten Haesehttp://informixdb.sourceforge.net
>
> > I tried adding the comma to make it a sequence - but now change.
>
> > ('SELECT payment_id FROM amember_payments WHERE member_id=%s AND
> > expire_date > NOW() AND completed=1 AND (product_id >11 AND product_id
> > <21)', (1608L,))
> > ()
>
> > What else could it be?
>
> Possibly a type mismatch. How is member_id declared in the CREATE
> TABLE? For diagnostic purposes, try passing in (1608,) and ('1608',).

Here is a copy of the table schema and the first 2 rows.

-- phpMyAdmin SQL Dump
-- version 2.9.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 27, 2007 at 11:29 AM
-- Server version: 5.0.27
-- PHP Version: 4.4.2
--
-- Database: `lybp_lybp`
--

-- 

--
-- Table structure for table `amember_payments`
--

CREATE TABLE `amember_payments` (
  `payment_id` int(11) NOT NULL auto_increment,
  `member_id` int(11) NOT NULL default '0',
  `product_id` int(11) NOT NULL default '0',
  `begin_date` date NOT NULL default '-00-00',
  `expire_date` date NOT NULL default '-00-00',
  `paysys_id` varchar(32) NOT NULL default '',
  `receipt_id` varchar(32) NOT NULL default '',
  `amount` decimal(12,2) NOT NULL default '0.00',
  `completed` smallint(6) default '0',
  `remote_addr` varchar(15) NOT NULL default '',
  `data` text,
  `time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
  `aff_id` int(11) NOT NULL default '0',
  `payer_id` varchar(255) NOT NULL default '',
  `coupon_id` int(11) NOT NULL default '0',
  `tm_added` datetime NOT NULL default '-00-00 00:00:00',
  `tm_completed` datetime default NULL,
  `tax_amount` decimal(12,2) NOT NULL default '0.00',
  PRIMARY KEY  (`payment_id`),
  KEY `member_id` (`member_id`),
  KEY `payer_id` (`payer_id`),
  KEY `coupon_id` (`coupon_id`),
  KEY `tm_added` (`tm_added`,`product_id`),
  KEY `tm_completed` (`tm_completed`,`product_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11020 ;

--
-- Dumping data for table `amember_payments`
--

INSERT INTO `amember_payments` VALUES (423, 107, 1, '2004-10-01',
'2004-10-21', 'authorize_aim', '5687944', 3.95, 1, '', NULL,
'2004-11-30 19:21:43', 0, '', 0, '2004-11-30 19:21:43', '2004-11-30
19:21:43', 0.00);
INSERT INTO `amember_payments` VALUES (422, 107, 1, '2004-10-22',
'2004-11-21', 'authorize_aim', '5873225', 9.95, 1, '', NULL,
'2004-11-30 19:22:18', 0, '', 0, '2004-11-30 19:20:13', '2004-11-30
19:20:13', 0.00);

Thanks for your help!
Erik

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


Can python create a dictionary from a list comprehension?

2007-05-27 Thread erikcw
Hi,

I'm trying to turn o list of objects into a dictionary using a list
comprehension.

Something like

entries = {}
 [entries[int(d.date.strftime('%m'))] = d.id] for d in links]

I keep getting errors when I try to do it.  Is it possible?  Do
dictionary objects have a method equivalent to [].append?  Maybe a
lambda?

Thanks for your help!
Erik

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


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

2007-05-27 Thread erikcw
On May 27, 4:01 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> erikcw wrote:
> > On May 26, 8:21 pm, John Machin <[EMAIL PROTECTED]> wrote:
> >> On May 27, 5:25 am, erikcw <[EMAIL PROTECTED]> wrote:
>
> >>> On May 25, 11:28 am, Carsten Haese <[EMAIL PROTECTED]> wrote:
> >>>> On Fri, 2007-05-25 at 09:51 -0500, Dave Borne wrote:
> >>>>>> 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' ?
> >>>> The parameter placeholder for MySQLdb is, indeed and unfortunately, %s.
> >>>> The OP is using parameter substitution correctly, though in an
> >>>> obfuscated fashion. 'sql' is a misnamed tuple containing both the query
> >>>> string *and* the parameters, which is being unpacked with '*' into two
> >>>> arguments to the execute call.
> >>>> The only problem I see is that the parameters should be a sequence, i.e.
> >>>> (self.uid,) instead of just (self.uid).
> >>>> HTH,
> >>>> --
> >>>> Carsten Haesehttp://informixdb.sourceforge.net
> >>> I tried adding the comma to make it a sequence - but now change.
> >>> ('SELECT payment_id FROM amember_payments WHERE member_id=%s AND
> >>> expire_date > NOW() AND completed=1 AND (product_id >11 AND product_id
> >>> <21)', (1608L,))
> >>> ()
> >>> What else could it be?
> >> Possibly a type mismatch. How is member_id declared in the CREATE
> >> TABLE? For diagnostic purposes, try passing in (1608,) and ('1608',).
>
> > Here is a copy of the table schema and the first 2 rows.
>
> > -- phpMyAdmin SQL Dump
> > -- version 2.9.0.2
> > --http://www.phpmyadmin.net
> > --
> > -- Host: localhost
> > -- Generation Time: May 27, 2007 at 11:29 AM
> > -- Server version: 5.0.27
> > -- PHP Version: 4.4.2
> > --
> > -- Database: `lybp_lybp`
> > --
>
> > -- 
>
> > --
> > -- Table structure for table `amember_payments`
> > --
>
> > CREATE TABLE `amember_payments` (
> >   `payment_id` int(11) NOT NULL auto_increment,
> >   `member_id` int(11) NOT NULL default '0',
> >   `product_id` int(11) NOT NULL default '0',
> >   `begin_date` date NOT NULL default '-00-00',
> >   `expire_date` date NOT NULL default '-00-00',
> >   `paysys_id` varchar(32) NOT NULL default '',
> >   `receipt_id` varchar(32) NOT NULL default '',
> >   `amount` decimal(12,2) NOT NULL default '0.00',
> >   `completed` smallint(6) default '0',
> >   `remote_addr` varchar(15) NOT NULL default '',
> >   `data` text,
> >   `time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
> > CURRENT_TIMESTAMP,
> >   `aff_id` int(11) NOT NULL default '0',
> >   `payer_id` varchar(255) NOT NULL default '',
> >   `coupon_id` int(11) NOT NULL default '0',
> >   `tm_added` datetime NOT NULL default '-00-00 00:00:00',
> >   `tm_completed` datetime default NULL,
> >   `tax_amount` decimal(12,2) NOT NULL default '0.00',
> >   PRIMARY KEY  (`payment_id`),
> >   KEY `member_id` (`member_id`),
> >   KEY `payer_id` (`payer_id`),
> >   KEY `coupon_id` (`coupon_id`),
> >   KEY `tm_added` (`tm_added`,`product_id`),
> >   KEY `tm_completed` (`tm_completed`,`product_id`)
> > ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11020 ;
>
> > --
> > -- Dumping data for table `amember_payments`
> > --
>
> > INSERT INTO `amember_payments` VALUES (423, 107, 1, '2004-10-01',
> > '2004-10-21', 'authorize_aim', '5687944', 3.95, 1, '', NULL,
> > '2004-11-30 19:21:43', 0, '', 0, '2004-11-30 19:21:43', '2004-11-30
> > 19:21:43', 0.00);
> > INSERT INTO `amember_payments` VALUES (422, 107, 1, '2004-10-22',
> > '2004-11-21', 'authorize_aim', '5873225', 9.95, 1, '', NULL,
> > '2004-11-30 19:22:18', 0, '', 0, '2004-11-30 19:20:13', '2004-11-30
> > 19:20:13', 0.00);
>
> > Thanks for your help!
> > Erik
>
> I feel obliged t

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

2007-05-29 Thread erikcw
On May 28, 2:47 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Mon, 28 May 2007 14:53:57 -0300, Dennis Lee Bieber
> <[EMAIL PROTECTED]> escribió:
>
> > On Sun, 27 May 2007 20:35:28 -0400, Carsten Haese <[EMAIL PROTECTED]>
> > declaimed the following in comp.lang.python:
>
> >> On Sun, 2007-05-27 at 16:39 -0400, [EMAIL PROTECTED] wrote:
> >> > >  sql = """SELECT payment_id FROM amember_payments WHERE
> >> > > member_id=%s AND expire_date > NOW() AND completed=1 AND (product_id
> >> > >>> 11 AND product_id <21)""", (self.uid)
>
> >It's confusing, as the example shown is not the common form for
> > parameter passing on the .execute() call -- without the .execute() it is
> > unclear. I presume the .execute() is using *sql to unpack the tuple...
>
> Yes, the original message said self.amember_cursor.execute(*sql)
> It IS confusing...
>
> --
> Gabriel Genellina

This is how I've always writing my queries.  I learned it from some
tutorial I found on Google when I started - what is the preferred/
pythonic way to write this query?

Thanks!
Erik

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


Trying to render a pie chart

2007-05-30 Thread erikcw
HI all,

I'm trying to use Matplotlib to render a pie chart for a django site
I'm working on.

I can't figure out how to get rid of the grey background, or make the
dimensions square (it's getting stretched).

Here is the code:

def oldchart(request):
from PIL import Image as PILImage
from matplotlib.backends.backend_agg import FigureCanvasAgg as
FigureCanvas
from matplotlib.figure import Figure
from StringIO import StringIO
fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
#ax.plot([1,2,3])
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]
ax.pie(fracs, labels=labels)
#ax.set_title('hi mom')
ax.grid(True)
#ax.set_xlabel('time')
#ax.set_ylabel('volts')
canvas.draw()
size = canvas.get_renderer().get_canvas_width_height()
buf=canvas.tostring_rgb()
im=PILImage.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1)
imdata=StringIO()
im.save(imdata, format='JPEG')
response = HttpResponse(imdata.getvalue(), mimetype='image/jpeg')
return response

Thanks for your help!
Erik

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


I can't get multi-dimensional array to work...

2007-03-30 Thread erikcw
Hi,

I'm trying to create a multidimensional data structure.  However, id
doesn't seem to work past the 2nd dimension.

Here is my method:

def endElement(self, name):
if name == 'row' :
if not self.data.has_key(self.parent):
self.data[self.parent] = {}
elif not self.data[self.parent].has_key(self.child):
self.data[self.parent][self.child] = []
self.data[self.parent]
[self.child].append((self.creativeid, self.clicks, self.imps))

I've tried all kinds of different variations, and I keep getting the
same result:

Traceback (most recent call last):
  File "sax.py", line 123, in ?
parser.parse(open('report.xml'))
  File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
line 109, in parse
xmlreader.IncrementalParser.parse(self, source)
  File "/usr/lib/python2.4/site-packages/_xmlplus/sax/xmlreader.py",
line 123, in parse
self.feed(buffer)
  File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
line 216, in feed
self._parser.Parse(data, isFinal)
  File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
line 315, in end_element
self._cont_handler.endElement(name)
  File "sax.py", line 51, in endElement
self.data[self.parent][self.child].append((self.creativeid,
self.clicks, self.imps))
KeyError: u'Pickup Trucks'

I have a lot of php experience - am I accidentally doing a "php thing"
in my code?

Thanks so much for your help!

Erik

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


Re: I can't get multi-dimensional array to work...

2007-03-30 Thread erikcw
On Mar 30, 5:23 pm, [EMAIL PROTECTED] wrote:
> On Mar 30, 4:56 pm, "erikcw" <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi,
>
> > I'm trying to create a multidimensional data structure.  However, id
> > doesn't seem to work past the 2nd dimension.
>
> > Here is my method:
>
> > def endElement(self, name):
> > if name == 'row' :
> > if not self.data.has_key(self.parent):
> > self.data[self.parent] = {}
> > elif not self.data[self.parent].has_key(self.child):
> > self.data[self.parent][self.child] = []
> > self.data[self.parent]
> > [self.child].append((self.creativeid, self.clicks, self.imps))
>
> > I've tried all kinds of different variations, and I keep getting the
> > same result:
>
> > Traceback (most recent call last):
> >   File "sax.py", line 123, in ?
> > parser.parse(open('report.xml'))
> >   File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
> > line 109, in parse
> > xmlreader.IncrementalParser.parse(self, source)
> >   File "/usr/lib/python2.4/site-packages/_xmlplus/sax/xmlreader.py",
> > line 123, in parse
> > self.feed(buffer)
> >   File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
> > line 216, in feed
> > self._parser.Parse(data, isFinal)
> >   File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
> > line 315, in end_element
> > self._cont_handler.endElement(name)
> >   File "sax.py", line 51, in endElement
> > self.data[self.parent][self.child].append((self.creativeid,
> > self.clicks, self.imps))
> > KeyError: u'Pickup Trucks'
>
> > I have a lot of php experience - am I accidentally doing a "php thing"
> > in my code?
>
> > Thanks so much for your help!
>
> > Erik
>
> I haven't tested it, but superficially I'd suggest giving this a try:
>
> def endElement(self, name):
> if name == 'row' :
> if not self.data.has_key(self.parent):
> self.data[self.parent] = {}
> if not self.data[self.parent].has_key(self.child):
> self.data[self.parent][self.child] = []
> self.data[self.parent]
> [self.child].append((self.creativeid, self.clicks, self.imps))

That seems to have done the trick!  I can't believe I spent all
afternoon trying to figure that out!!  Thanks a million!

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


Error when trying to pass list into function.

2007-04-02 Thread erikcw
Hi,

I'm getting the following error when I try to pass a list into a
function.

My List: crea =[(u'218124172', u'536', u'32394'), (u'218320282',
u'1323', u'77931')]

Traceback (most recent call last):
  File "wa.py", line 118, in ?
curHandler.walkData()
  File "wa.py", line 49, in walkData
self.results[parent][child]['results'] = self.calculate(crea)
#pass in list of tuples
TypeError: calculate() takes exactly 1 argument (2 given)

def calculate(dta):
#stub

How can I make this work?

Thanks!
Erik

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


Math with unicode strings?

2007-04-02 Thread erikcw
Hi,

I'm parsing xml data with xml.sax and I need to perform some
arithmetic on some of the xml attributes.  The problem is they are all
being "extracted" as unicode strings, so whenever I try to perform
math operations on the variables, I get this error:

cr[0] = data['cls1']/data['ims1'];
TypeError: unsupported operand type(s) for /: 'unicode' and 'unicode'

What do I need to do to extract the intergers from these unicode
strings (or better yet, parse them as intergers in the first place.).
I'm using the SAX method attrs.get('cls1',"") to parse the xml.  Can I
"cast" the string into an interger?

Thanks!
Erik

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


How do you escape % when formatting a string?

2007-04-02 Thread erikcw
Hi,

I'm trying to format a string like so:

string = "You have a 75% chance of success with %s, don't use %s" %(a,
b)

This gives me:
TypeError: not enough arguments for format string

I've tried 75\%, but that doesn't seem to help.  What am I missing?

Thanks!
Erik

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


AttributeError: 'tuple' object has no attribute 'encode'

2007-04-05 Thread erikcw
Hi,

I'm trying to build a SQL string

sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
(cid, ag, self.data[parent][child]['results']['test'])

It raises this error: AttributeError: 'tuple' object has no attribute
'encode'

Some of the variables are unicode (test and ag) - is that what is
causing this error?  What do I need to do to make it work?

Thanks!
Erik

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


Re: AttributeError: 'tuple' object has no attribute 'encode'

2007-04-05 Thread erikcw
On Apr 5, 11:41 am, [EMAIL PROTECTED] wrote:
> On Apr 5, 10:31 am, "erikcw" <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > I'm trying to build a SQL string
>
> > sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
> > (cid, ag, self.data[parent][child]['results']['test'])
>
> > It raises this error: AttributeError: 'tuple' object has no attribute
> > 'encode'
>
> > Some of the variables are unicode (test and ag) - is that what is
> > causing this error?  What do I need to do to make it work?
>
> > Thanks!
> > Erik
>
> It sounds like you're not calling the "encode" method correctly. But
> it's hard to say when you didn't include that bit of code. You may
> need to check your database's docs to make sure it accepts unicode
> strings and if so, what types (utf-8, 16, 32).
>
> See this post for a similar problem:
>
> http://lists.modevia.com/archives/py-transports/2005-December/001719
>
> and this link details tuple 
> usage:http://www.faqs.org/docs/diveintopython/odbchelper_tuple.html
>
> Mike

I tried adding .encode("utf-8") onto each of the variables in the
tuple, but that didn't seem to help.  The error just changes slightly
to AttributeError: 'long' object has no attribute 'encode'

I'm using Mysql.

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


Re: AttributeError: 'tuple' object has no attribute 'encode'

2007-04-05 Thread erikcw
On Apr 5, 12:37 pm, "Paul Boddie" <[EMAIL PROTECTED]> wrote:
> erikcw wrote:
>
> > I'm trying to build a SQL string
>
> > sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
> > (cid, ag, self.data[parent][child]['results']['test'])
>
> This makes a tuple, though: the first element is the SQL string; the
> second element contains a tuple of parameters.
>
> > It raises this error: AttributeError: 'tuple' object has no attribute
> > 'encode'
>
> What does? I imagine that this error comes from a call to a cursor
> object's execute method. In other words, I imagine that you may be
> doing something like this:
>
> cursor.execute(*sql)
>
> Not that there would be anything obviously wrong with that: you are
> keeping the string and its parameters separate, after all. However,
> you'd have to show us the full error (a traceback including lines of
> code from the database library) for us to really see what's going on.
>
> > Some of the variables are unicode (test and ag) - is that what is
> > causing this error?  What do I need to do to make it work?
>
> Show us more of the error! ;-)
>
> Paul

Here is the full error: (sorry)

Traceback (most recent call last):
 File "/home/erik/Desktop/wa.py", line 178, in ?
curHandler.walkData()
 File "/home/erik/Desktop/wa.py", line 91, in walkData
sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
(cid.encode("utf-8"), ag, self.data[parent][child]['results']['test'])
AttributeError: 'long' object has no attribute 'encode'

sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""",
(cid.encode("utf-8"), ag, self.data[parent][child]['results']['test'])
self.cursor.execute(sql)

Now, I changed all ofth e %i/%d to %s, and changed
self.cursor.execute(sql) to self.cursor.execute(*sql) and it seems to
be working now!

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


Extract zip file from email attachment

2007-04-05 Thread erikcw
Hi all,

I'm trying to extract zip file (containing an xml file) from an email
so I can process it.  But I'm running up against some brick walls.
I've been googling and reading all afternoon, and can't seem to figure
it out.

Here is what I have so far.

p = POP3("mail.server.com")
print p.getwelcome()
# authentication, etc.
print p.user("USER")
print p.pass_("PASS")
print "This mailbox has %d messages, totaling %d bytes." % p.stat()
msg_list = p.list()
print msg_list
if not msg_list[0].startswith('+OK'):
# Handle error
exit(1)

for msg in msg_list[1]:
msg_num, _ = msg.split()
resp = p.retr(msg_num)
if resp[0].startswith('+OK'):
#print resp, '===\n'
#extract message body and attachment.
parsed_msg = email.message_from_string('\n'.join(resp[1]))
payload= parsed_msg.get_payload(decode=True)
print payload  #doesn't seem to work
else:
pass# Deal with error retrieving message.

How do I:
a) retrieve the body of the email into a string so I can do some
processing? (I can get at the header attributes without any trouble)
b) retrieve the zip file attachment, and unzip into a string for xml
processing?

Thanks so much for your help!
Erik

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


Re: Extract zip file from email attachment

2007-04-05 Thread erikcw
On Apr 5, 8:00 pm, hlubenow <[EMAIL PROTECTED]> wrote:
> erikcw wrote:
> > Hi all,
>
> > I'm trying to extract zip file (containing an xml file) from an email
> > so I can process it.  But I'm running up against some brick walls.
> > I've been googling and reading all afternoon, and can't seem to figure
> > it out.
>
> > Here is what I have so far.
>
> > p = POP3("mail.server.com")
> > print p.getwelcome()
> > # authentication, etc.
> > print p.user("USER")
> > print p.pass_("PASS")
> > print "This mailbox has %d messages, totaling %d bytes." % p.stat()
> > msg_list = p.list()
> > print msg_list
> > if not msg_list[0].startswith('+OK'):
> > # Handle error
> > exit(1)
>
> > for msg in msg_list[1]:
> > msg_num, _ = msg.split()
> > resp = p.retr(msg_num)
> > if resp[0].startswith('+OK'):
> > #print resp, '===\n'
> > #extract message body and attachment.
> > parsed_msg = email.message_from_string('\n'.join(resp[1]))
> > payload= parsed_msg.get_payload(decode=True)
> > print payload  #doesn't seem to work
> > else:
> > pass# Deal with error retrieving message.
>
> > How do I:
> > a) retrieve the body of the email into a string so I can do some
> > processing? (I can get at the header attributes without any trouble)
> > b) retrieve the zip file attachment, and unzip into a string for xml
> > processing?
>
> > Thanks so much for your help!
> > Erik
>
> Hi,
>
> some weeks ago I wrote some code to extract attachments from emails.
> It's not that long, so maybe it could be of help for you:
>
> ---
>
> #!/usr/bin/env python
>
> import poplib
> import email
> import os
> import sys
> import string
>
> #
> # attsave.py
> # Check emails at PROVIDER for attachments and save them to SAVEDIR.
> #
>
> PROVIDER = "pop.YourMailProvider.de"
> USER = "YourUserName"
> PASSWORD = "YourPassword"
>
> SAVEDIR = "/home/YourUserDirectory"
>
> def saveAttachment(mstring):
>
> filenames = []
> attachedcontents = []
>
> msg = email.message_from_string(mstring)
>
> for part in msg.walk():
>
> fn = part.get_filename()
>
> if fn <> None:
> filenames.append(fn)
> attachedcontents.append(part.get_payload())
>
> for i in range(len(filenames)):
> fp = file(SAVEDIR + "/" + filenames[i], "wb")
> fp.write(attachedcontents[i])
> print 'Found and saved attachment "' + filenames[i] + '".'
> fp.close()
>
> try:
> client = poplib.POP3(PROVIDER)
> except:
> print "Error: Provider not found."
> sys.exit(1)
>
> client.user(USER)
> client.pass_(PASSWORD)
>
> anzahl_mails = len(client.list()[1])
>
> for i in range(anzahl_mails):
> lines = client.retr(i + 1)[1]
> mailstring = string.join(lines, "\n")
> saveAttachment(mailstring)
>
> client.quit()
>
> ---
>
> See you
>
> H.

Thanks H!

I'm now able to get the name of the zip file, and the contents (is it
still encoded?).

I now need to be able to unzip the zip file into a string and get the
body of the email into a string.

Here is my updated code:
p = POP3("mail.**.com")
print p.getwelcome()
# authentication, etc.
print p.user("USER")
print p.pass_("PASS")
print "This mailbox has %d messages, totaling %d bytes." % p.stat()
msg_list = p.list()
print msg_list
if not msg_list[0].startswith('+OK'):
# Handle error in listings
exit(1)

for msg in msg_list[1]:
msg_num, _ = msg.split()
resp = p.retr(msg_num)
if resp[0].startswith('+OK'):
#print resp, '===\n'
parsed_msg = email.message_from_string('\n'.join(resp[1]))
for part in parsed_msg.walk():
fn = part.get_filename()
if fn <> None:
fileObj = StringIO.StringIO()
fileObj.write( part.get_payload() )
#attachment = zlib.decompress(part.get_payload())
#print zipfile.is_zipfile(fileObj)
attachment = zipfile.ZipFile(fileObj)
pri

Re: Extract zip file from email attachment

2007-04-06 Thread erikcw
On Apr 6, 12:51 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> erikcw wrote:
> > resp = p.retr(msg_num)
> > if resp[0].startswith('+OK'):
>
> You don't have to check this; errors are transformed into exceptions.
>
> > fileObj = StringIO.StringIO()
>
> cStringIO is faster
>
> > fileObj.write( part.get_payload() )
>
> You have to reset the file pointer to the beginning: fileObj.seek(0),
> else ZipFile will not be able to read the contents.
>
> --
> Gabriel Genellina

Hi Gabriel,

I added fileObj.seek(0) on the line directly after
fileObj.write( part.get_payload() ) and I'm still getting the
following error.

Traceback (most recent call last):
  File "wa.py", line 209, in 
attachment = zipfile.ZipFile(fileObj)
  File "/usr/lib/python2.5/zipfile.py", line 346, in __init__
self._GetContents()
  File "/usr/lib/python2.5/zipfile.py", line 366, in _GetContents
self._RealGetContents()
  File "/usr/lib/python2.5/zipfile.py", line 378, in _RealGetContents
raise BadZipfile, "File is not a zip file"
zipfile.BadZipfile: File is not a zip file

Could the file like object still be encoded in MIME or something?

Thanks!
Erik

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


MySQL Insert Unicode Problem

2007-04-09 Thread erikcw
Hi,

I'm trying to insert some data from an XML file into MySQL.  However,
while importing one of the files, I got this error:

Traceback (most recent call last):
  File "wa.py", line 304, in ?
main()
  File "wa.py", line 257, in main
curHandler.walkData()
  File "wa.py", line 112, in walkData
self.cursor.execute(*sql) #try multi-execute to speed up query.
  File "build/bdist.linux-i686/egg/MySQLdb/cursors.py", line 151, in
execute
  File "build/bdist.linux-i686/egg/MySQLdb/connections.py", line 247,
in literal
  File "build/bdist.linux-i686/egg/MySQLdb/connections.py", line 185,
in unicode_literal
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u20ac'
in position 28: ordinal not in range(256)

What do I need to change to make this work?

Thanks!
Erik

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


UnicodeEncodeError - a bit out of my element...

2007-04-11 Thread erikcw
Hi all,

I'm trying to parse an email message, but am running into this
exception.

Traceback (most recent call last):
  File "wa.py", line 336, in ?
main()
  File "wa.py", line 332, in main
print out['msg']
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd6' in
position 238: ordinal not in range(128)

How can I decode/encode this string to print to stdout and send again
in another email?  Do I have to know what language the email is in?

Thanks!
Erik

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


Re: UnicodeEncodeError - a bit out of my element...

2007-04-11 Thread erikcw
On Apr 11, 11:35 am, liupeng <[EMAIL PROTECTED]> wrote:
> I cut from Sam Python Phrasebook
> "Converting Unicode to Local Strings"
>
> import string locStr = "El "
> uniStr = u"Ni\u00F1o"
> print uniStr.encode('utf-8')
> print uniStr.encode('utf-16')
> print uniStr.encode('iso-8859-1')
> #Combine local and unicode results
> #in new unicode string
> newStr = locStr+uniStr
> print newStr.encode('iso-8859-1')
> #ascii will error because character '\xF1'
> #is out of range
> asciiStr = newStr.encode('iso-8859-1')
> asciiStr =asciiStr.translate(\
> string.maketrans('\xF1','n'), '')
> print asciiStr.encode('ascii')
> print newStr.encode('ascii')
>
> unicode_str.py
>
> Niño
> ÿN|I|ñ|o
> Niño
> El Niño
> El Nino
> Traceback (most recent call last):
>   File "C:\books\python\CH2\code\unicode_str.py",
> line 19, in ?
> print newStr.encode('ascii')
> UnicodeEncodeError: 'ascii' codec can't encode
>  character u'\xf1' in position 5: ordinal not in
>  range(128)
>
> On Wed, Apr 11, 2007 at 08:16:07AM -0700, erikcw wrote:
> > Hi all,
>
> > I'm trying to parse an email message, but am running into this
> > exception.
>
> > Traceback (most recent call last):
> >   File "wa.py", line 336, in ?
> > main()
> >   File "wa.py", line 332, in main
> > print out['msg']
> > UnicodeEncodeError: 'ascii' codec can't encode character u'\xd6' in
> > position 238: ordinal not in range(128)
>
> > How can I decode/encode this string to print to stdout and send again
> > in another email?  Do I have to know what language the email is in?
>
> > Thanks!
> > Erik
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>
>
>  signature.asc
> 1KDownload

I used the .encode("utf-8") method on the string and it fixed
everything!  Thanks for your help!

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


Traceback when trying to run script from cron?

2007-04-14 Thread erikcw
Hi all,

When trying to run this python script from cron, I get the following
error:


Traceback (most recent call last):
 File "/home/lybp/public_html/wa/wa.py", line 14, in ?
   import MySQLdb
ImportError: No module named MySQLdb

The cron command is python /home/lybp/public_html/wa/wa.py

Any idea why MySQLdb wouldn't like this?

Thanks!
Erik

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


Re: Traceback when trying to run script from cron?

2007-04-17 Thread erikcw
On Apr 14, 10:50 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On 14 Apr 2007 18:56:00 -0700, "erikcw" <[EMAIL PROTECTED]>
> declaimed the following in comp.lang.python:
>
>
>
> > The cron command is python /home/lybp/public_html/wa/wa.py
>
> > Any idea why MySQLdb wouldn't like this?
>
> Does the cron service run with the same PYTHONPATH? Stuff in a
> "print sys.path" (or a write to some file you can later examine) before
> the deadly import, and compare direct invocation with the cron
> invocation.
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED] [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/

You're right.  Something is not right with my sys.path.

cron:
'/home/lybp/public_html/winneralert', '/usr/lib/python2.2', '/usr/lib/
python2.2/plat-linux2', '/usr/lib/python2.2/lib-tk', '/usr/lib/
python2.2/lib-dynload', '/usr/lib/python2.2/site-packages']
Traceback (most recent call last):
 File "/home/lybp/public_html/winneralert/wa.py", line 18, in ?
   import MySQLdb
ImportError: No module named MySQLdb


SHELL:
# python wa.py
['/home/lybp/public_html/winneralert', '/usr/local/lib/python2.4/site-
packages/setuptools-0.6c5-py2.4.egg', '/usr/local/lib/python2.4/site-
packages/MySQL_python-1.2.2-py2.4-linux-i686.egg', '/usr/local/lib/
python24.zip', '/usr/local/lib/python2.4', '/usr/local/lib/python2.4/
plat-linux2', '/usr/local/lib/python2.4/lib-tk', '/usr/local/lib/
python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages']
+OK Hello there.

Why isn't cron able to import the MySJL module?  How do I make this
work? (newbie)

Thanks!
Erik

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


How to identify which numbers in a list are within each others' range

2008-01-31 Thread erikcw
Hi,

I have a list of numbers each with a +/- margin of error.  I need to
identify which ones overlab each other.

For example:
55 +/- 3
20 +/- 2
17 +/- 4
60 +/- 3

#base, max, min
list = [
(55, 58, 52),
(20, 22, 18),
(17, 21, 13),
(60, 63, 57),
]

In this example the range of list[0] overlaps the range of list[3] AND
list[1] overlaps list[2]

What is the best way to in python to identify the list items that
overlap and the items that don't overlap with any other.

Thanks!
Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Encrypting a short string?

2008-02-11 Thread erikcw
Hi,

I'm trying to devise a scheme to encrypt/obfuscate a short string that
basically contains the user's username and record number from the
database.  I'm using this encrypted string to identify emails from a
user. (the string will be in the subject line of the email).

I'm trying to figure out which approach I should use to encrypt the
data.  The string will be less than 20 characters long, and I'd like
the encrypted version to be about the same size.

I tried DES in the Crypto module, but the cipher text was to long to
be usable in this case.

Any suggestions?

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


Re: Encrypting a short string?

2008-02-11 Thread erikcw
On Feb 11, 3:07 pm, [EMAIL PROTECTED] wrote:
> erikcw napisal(a):
>
>
>
> > Hi,
>
> > I'm trying to devise a scheme to encrypt/obfuscate a short string that
> > basically contains the user's username and record number from the
> > database.  I'm using this encrypted string to identify emails from a
> > user. (the string will be in the subject line of the email).
>
> > I'm trying to figure out which approach I should use to encrypt the
> > data.  The string will be less than 20 characters long, and I'd like
> > the encrypted version to be about the same size.
>
> > I tried DES in the Crypto module, but the cipher text was to long to
> > be usable in this case.
>
> > Any suggestions?
>
> > Thanks!
>
> How about:
>
> >>> hashlib.sha256("[EMAIL PROTECTED]|2937267834").hexdigest()[:20]
>
> Regards,
> Marek

Thanks Marek,

But that can't be reversed, right?  I'd like to be able to decrypt the
data instead of having to store the hash in my database...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encrypting a short string?

2008-02-11 Thread erikcw
On Feb 11, 4:07 pm, [EMAIL PROTECTED] wrote:
> erikcw napisal(a):> But that can't be reversed, right? I'd like to be able to 
> decrypt the
> > data instead of having to store the hash in my database...
>
> In such case it seems you have no choice but to use a symmetric
> encryption algorithm - in other words, your original method. If the
> strings are ~20 bytes long (3 DES blocks), then the base64-encoded
> ciphertext will have 32 characters. In case of AES, that'll be up to
> 45 characters. Wouldn't such length be acceptable?
>
> Paul Rubin napisal(a):> 2. What happens if the user edits the subject line?
> > Under normal security requirements you cannot do this. The ciphertext
> > has to be longer than the plaintext since you don't want the opponent
> > to be able to tell whether two plaintexts are the same. Therefore you
> > have to attach some random padding to each plaintext. Also, you
> > presumably want the ciphertext to be encoded as printing characters,
> > while normally you'd treat the input as binary, so there is some
> > further expansion.
>
> If what erikcw is looking for is a cryptographically secure protocol,
> there are more things to be careful about, like authentication or
> replay attacks. But indeed, I'm wondering now what his use-case is.> I'm 
> using this encrypted string to identify emails from a
> > user. (the string will be in the subject line of the email).
>
> Why not use "From" field to identify emails from a particular user?
>
> Regards,
> Marek

In essence what I'm doing is trying to manage tickets for a helpdesk.
I want the ticket identifier to be short enough to fit in the subject
line along with the normal subject chosen by the user.  So
cryptographic security isn't really important.  I can't use the from:
field because a single user could have multiple tickets.
-- 
http://mail.python.org/mailman/listinfo/python-list


Accessing "sub elements" with xml.sax ?

2008-02-25 Thread erikcw
Hi,

I'm trying to use xml.sax (from xml.sax.handler import ContentHandler)
to processes the following data:








I've figured out how to access the attributes in "row" - but I want to
also access the "analytics" child element.

I've tried:
class YahooHandler(ContentHandler):
ccountNum)

def startElement(self, name, attrs):
if name == 'row' or name == 'analytics':
self.campaign = attrs.get('cmpgnName',"")
self.adgroup = attrs.get('adGrpName',"")
self.headline = attrs.get('adName',"")
self.imps = attrs.get('numImpr',None)
self.clicks = attrs.get('numClick',None)
self.cost = attrs.get('cost',"")

def endElement(self, name):
if name == 'row':
if self.campaign not in self.data:
self.data[self.campaign] = {}
if self.adgroup not in self.data[self.campaign]:
self.data[self.campaign][self.adgroup] = []
self.data[self.campaign][self.adgroup].append({'campaign':
self.campaign,
'adgroup': self.adgroup,
'headline': self.headline,
'imps': self.imps,
'clicks': self.clicks,
'ctr': self.ctr,
'cost': self.cost,
})
print self.data

But it the data comes out as seperate dictionaries - I want the
analytics and the row elements in one dictionary.

What am I doing wrong?

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


Python-PHP bridge?

2008-03-13 Thread erikcw
Hi all,

I'm starting to translate a large legacy php app to python (django),
and was wondering if anyone knew of a bridge between the 2 languages.
(access php objects from python and vice versa).

Some of the solutions I've come across so far include using php's
passthru() ( http://us3.php.net/passthru ) and pyphp.

passthru():  I'm concerned about the potential performance hit of all
these system calls.  Isn't this the equivalent of a CGI app?

pyphp:  Seems closer to what I'm looking for, but I haven't been able
to get the examples working yet.  Maybe it is to alpha?

If anyone has any experience with these tools or others and can share
some advice, I'd appreciate it!

Thanks!
Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Custom Exception Value?

2008-03-14 Thread erikcw
Hi,

When I use sys.exc_info() on one of my custom exception classes, the
"message"/value isn't returned.  But it is when I use a built in
exception.

Example:

In [32]: class Test(Exception):
   : def __init__(self, x):
   : self.value = x
   : def __str__(self):
   : return repr(self.value)
   :
   :
In [39]: try:
   : raise Test('mess')
   : except:
   : sys.exc_info()
   :
   :
Out[39]: (, Test(), )

In [40]: try:
   : 1/0
   : except:
   : sys.exc_info()
   :
   :
Out[40]:
(,
 ZeroDivisionError('integer division or modulo by zero',),
 )


Wy does the output of ZeroDivisionError appear in sys.exc_info() but
for test it is just Test() (should be Test('mess') right??)

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


Merging a patch/diff generated by difflib?

2008-03-18 Thread erikcw
Hi,

I'm trying to create an undo/redo feature for a webapp I'm working on
(django based).  I'd like to have an undo/redo function.

My first thought was to use the difflib to generate a diff to serve as
the "backup", and then if someone wants to undo their operation, the
diff could just be merged/patched with the current text.

However, I've not be able to find a patch library.  Are there any
libraries that will handle merging the diff back into the text?

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


"Soup Strainer" for ElementSoup?

2008-03-23 Thread erikcw
Hi all,

I was reading in the Beautiful Soup documentation that you should use
a "Soup Strainer" object to keep memory usage down.

Since I'm already using Element Tree elsewhere in the project, I
figured it would make sense to use ElementSoup to keep the api
consistent. (and cElementTree should be faster right??).

I can't seem to figure out how to pass ElementSoup a "soup strainer"
though.

Any ideas?

Also - do I need to use the extract() method with ElementSoup like I
do with Beautiful Soup to keep garbage collection working?

Thanks!
Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Soup Strainer" for ElementSoup?

2008-03-25 Thread erikcw
On Mar 25, 12:17 am, John Nagle <[EMAIL PROTECTED]> wrote:
> erikcwwrote:
> > Hi all,
>
> > I was reading in the Beautiful Soup documentation that you should use
> > a "Soup Strainer" object to keep memory usage down.
>
> > Since I'm already using Element Tree elsewhere in the project, I
> > figured it would make sense to use ElementSoup to keep the api
> > consistent. (and cElementTree should be faster right??).
>
> > I can't seem to figure out how to pass ElementSoup a "soup strainer"
> > though.
>
> > Any ideas?
>
> > Also - do I need to use the extract() method with ElementSoup like I
> > do with Beautiful Soup to keep garbage collection working?
>
> > Thanks!
> > Erik
>
> I really should get my version of BeautifulSoup merged back into
> the mainstream.  I have one that's been modified to use weak pointers
> for all "up" and "left" links, which makes the graph cycle free. So
> the memory is recovered by reference count update as soon as you
> let go of the head of the tree.  That helps with the garbage problem.
>
> What are you parsing?  If you're parsing well-formed XML,
> BeautifulSoup is overkill.  If you're parsing real-world HTML,
> ElementTree is too brittle.
>
> John Nagle

I'm parsing real-world HTML with BeautifulSoup and XML with
cElementTree.

I'm guessing that the only benefit to using ElementSoup is that I'll
have one less API to keep track of, right?  Or are there memory
benefits in converting the Soup object to an ElementTree?

Any idea about using a Soup Strainer with ElementSoup?

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


Specifying an IP address for outbound connections?

2008-10-28 Thread erikcw
Python seems to default to the main system IP for outbound connections
(such as urllib), but I want to bind to one of my other IPs for
outbound connections.

Any ideas?

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


Need some help speeding up this loop

2008-10-29 Thread erikcw
Hi all,

I'm trying to write a loop that will build a list of "template
strings".

My current implementation is *really slow*.  It took 15 minutes to
finish. (final len(list) was about 16k entries.)

#combinations = 12 small template strings ie "{{ city }},
{{ state }}..."
#states = either a django model or a list of 50 states
#cities = either a django model of 400 cities or a smaller list of
cities.

templates = []
for c in combinations:
if len(states):
for state in states:
if type(geo) is City:
cities = state.city_set.all()
else:
cities = geo
for city in cities:
if type(city) is City:
city = city.city
templates.append(c.template.replace('{{ city }}',
city))
templates.append(c.template) #just in case there are no
cities
templates = [k.replace('{{ state }}',
state.state).replace('{{ state_abbr }}', state.abbreviation) for k in
templates]
elif len(geo):
for city in geo:
templates.append(c.template.replace('{{ city }}', city))
else:
#no cities or states so add roots
templates.append(c.template)

The final output needs to be a list of the templates combined with all
the states and cities (some templates will only have the city, some
only the state).

Any ideas how I can optimize this?

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


Can't figure out traceback: (error_proto(-ERR EOF) line 121 poplib.py

2008-04-08 Thread erikcw
Hi,

I keep getting this error from poplib:
(error_proto(-ERR EOF) line 121 poplib.py

Does this mean the connection has timed out?  What can I do to deal
with it?

Thanks!
Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Database vs Data Structure?

2008-04-17 Thread erikcw
Hi,

I'm working on a web application where each user will be creating
several "projects" in there account, each with 1,000-50,000 objects.
Each object will consist of a unique name, an id, and some meta data.

The number of objects will grow and shrink as the user works with
their project.

I'm trying to decided whether to store the objects in the database
(each object gets it's own row) or to use some sort of data-structure
(maybe nested dictionaries or a custom class) and store the pickled
data-structure in a single row in the database (then unpickle the data
and query in memory).

A few requirements:
-Fast/scalable (web app)
-able to query objects based on name and id.
-will play nicely with versioning (undo/redo)

Any input on the best way to go?

Thanks!
Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Database vs Data Structure?

2008-04-17 Thread erikcw
Hi,

I'm working on a web application where each user will be creating
several "projects" in there account, each with 1,000-50,000 objects.
Each object will consist of a unique name, an id, and some meta data.

The number of objects will grow and shrink as the user works with
their project.

I'm trying to decided whether to store the objects in the database
(each object gets it's own row) or to use some sort of data-structure
(maybe nested dictionaries or a custom class) and store the pickled
data-structure in a single row in the database (then unpickle the data
and query in memory).

A few requirements:
-Fast/scalable (web app)
-able to query objects based on name and id.
-will play nicely with versioning (undo/redo)

Any input on the best way to go?

Thanks!
Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Wrong application name in menubar of OS X

2008-09-12 Thread erikcw
Hi,

The menubar of OS X is showing the application name as Python instead
of the name of my wxpython gui app.  How do I make my application name
show-up in the menu bar?

Thanks!
Erik
--
http://mail.python.org/mailman/listinfo/python-list


Launching a subprocess without waiting around for the result?

2008-09-18 Thread erikcw
Hi,

I have a cgi script where users are uploading large files for
processing.  I want to launch a subprocess to process the file so the
user doesn't have to wait for the page to load.

What is the correct way to launch subprocess without waiting for the
result to return?

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


Re: Launching a subprocess without waiting around for the result?

2008-09-18 Thread erikcw
On Sep 18, 3:33 pm, Ben Finney <[EMAIL PROTECTED]>
wrote:
> erikcw <[EMAIL PROTECTED]> writes:
> > I have a cgi script where users are uploading large files for
> > processing. I want to launch a subprocess to process the file so the
> > user doesn't have to wait for the page to load.
>
> For "how do I deal with subprocesses from Python", the (new in Python
> 2.4) 'subprocess' module is the default go-to answer
> http://www.python.org/doc/lib/module-subprocess>, replacing a
> rather fragmented set of modules before it.
>
> > What is the correct way to launch subprocess without waiting for the
> > result to return?
>
> Creating an instance of 'subprocess.Popen' will launch the process and
> return the Popen instance. You then have the option of polling it or
> waiting for it to complete.
>
> --
>  \     “To stay young requires unceasing cultivation of the ability to |
>   `\                   unlearn old falsehoods.” —Robert Anson Heinlein |
> _o__)                                                                  |
> Ben Finney

So if I create a Popen object and then just ignore the object and exit
the program the subproccess will finish it's work and then exit itself
cleanly?
--
http://mail.python.org/mailman/listinfo/python-list


Download the "head" of a large file?

2009-07-27 Thread erikcw
I'm trying to figure out how to download just the first few lines of a
large (50mb) text file form a server to save bandwidth.  Can Python do
this?

Something like the Python equivalent of curl http://url.com/file.xml |
head -c 2048

Thanks!
Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Is this a bug in multiprocessing or in my script?

2009-08-04 Thread erikcw
Hi,

I'm trying to get multiprocessing to working consistently with my
script.  I keep getting random tracebacks with no helpful
information.  Sometimes it works, sometimes it doesn't.

Traceback (most recent call last):
  File "scraper.py", line 144, in 
print pool.map(scrape, range(10))
  File "/usr/lib/python2.6/multiprocessing/pool.py", line 148, in map
return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib/python2.6/multiprocessing/pool.py", line 422, in get
raise self._value
TypeError: expected string or buffer

It's not always the same traceback, but they are always short like
this.  I'm running Python 2.6.2 on Ubuntu 9.04.

Any idea how I can debug this?

Thanks!
Erik
-- 
http://mail.python.org/mailman/listinfo/python-list