Re: sum up numbers in a list

2008-08-26 Thread c james

 L=['10','15','20']
 sum(int(x) for x in L)
45

or
 sum(map(int,L))
45

sharon kim wrote:

hi all,

i have a list, for example;

  L=[]
  L.append('10')
  L.append('15')
  L.append('20')
  len(L)
3
  print L
['10', '15', '20']

is there a way to sum up all the numbers in a list?  the number of 
objects in the list is vary, around 50 to 60. all objects are 1 to 3 
digit positive numbers.


all i can think of is check the length of the list (in the above 
example, 3), then L[0]+L[1]+L[2] ..


is there a better way to do the job? thanks.


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


Re: Books for learning how to write big programs

2008-05-27 Thread c james

duli wrote:

Hi:
I would like recommendations for books (in any language, not
necessarily C++, C, python) which have walkthroughs for developing
a big software project ? So starting from inception, problem
definition, design, coding and final delivery on a single theme
or application.

Most of the code I have written and books that I have read deal with
toy programs and I am looking for something a bit more
comprehensive.  For example, maybe a complete compiler written in C++
for some language, or a complete web server or implementing
.net libraries in some language (just a few examples of the scale of
things I am interested in learning).

Thanks!
Duli.


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



Dreaming in Code www.dreamingincode.com may fit part of this 
description. While it isn't exactly what you desire, it is a good read 
on the difficulty in creating excellent software.


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


Re: Problems with psycopg2

2008-05-05 Thread c james

David Anderson wrote:
The thing is this query works fine on the console through psql, but not 
in my code? can anyone explain me why?


On Thu, May 1, 2008 at 9:31 PM, David Anderson [EMAIL PROTECTED] 
mailto:[EMAIL PROTECTED] wrote:


Hi all
I have this function:
def checkName(self, name):
cur = self.conn.cursor()
 
sql = SELECT * from patient WHERE fn_pat = ' + name + '

cur.execute(sql)
rows = cur.fetchall()
   
if rows == []:

self.insert()

It seems to work fine, But I'm getting this exception:
psycopg2.ProgrammingError: current transaction is aborted, commands
ignored until end of transaction block
at: cur.execute(sql)

What's the problem?
thx

ps: fn_pat is the column of the db, name is the string passed in the
function parameter





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


Does `name` happen to have an apostrophe in it?  You may want to look at 
using bind variables.


cur.execute(SELECT * from patient WHERE fn_pat=%(name)s,
{'name': name})


http://wiki.python.org/moin/DbApiCheatSheet
http://mail.python.org/pipermail/python-list/2005-March/314154.html

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


keyword 'in' not returning a bool?

2008-02-08 Thread c james
Try this

 sample = {'t':True, 'f':False}
 't' in sample
True
 type('t' in sample)
type 'bool'
 't' in sample == True
False

Why is this?  Now try
 bool('t' in sample) == True
True

Can someone explain what is going on?

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


Re: Cheat sheet

2007-12-28 Thread c james
Riccardo T. wrote:
 I wrote a little cheat sheet for this wonderful language, but because of
 my still little experience with it, I would like to have a feedback
 Could you have a look at it and tell me what do you think about, please?
 
 http://greyfox.imente.org/index.php?id=73
 
 --
 GreyFox

On the svg version, as viewed on firefox, the sections Simple
statements, Definations and Comments; there appears to be a problem
with text wrapping.

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


Overriding member methods in __init__

2007-12-03 Thread c james
Given a condition at the time a class is instantiated, I want to change
how __call__ is used.  From the example below, self.no is using self.yes
but self.__call__ is not.  Can someone please explain why?

EXAMPLE:
class YesNo(object):
def __init__(self, which):
self.no = self.yes
self.__call__ = self.yes

def yes(self, val):
print 'Yes', val

def no(self, val):
print 'No', val

def __call__(self, val):
raise NotImplementedError()

 y = YesNo(True)
 y.yes('hello')
Yes hello
 y.no('hello')
Yes hello
 y('hello')
Traceback 
Not ImplementedError

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


Re: Overriding member methods in __init__

