Re: transforming list

2007-10-23 Thread sandipm
hi james,
  this is one implementation using python dictionaries.

report ={}
for row in data:
if not row[0] in report:
report[row[0]] = [row[0], row[1], 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0]
if row[2]:
report[row[0]][row[2]+1] = row[3]
reports = report.values()



regards,
Sandip More



james_027 wrote:
> hi,
>
> i have a list from a resultset like this
> 1,"Chicago Bulls",,,""
> 2,"Cleveland Caveliers",,,""
> 4,"Detroit Pistons",1,"23686386.35"
> 4,"Detroit Pistons",2,"21773898.07"
> 4,"Detroit Pistons",3,"12815215.57"
> 4,"Detroit Pistons",4,"48522347.76"
> 4,"Detroit Pistons",5,"28128425.99"
> 4,"Detroit Pistons",6,"15681603.08"
> 4,"Detroit Pistons",8,"12627725.03"
> 4,"Detroit Pistons",9,"11417.00"
> 4,"Detroit Pistons",10,"945689.22"
> 4,"Detroit Pistons",11,"818246.57"
> 4,"Detroit Pistons",12,"1154292.77"
> 5,"Phoenix Suns",1,"23211445.97"
> 5,"Phoenix Suns",3,"11268469.53"
> 5,"Phoenix Suns",4,"49913569.61"
> 5,"Phoenix Suns",5,"26035236.09"
> 5,"Phoenix Suns",7,"113953310.50"
> 5,"Phoenix Suns",8,"17091769.84"
> 5,"Phoenix Suns",10,"818569.99"
> 5,"Phoenix Suns",11,"824602.19"
> 5,"Phoenix Suns",12,"1142018.11"
>
> and I wish to transform it into something like this
>
> 1, "Chicago Bulls", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
> 2, "Cleveland Caveliers", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
> 4, "Detroit Pistons", 23686386.35, 21773898.07, 12815215.57,
> 48522347.76, 28128425.99, 15681603.08, 0, 12627725.03, 11417.00,
> 945689.22, 818246.57, 1154292.77
> 5, "Phoenix Suns", 23211445.97, 0, 11268469.53, 499113569.61,
> 26035236.09, 0, 113953310.50, 17091769.84, 0, 818569.99, 824602.19,
> 1142018.11
>
> currently my solution is something like this the rows is the original
> list which is list of list ... the report is a list of list with the
> new arrangement that I want.
>
> report = []
> report_item = None
> temp_c_id = 0
> for row in rows:
> if not row[0] == temp_c_id:
> temp_c_id = row[0]
> if report_item: report.append(report_item)
> report_item = [row[0], row[1], 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0]
> if row[2]:
> report_item[row[2]+1] = row[3]
>
> I feel this solution is not that good, can I make this more pythonic?!
>
> Thanks
> james :)

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


Re: Anagrams

2007-10-24 Thread sandipm
hi,
 Is "all" inbuilt function in python? what it does?


> from itertools import ifilter, count
>
> def anagram_finder():
>  primes = ifilter(lambda p: all(p % k for k in xrange(2, p)), count(2))
  
>  primeAlpha = dict(zip(string.lowercase, primes))
>  ...


--
sandip

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


Re: Anagrams

2007-10-24 Thread sandipm
thanks..I  am using python 2.4.4. so i couldnt find "all" either as
inbuilt module
or by doing "from itertools import *",  "all" is not available.
I think I need to move to 2.5 then

but what are the pros/cons of moving to 2.5? as we are using 2.4.4 on
production server which is quite stable.
any good pointers?

sandip




On Oct 24, 9:32 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> >>  Is "all" inbuilt function in python? what it does?
>
>  help(all)
> > Help on built-in function all in module __builtin__:
>
> > all(...)
> > all(iterable) -> bool
>
> > Return True if bool(x) is True for all values x in the iterable.
>
> It may be helpful to know that any() and all() were added in
> Python 2.5
>
> http://docs.python.org/lib/built-in-funcs.html#l2h-9
>
> -tkc


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


python in academics?