2007-12-03 Thread c james
Bruno Desthuilliers wrote:
 c james a écrit :
 Given a condition at the time a class is instantiated, I want to change
 how __call__ is used.  From the example below, self.no is using self.yes
 but self.__call__ is not.  Can someone please explain why?
 
 IIRC, you can't override __magic__ methods on a per-instance basis.


 This should do the trick:
 
 class YesNo(object):
def __init__(self, which):
  self.which = which
 
def __call__(self, val):
  return (self.no, self.yes)[self.which](val)
 
def yes(self, val):
  print 'Yes', val
 
def no(self, val):
  print 'No', val

Thanks, I was trying to eliminate another level of indirection with a
test at each invocation of __call__

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


Re: Overriding member methods in __init__

2007-12-03 Thread c james
Arnaud Delobelle wrote:
 Why not simply do:
 
 class YesNo(object):
 def __init__(self, which):
 self.yesno = which and self.yes or self.no
 def yes(self, val):
 print 'Yes', val
 def no(self, val):
 print 'No', val
 def __call__(self, val):
 self.yesno(val)
 
 I don't know which is fastest but I don't think there would be much
 difference.
 
 --
 Arnaud
 

This is more in the spirit of what I was trying to accomplish.
Originally, I was unaware the __call__ could not be assigned a different
method.

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


Re: appending into a list

2007-10-30 Thread c james
Beema shafreen wrote:
   2721520  2721569A_16_P21360235199-49
   2721768  2721821A_16_P03641971139-53
   2721960  2722004A_16_P21360237312-44
 I need to append the column D and E into a list:
 in such a way that the list should have 
 [D,E,D,E,D,E]
 How do i do it.
 
 regards
 shafreen

Without a header, you could use something like

data = [x.split()[-2:] for x in open(filename).readlines()]

With header

f = open(filename)
f.readline()
data = [x.split()[-2:] for x in f.readlines()]


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


Re: Pure Python equivalent of unix file command?

2007-07-23 Thread c james
Take a look at
http://www.demonseed.net/~jp/code/magic.py



W3 wrote:
 Hi all,
 
 Just a quick one... Is there such a thing?
 
 Thanks,
 /Walter

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


Re: multi threaded SimpleXMLRPCServer

2007-05-14 Thread c james
I use a variation of the following.  Clean and straight forward.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425043


Vyacheslav Maslov wrote:
 Hi, all!
 
 I need multi threaded version of SimpleXMLRPCServer. Does python library 
 already have implementation of this one? Or i need to implement multi 
 threading by myself?
 
 Which way is the simpliest?

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


Re: Plugin architecture - how to do?

2007-04-10 Thread c james
Take a look at Trac.  This might give you some ideas.

http://trac.edgewall.org/wiki/TracDev/ComponentArchitecture

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


Re: SimpleXMLRPCServer - client address

2007-04-02 Thread c james
Jan Danielsson wrote:
 Hello all,
 
I writing an application based on the SimpleXMLRPCServer class. I
 would like to know the IP address of the client performing the RPC. Is
 that possible, without having to abandon the SimpleXMLRPCServer class?
 
I did this a long time ago so it's not likely the best solution.

class RPCServer(SimpleXMLRPCServer):

 def _dispatch(self, method, params):
 Extend dispatch, adding client info to some parameters.
 if method in ({my list of methods I needed client address}):
 return SimpleXMLRPCServer._dispatch(self, method,
params+(self.client_address,))
 return SimpleXMLRPCServer._dispatch(self, method, params);

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


Re: Controlling kwrite by dcop

2005-05-08 Thread R. C. James Harlow
On Sunday 08 May 2005 13:41, [EMAIL PROTECTED] wrote:
 As you can see you can interact with kwrite from dcop.
 Unfortunately I don't have this module in my Python (2.3) nor I have
 been able to find it.

It's normally installed seperately from the main kde libraries - on gentoo 
it's a package called dcoppython, that might help you in your search if 
you're on a different distro.

james.


pgp55bUjWf3dA.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: large dictionary creation takes a LOT of time.

2005-04-29 Thread R. C. James Harlow
On Friday 29 April 2005 11:53, Ville Vainio wrote:
  Kent == Kent Johnson [EMAIL PROTECTED] writes:

 Kent if frequency.has_key(word):
 Kent frequency[word] += 1
 Kent else:
 Kent frequency[word] = 1

 This is a good place to use 'get' method of dict:

 frequency[word] = frequency.get(word,0) + 1

try/except might be fastest of all:

http://gumuz.looze.net/wordpress/index.php/archives/2005/04/28/python-dictionary-speed-optimisation/


pgpyyTIumZQjr.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Multiple tuples for one for statement

2005-04-25 Thread R. C. James Harlow
On Monday 25 April 2005 04:20, James Stroud wrote:
 for a,b,c in zip(tup1, tup2, tup3):
print a
print b
print c

or just:

for a,b,c in (tup1, tup2, tup3):
print a
print b
print c


pgpJ0RNTnCUA3.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Multiple tuples for one for statement

2005-04-25 Thread R. C. James Harlow
On Monday 25 April 2005 14:34, Ivan Van Laningham wrote:
 Hi All--

 R. C. James Harlow wrote:
  or just:
 
  for a,b,c in (tup1, tup2, tup3):
  print a
  print b
  print c

 And this works in Python version???

Ah, reading the replies to the original post, this works but doesn't give the 
result that the original poster wanted.


pgpottbqbjtRP.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Do I need a nested lambda to do this?

2005-04-25 Thread R. C. James Harlow
On Tuesday 26 April 2005 00:34, raoul wrote:
 I can't figure this one out. Trying to be unnecessarily functional I
 suspect.

With list comprehensions:

Python 2.3.4 (#1, Mar 26 2005, 20:54:10)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on linux2
Type help, copyright, credits or license for more information.
 vals = [1.000,2.344,4.2342]
 tabs = [((0,1),(0,3),(0,4)),
...((2,2),(3,0),(3,9)),
...((3,4),(6,3),(7,1))]
 [(tuple([(vals[index],subtab[1]) for subtab in tab])) for index,tab in 
enumerate(tabs)]
[((1.0, 1), (1.0, 3), (1.0, 4)), ((2.3439, 2), 
(2.3439, 0), (2.3439, 9)), ((4.23420004, 4), 
(4.23420004, 3), (4.23420004, 1))]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Variables

2005-04-24 Thread R. C. James Harlow
On Sunday 24 April 2005 03:20, Richard Blackwood wrote:
 To All:

 Folks, I need your help. I have a friend who claims that if I write:

 foo = 5

 then foo is NOT a variable, necessarily. 

  This is a really amusingly recursive discussion. Your friend has a piece of 
knowledge, what a variable is and is not. He uses the word variable to 
refer to this piece of knowledge. Now, if in some parallel universe people 
used the word hairbrush to refer to this bit of knowledge, that wouldn't 
stop his argument having validity if he travelled to that universe - if it 
would then it's already invalidated, as there are more people in *this* 
universe who use the word variable to refer to foo than there are who insist 
that it's not correct, by a significant proportion. So by corollary your 
friend has already argued that one can use many different words to correctly 
refer to the same concept.

  I think your friend would also find it hard to disagree that a single word 
can have multiple meanings, like the example given of Domain in maths. I 
think he would have a similarly tough time saying that these words with 
multiple meanings were only allowable in maths, not in english or the 
offshoots of maths like programming. So there's no reason why the concept of 
what you call a variable and what he calls a variable shouldn't be different.

  In summary, the words he uses to describe variables are constants, but point 
at variables, which are different than the constants you use to describe 
variables, which point at variables, and vary from the variables that his 
variables point at.

  And if that last sentence doesn't convince him of the futility of trying to 
use natual language to communicate precise concepts, nothing will.




pgp4lqQYa6ygj.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Parsing data from URL

2005-04-24 Thread R. C. James Harlow
On Monday 25 April 2005 01:24, Harlin Seritt wrote:

 dat = urllib.urlopen(url, 'r').read()

Drop the 'r' - urlopen is posting the 'r' to the server, instead of doing what 
you mean, opening the file read-only.


pgpmZ2zcMs1bO.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: goto statement

2005-04-21 Thread R. C. James Harlow
On Thursday 21 April 2005 17:42, Maxim Kasimov wrote:
  Have you tried the triple quote comment technique?

 how do use this here:

Simple.

 sql = '''
 some long query
 '''

Change this to:

sql = 
some long query


since you shouldn't be using multiple quoting styles in one module, any more 
than you should be using multiple casing styles.

Then just put single quotes around the place where you want to comment. Not 
hard, is it?

james.


pgpEOkrYjHYZq.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Supercomputer and encryption and compression @ rate of 96%

2005-04-15 Thread R. C. James Harlow
On Thursday 14 April 2005 10:27, [EMAIL PROTECTED] wrote:
 Supercomputer and encryption and compression @ rate of 96%

snip

Dear Sir or Madam,
I have received notification that you posted a compression algorithm on 
the newsgroup comp.lang.python on or about 10:27:26 on the 04/14/2005. I am 
writing to you to inform you that the algorithm published infringes my 
zero-bit compression algorithm, US Pat No. 13375P33K, which details the 
transmission of information using no bandwidth to achieve a 100% 
compression. Please immediately follow up with a retraction of rights to this 
algorithm or you will hear from my solicitor, Mr J. Peasbody.

Yours in law,
James Harlow.

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


Re: Piping data into script under Win32

2005-04-15 Thread R. C. James Harlow
On Saturday 16 April 2005 03:11, runes wrote:
 I trying to figure out a way to make a python script accept data output
 from another process under Windows XP, but I fail miserably. I have a
 vague memory having read this is not possible with Python under
 Windows...

 C:\ type countlines.py | python countlines.py
 Counted 3 lines.

Are you definitely doing this and not:

C:\ type countlines.py | countlines.py

That will give you the error you're seeing. The example you posted works for 
me.


pgpG1kLcMgQ3U.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Piping data into script under Win32

2005-04-15 Thread R. C. James Harlow
On Saturday 16 April 2005 03:43, runes wrote:

 type countlines.py | python countlines.py = Success
 type countlines.py | countlines.py = Failure

 Why doesn't the latter work?

Don't quote me on this, but I think it's because invoking countlines.py 
involves running some sort of wrapper that discards its stdin and stdout.

If anyone has a more authorative answer, I'd like to know, because this caught 
me out too.

james.


pgp0UPpjLSwGF.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Supercomputer and encryption and compression @ rate of 96%

2005-04-14 Thread R. C. James Harlow
On Thursday 14 April 2005 22:18, Tiziano Bettio wrote:

 Actually your script doesn't work on my python distribution...

Works fine here - did you decompress the first bit of the python executable? 
You have to do that before Fredrick's script works...


pgpYFHzjRTUoB.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Supercomputer and encryption and compression @ rate of 96%

2005-04-14 Thread R. C. James Harlow
On Thursday 14 April 2005 22:21, R. C. James Harlow wrote:
 You have to do that before Fredrick's script works...

Damn - 'Fredrik's' - I accidentally decompressed his name.


pgpbUXNRRyNvA.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python license (2.3)

2005-04-12 Thread R. C. James Harlow
On Tuesday 12 April 2005 09:51, Antoon Pardon wrote:
 It seems I have to include the following in
 my code:

   Copyright (c) 2001, 2002 Python Software Foundation;
All Rights Reserved

 Do I understand correctly?

You are of course allowed to *add* your own copyright statement:

Copyright (c) 2001, 2002 Python Software Foundation;
All Rights Reserved
Copyright (c) 2005 Antoon Pardon;
All Rights Reserved


pgpzG0ivpEZo9.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: database in python ?

2005-04-11 Thread R. C. James Harlow
On Monday 11 April 2005 11:01, Pierre-Frédéric Caillaud wrote:
 psycopg ... has a dictfetchall() method which is worth its weight in
 donuts ! 

It's very simple to write one for MySQLdb:

def dictfetchall(cursor):
'''Takes a MySQLdb cursor and returns the rows as dictionaries.'''
col_names = [ d[0] for d in cursor.description ]
return [ dict(zip(col_names, row)) for row in cur.fetchall() ]

In truth, although postgres has more features, MySQL is probably better for 
someone who is just starting to use databases to develop for: the chances are 
higher that anyone using their code will have MySQL than Postgres, and they 
aren't going to need the features that Postgresql has that MySQL doesn't. 
IMO, this has changed since only a year or two ago, when MySQL didn't support 
foreign-key constraints.


pgpIeUfcyT5Ux.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list