2007-10-29 Thread sandipm
seeing posts from students on group. I am curious to know, Do they
teach python in academic courses in universities?

in undergrad comp science courses,  We had scheme language as scheme
is neat and beautiful language to learn programming. We learnt other
languages ourselve with basics set right by scheme..



sandip

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


Re: How to find if a string contains another string

2007-10-29 Thread sandipm

you can use "find" function...which gives index of occurrence of
smaller one in bigger one
and return -1 if does not exists..


if bigstring.find(smallone) > -1:
return true
else:
return false


sandip




On Oct 30, 9:15 am, "Borse, Ganesh" <[EMAIL PROTECTED]>
wrote:
> Sorry, am getting this error.
>
> >>> bigstring="python anaconda boa cobra"
> >>> smallone="boa"
> >>> smallone in bigstring
>
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: 'in ' requires character as left operand
>
> >>> if smallone in bigstring:
>
> ...print 'ok'
> ... else:
> ... print 'nok'
> ...
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: 'in ' requires character as left operand
>
> Do I need to import any module?
>
> Thanks,
> Ganesh
>
> -Original Message-
> From: Gary Herron [mailto:[EMAIL PROTECTED]
> Sent: 30 October 2007 12:09
> To: Borse, Ganesh
> Cc: [EMAIL PROTECTED]
> Subject: Re: How to find if a string contains another string
>
> Borse, Ganesh wrote:
> > Hi,
>
> > Am new to python.
> > May someone please help me know this?
>
> > How can we check whether one big string contains another small string?
>
> > E.g.
> > bigstring="python anaconda boa cobra"
> > smallone="boa"
>
> Use the operator named in:
>
> >>> bigstring="python anaconda boa cobra"
> >>> smallone="boa"
> >>> smallone in bigstring
> True
>
> Often used in if statements like this:
>
> >>> if smallone in bigstring:
> ...   print 'contained'
> ... else:
> ...   print 'not'
> ...
> contained
>
> Gary Herron
>
> > If 0 == contains(bigstring,smallone):
> > print "Yes, boa is snake.."
>
> > Is there any function like "contains" or "exists" in python?
> > What are different alternatives we have?
>
> > Please help.
>
> > Thanks and Regards,
> > Ganesh
>
> > ==
> >  Please access the attached hyperlink for an important
> > electronic communications disclaimer:
>
> >http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> > ==
> > 
>
> ===­===
> Please access the attached hyperlink for an important electronic 
> communications disclaimer:
>
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> ===­===


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

Re: choose from a list

2007-11-01 Thread sandipm


one more way of connecting to sql.

MySQLdb.connect(client_flag=65536131072,cursorclass=cursors.DictCursor,host=HOST,port=3306,user=USER,passwd=PASSWD,db=DbName)
cursor = conn.cursor()

 in your case, only list of dictiories will be returned but
when query/stored procedure  returns more than one result set ...

cursor.execute(sql)
result = cursor.fetchall()
x = [result]
while (cursor.nextset()):
 nextres  =cursor.fetchall()
 if nextres!= ():
  x.append(nextres)


thanks
sandip



On Nov 1, 10:13 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> On Nov 1, 11:54 am, barronmo <[EMAIL PROTECTED]> wrote:
>
>
>
> > This is really remarkable.  My previous experience with programming
> > was in VB for Applications; doing the same thing seemed much more
> > complicated.  This little function is only about 15 lines of code and
> > it forms the basis for my entire application.  With a few simple
> > modifications I'll be able to get anything out of the database with a
> > minimum of entries from the user.
>
> > It turns out that 'results' was a tuple of dictionaries.  I got an
> > error trying to call the tuple; converting it to a list worked.  Here
> > is the current function:
>
> > import MySQLdb
>
> > def name_find(namefrag):
>
> >  conn = MySQLdb.connect(host = "localhost",
> >   user = "root",
> >   passwd = "Barron85",
> >   db = "meds")
> >  cursor = conn.cursor(MySQLdb.cursors.DictCursor)
> >  cursor.execute("SELECT patient_ID, firstname, lastname FROM
> > demographics WHERE lastname LIKE '%s%%'" % (namefrag))
>
> >  results = cursor.fetchall()
> >  for index, row in enumerate(results):
> >   print "%d %s   %s %s" % (index, row["patient_ID"],
> > row["firstname"], row["lastname"])
> >  indx = int(raw_input("Select the record you want: "))
> >  results_list = list(results)
> >  return results_list[indx]['patient_ID']
>
> >  cursor.close()
> >  conn.close()
>
> > This returns the patient_ID after selecting a name from the list, eg
> > 615L.  I'm not sure why the "L" is there but it shouldn't be hard to
> > remove.  
>
> It's a long integer. You don't have to worry about it:
>
> >>> a = long(615)
> >>> a
> 615L
> >>> print a
>
> 615
>
> Notice the L is gone when you go to use it:
>
> >>> print 'SELECT * FROM test WHERE pid=%s' % a
>
> SELECT * FROM test WHERE pid=615
>
> > Mensanator, thanks a lot for your help.  This has been quite
> > a lot to digest--huge leap in my understanding of Python.
>
> > Michael Barron
>
> > On Oct 31, 12:32 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>
> > > On Oct 30, 7:39?pm, barronmo <[EMAIL PROTECTED]> wrote:
>
> > > > I didn't know "result" was alist!
>
> > > I don't use MySQL but that's how others work.
> > > Eachlistitem is a record, each record a tuple
> > > of field values.
>
> > > > Can all that info be stored in alist?
>
> > > If you don't fetch too many records at once.
> > > This is a test of my word database using ODBC
> > > and MS-ACCESS (the SQL is very simple since
> > > all the actual work is done in MS-ACCESS, Python
> > > is just retrieving the final results).
>
> > > import dbi
> > > import odbc
> > > con = odbc.odbc("words")
> > > cursor = con.cursor()
> > > cursor.execute("SELECT * FROM signature_anagram_summary")
> > > results = cursor.fetchall()
>
> > > Here, results (the recipient of .fetchall) is alistof tuples.
> > > The contents are:
>
> > > [(9, 10, 'anoretics', '101010001110011100'),
> > > (9, 10, 'atroscine', '101010001110011100'),
> > > (9, 10, 'certosina', '101010001110011100'),
> > > (9, 10, 'creations', '101010001110011100'),
> > > (9, 10, 'narcotise', '101010001110011100'),
> > > (9, 10, 'ostracine', '101010001110011100'),
> > > (9, 10, 'reactions', '101010001110011100'),
> > > (9, 10, 'secration', '101010001110011100'),
> > > (9, 10, 'tinoceras', '101010001110011100'),
> > > (9, 10, 'tricosane', '101010001110011100')]
>
> > > > How do the columns work?
>
> > > I don't know, I don't get column names. It looked like
> > > from your example that you can use names, I would have
> > > to use indexes, such as results[3][2] to get 'creations'.
> > > Maybe MySQL returns dictionaries instead of tuples.
>
> > > > I was curious to see what the data
> > > > looked like but I can't seem to print "result" from the prompt.  Do
> > > > variables used inside functions live or die once the function
> > > > executes?
>
> > > Yeah, they die. You would have to have the function return
> > > the resultslistand indx, then you could use it's contents
> > > as criteria for further queries.
>
> > > So you might want to say
>
> > > name_find_results,indx = name_find(namefrag)
>
> > > > If they die, how do I get around this?
>
> > > Add 'return results,indx' to the function. Or better still,
> > > just return the record the user selected
> > > return results[indx]
> > > You wouldn't need indx

achieving performance using C/C++

2007-11-04 Thread sandipm
I did fair amount of programming in python but never used c/c++ as
mentioned below.
any good tutorials for using C/C++ to optimize python codebase for
performance?
how widely do they use such kind of mixed coding practices?

sandip

-- Forwarded message --
From: "D.Hering"
.
.
.
.
Python is very easily extended to near C speed. The Idea that FINALLY
sunk in, was that I should first program my ideas in Python WITHOUT
CONCERN FOR PERFOMANCE. Then, profile the application to find the
"bottlenecks" and extend those blocks of code to C or C++. Cython/
Pyrex/Sip are my preferences for python extension frameworks.
.
.
.
.

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


Re: __file__ vs __FILE__

2007-11-05 Thread sandipm
interestingly...
I wanted to reuse this code so i wrote function in a file

def getParentDir():
import os
return os.path.dirname(os.path.abspath(__file__))


and called this function, in another file, its giving me parent
directory of file where this function is defined.?
how to reuse this piece of code then? or am i doing something wrong?

btw using __path__[0], I can get the parent directory of file too...


sandip




On Nov 5, 5:09 am, Giampaolo Rodola' <[EMAIL PROTECTED]> wrote:
> On 3 Nov, 15:46, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
>
>
>
> > En Sat, 03 Nov 2007 10:07:10 -0300, Giampaolo Rodola' <[EMAIL PROTECTED]>  
> > escribió:
>
> > > On 3 Nov, 04:21, klenwell <[EMAIL PROTECTED]> wrote:
> > >> In PHP you have the __FILE__ constant which gives you the value of the
> > >> absolute path of the file you're in (as opposed to the main script
> > >> file.)
> > > This is not really 'one-line' since you have to import two modules
> > > first, but it looks nicer...:
>
> > > import sys, os
> > > print sys.argv[0] # absolute file name
> > > print os.path.dirname(sys.argv[0]) # absolute dir name
>
> > Note that this returns the location of the *main* script, not the current  
> > module, as the OP explicitely asked for.
>
> > --
> > Gabriel Genellina
>
> Whoops! You're right.


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

reading/writing files

2007-11-27 Thread sandipm
Hi,

  I am trying to read a file and write into other file. if I do it for
simple text file, it works well.
but for pdfs or some other mime types, its failing.

actually main problem is i am uploading file using cgi, in this
process I am getting content of file, and I am
trying to save the file. I think I need to specify mimetype of file
somehow..?
any quick solutions? or need to google.

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


Re: reading/writing files

2007-11-27 Thread sandipm
f1= open("file1.pdf", "rb")
x = f1.read()
open("file2.pdf", "wb").write(x)



works...

thanks
sandip

On Nov 27, 5:43 pm, sandipm <[EMAIL PROTECTED]> wrote:
> Hi,
>
>   I am trying to read a file and write into other file. if I do it for
> simple text file, it works well.
> but for pdfs or some other mime types, its failing.
>
> actually main problem is i am uploading file using cgi, in this
> process I am getting content of file, and I am
> trying to save the file. I think I need to specify mimetype of file
> somehow..?
> any quick solutions? or need to google.
>
> sandip

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


Re: Problem with parsing email message with extraneous MIME information

2007-12-27 Thread sandipm
I think I faced same problem quite sometime back...
but in our case, due to some settings in Microsoft outlook , forwarded
emails were  also coming as an attachment to email.

so That attachement itself has same format as email's format, so to
get information from attachment we needed to treat attachment as a
email to parse it.
can you send me some full email file? it will help to analyze
problem..


sandip





On Dec 21, 6:22 pm, "Steven Allport" <[EMAIL PROTECTED]> wrote:
> I am working on processing eml email message using the email module (python
> 2.5), on files exported from an Outlook PST file, to extract the composite
> parts of the email. In most instances this works fine, the message is read
> in using message_from_file, is_multipart returns True and I can process each
> component and extract message attachments.
>
> I am however running into problem with email messages that contain emails
> forwarded as attachments. The email has some additional encapulated header
> information from each of the forwared emails.When I processes the files
> is_multipart returns False the content-type is reported as text/plain
> and the payload includes all the message body from 'This message is in MIME
> format' though to the end.
>
> for example.
>
> 
> MIME-Version: 1.0
> X-Mailer: Internet Mail Service (5.5.2448.0)
> This message is in MIME format. Since your mail reader does not understand
> this format, some or all of this message may not be legible.
> --_=_NextPart_000_01C43634.1A06A235
> --_=_NextPart_001_01C43634.1A06A235
> --_=_NextPart_001_01C43634.1A06A235
> --_=_NextPart_001_01C43634.1A06A235--
> --_=_NextPart_000_01C43634.1A06A235
> 
> --_=_NextPart_002_01C43634.1A06A235
> --_=_NextPart_003_01C43634.1A06A235
> --_=_NextPart_003_01C43634.1A06A235
> --_=_NextPart_003_01C43634.1A06A235--
> --_=_NextPart_002_01C43634.1A06A235
> --_=_NextPart_002_01C43634.1A06A235--
> --_=_NextPart_000_01C43634.1A06A235
> Mime-Version: 1.0
> Content-Type: multipart/mixed;
>  boundary="m.182DA3C.BE6A21A3"
> 
>
> If I remove the section of the email from the 'This is in MIME format'
> through to Mime-Version: 1.0 the message is processed correctly. (ie.
> is_multipart = True , Content-Type = multipart/mixed etc.)
>
> Could anybody tell me if the above message header breaks the conventions for
> email messages or is it just some that is not handled correctly by the email
> module.
>
> I would appreciate any feedback from anyone else who has experienced such
> problems or could provide hints to a reliable solution.
>
> Thanks,
> Steve

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


python script as executable

2008-04-28 Thread sandipm
Hi,
I have written a python script to run from cron.
I  have put #!/usr/bin/env python at top. file executes correctly when
I run using python filename.py but
it fails to execute when try to run it like script/command.
it throws error:
:No such file or directory

I am editing file from eclipse for python from windows. and then
uploading on linus machine to run it.


any pointers?

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


Re: python script as executable

2008-04-28 Thread sandipm
thanks it worked

On Apr 29, 10:49 am, "Eric Wertman" <[EMAIL PROTECTED]> wrote:
> Try to ftp it in ascii mode, or find a dos2unix utility .. the file
> has probably got \r\n (windows) line terminators in it.. causes
> problems.   I guess it's also possible that /usr/bin/env doesn't
> exist... not likely though.
>
> On Tue, Apr 29, 2008 at 1:36 AM, sandipm <[EMAIL PROTECTED]> wrote:
> > Hi,
> >  I have written a python script to run from cron.
> >  I  have put #!/usr/bin/env python at top. file executes correctly when
> >  I run using python filename.py but
> >  it fails to execute when try to run it like script/command.
> >  it throws error:
> >  :No such file or directory
>
> >  I am editing file from eclipse for python from windows. and then
> >  uploading on linus machine to run it.
>
> >  any pointers?
>
> >  sandip
> >  --
> >  http://mail.python.org/mailman/listinfo/python-list

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


config files in python

2008-05-04 Thread sandipm
Hi,
 In my application, I have some configurable information which is used
by different processes. currently I have stored configration in a
conf.py file as name=value pairs, and I am importing conf.py file to
use this variable. it works well

import conf
print conf.SomeVariable

but if I need to change some configuration parameteres,  it would need
me to restart processes.

I want to store this data in some conf file (txt) and would like to
use it same way as I am using these variables as defined in py
files.

one solution I can think of is writing data as a dictionary into conf
file. and then by reading data, apply eval on that data. and update
local dict? but this is not a good solution

any pointers?

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


Re: config files in python

2008-05-05 Thread sandipm
Thanks for various useful suggestions.
actually right now I am using conf files only in psp handler of
mod_python/apache
but I have other processes which might use same config files.

One way is I can put conf related data directly in database and
database handling module can directly pickup values from db.
but the problem with that approach is that it is difficult in managing
changes from version control system.as I need to explicitly
create DB scripts to put it in version control and other devs require
them run those scripts.

but if I put those conf params in files it would be easy for me to
change params and useful for other developers
to integrate those changes in their dev environments without doing any
explicit ops.

here I would like to have python file which read conf from text file
and load those params in current process space.
so only importing that python file should read up the conf file and
load the current process with configurable parameters.
I thought this would be good way to do it, rather than getting
involved in slightly complicted reload mechanisms of python modules?



Regards,
Sandip

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