[Tutor] c++ data types in python script
Hi people, I've used SWIG module to embed python inside c++ app. I pass a list of objects (with lots of different properties of types string, float, custom types like URL, Software and finally of list of strings). Now I'm in python. URL and Software has str() method that converts their value to string recognizable by JSON. But the problem is with list of strings. So, as I said I passed std::list myObjects to python function. Then I iterate it and for each object in myObjects I create a python copy (serialize it) to be able to put into JSON format and store in appropriate file. object has property benchmarks of type list. I do: ... class PythonObject: def __init__(self, object): self.benchmarks = list() for s in object.benchmarks: self.benchmarks.append(s) ... and it fails, also I do: ... class PythonObject: def __init__(self, object): self.benchmarks = [unicode(s) for s in object.benchmarks] ... and it fails, also I do: ... class PythonObject: def __init__(self, object): for s in object.benchmarks: print s[0] + s[1] + s[2] print type(s) ... and it fails printing wor Segmentation fault (core dumped) $ also I do: ... class PythonObject: def __init__(self, object): self.benchmarks = unicode(object.benchmarks) ... and it does not fail, instead it puts in JSON this string: ... "benchmarks": " > *' at 0xb63ed4e8>>", ... but it is not what I need What I'm trying to stress is that c++ objects should be converted (serialized) before putting them into json. Otherwise type errors occur and process fails. I love learning python and hope somebody may suggest me or tell something. Thank you all anyway! Arthur ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ data types in python script
Arthur Mc Coy, 06.03.2011 09:56: I've used SWIG module to embed python inside c++ app. Given that this deals with an advanced topic (C-level extensions), I find comp.lang.python (python-list), where you also posted this, a more appropriate place for discussion than the Python tutor mailing list. So I suggest people answer on c.l.py. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ data types in python script
Thank you, sorry for duplicating. I hope moderators can delete it if needed. Wish you well, Arthur On Sun, Mar 6, 2011 at 11:18 AM, Stefan Behnel wrote: > Arthur Mc Coy, 06.03.2011 09:56: > > I've used SWIG module to embed python inside c++ app. >> > > Given that this deals with an advanced topic (C-level extensions), I find > comp.lang.python (python-list), where you also posted this, a more > appropriate place for discussion than the Python tutor mailing list. So I > suggest people answer on c.l.py. > > Stefan > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] BLAS Implementation on Python
Hello, I am thinking of implementing a BLAS package in pure python. I am wondering if this is a good idea. My design goals are: [1] Efficient manipulation of Matrices and Vectors using pure python objects and python code. [2] Targetted to run on Python3 [3] Extensive use of defensive programming style [4] To serve as a reference design for future High Performance Code in Python [5] To serve as a reference material in classroom courses on numerical computing or for hobbyist programmers Thanks, Mahesh Narayanamurthi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] calculate the sum of a variable - python
Hi : I'm a Senior SAS Analyst. I'm trying to learn Python. I would appreciate if anybody could help me with this. It works fine if I give input instead of reading a text file. I don't understand where I'm going wrong. I'm trying to read a text file and find out the following: 1. Sum of amt for each id 2. Count of id 3. minimum of date1 4. maximum of date1 Here is the sample text file: test.txt file: bin1cd1 date1 amt cdid cd2 452 2 2010-02-20 $23.26 0810005954206107 452 2 2010-02-20 $20.78 0 810005954206107 452 2 2010-02-24 $5.99 2 810083974520151 452 2 2010-02-12 $114.25 7 810083974598101 452 2 2010-02-06 $28.00 0 810114236206032 452 2 2010-02-09 $15.01 0 810027445306040 452 18 2010-02-13 $113.24 0 810027445306040 452 2 2010-02-13 $31.80 0 810027445306040 Here is the code I've tried out to calculate sum of amt by id: import sys from itertools import groupby from operator import itemgetter t = () tot = [] for line in open ('test.txt','r'): aline = line.rstrip().split() a = aline[5] b = (aline[3].strip('$')) t = (a,b) t1 = str(t) tot.append(t1) print tot def summary(data, key=itemgetter(0), value=itemgetter(1)): for k, group in groupby(data, key): yield (k, sum(value(row) for row in group)) if __name__ == "__main__": for id, tot_spend in summary(tot, key=itemgetter(0), value=itemgetter(1)): print id, tot_spend Error: Traceback (most recent call last): File "", line 2, in File "", line 3, in summary TypeError: unsupported operand type(s) for +: 'int' and 'str' Thanks, Sree. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculate the sum of a variable - python
On Sun, Mar 6, 2011 at 9:31 PM, nookasree ponamala wrote: > Hi : > > I'm a Senior SAS Analyst. I'm trying to learn Python. I would appreciate if > anybody could help me with this. It works fine if I give input instead of > reading a text file. I don't understand where I'm going wrong. > > I'm trying to read a text file and find out the following: > 1. Sum of amt for each id > 2. Count of id > 3. minimum of date1 > 4. maximum of date1 > > Here is the sample text file: > > test.txt file: > > bin1cd1 date1 amt cdid cd2 > 452 2 2010-02-20 $23.26 0810005954206107 > 452 2 2010-02-20 $20.78 0 810005954206107 > 452 2 2010-02-24 $5.99 2 810083974520151 > 452 2 2010-02-12 $114.25 7 810083974598101 > 452 2 2010-02-06 $28.00 0 810114236206032 > 452 2 2010-02-09 $15.01 0 810027445306040 > 452 18 2010-02-13 $113.24 0 810027445306040 > 452 2 2010-02-13 $31.80 0 810027445306040 > > > Here is the code I've tried out to calculate sum of amt by id: > > import sys > from itertools import groupby > from operator import itemgetter > t = () > tot = [] > for line in open ('test.txt','r'): >aline = line.rstrip().split() >a = aline[5] >b = (aline[3].strip('$')) >t = (a,b) >t1 = str(t) >tot.append(t1) >print tot > def summary(data, key=itemgetter(0), value=itemgetter(1)): >for k, group in groupby(data, key): >yield (k, sum(value(row) for row in group)) > > if __name__ == "__main__": >for id, tot_spend in summary(tot, key=itemgetter(0), > value=itemgetter(1)): >print id, tot_spend > > > Error: > Traceback (most recent call last): > File "", line 2, in > File "", line 3, in summary > TypeError: unsupported operand type(s) for +: 'int' and 'str' > Of course I first have to commend you for including the full traceback with the code because it makes this entirely easy to answer. In general, the traceback tells you the most important stuff last, so I'll start with this line: > TypeError: unsupported operand type(s) for +: 'int' and 'str' That tells us that the problem is you are trying to use + (addition) on an integer and a string - which you can't do because of the type mismatch (TypeError). The next line > File "", line 3, in summary tells us that the error occurred on line3 in summary: 1 | def summary(data, key=itemgetter(0), value=itemgetter(1)): 2 |for k, group in groupby(data, key): 3 |yield (k, sum(value(row) for row in group)) Well, there's no '+', but you do have 'sum', which uses addition under the hood. So how do you go about fixing it? Well, you change the value getting passed to sum to an integer (or other number): sum(int(value(row)) for row in group) Should either fix your problem, or throw a differen error if you try to convert a string like 'Hello' to an integer. (Alternatively, use float if you're interested in decimals) HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculate the sum of a variable - python
Thanks for the reply Wayne, but still it is not working, when I used int It throws the below error: File "", line 2, in File "", line 3, in summary File "", line 3, in ValueError: invalid literal for int() with base 10: "'" I tried using float and the error is: Traceback (most recent call last): File "", line 2, in File "", line 3, in summary File "", line 3, in ValueError: invalid literal for float(): ' Thanks, Sree. --- On Mon, 3/7/11, Wayne Werner wrote: From: Wayne Werner Subject: Re: [Tutor] calculate the sum of a variable - python To: "nookasree ponamala" Cc: tutor@python.org Date: Monday, March 7, 2011, 9:14 AM On Sun, Mar 6, 2011 at 9:31 PM, nookasree ponamala wrote: Hi : I'm a Senior SAS Analyst. I'm trying to learn Python. I would appreciate if anybody could help me with this. It works fine if I give input instead of reading a text file. I don't understand where I'm going wrong. I'm trying to read a text file and find out the following: 1. Sum of amt for each id 2. Count of id 3. minimum of date1 4. maximum of date1 Here is the sample text file: test.txt file: bin1 cd1 date1 amt cd id cd2 452 2 2010-02-20 $23.26 0 8100059542 06107 452 2 2010-02-20 $20.78 0 8100059542 06107 452 2 2010-02-24 $5.99 2 8100839745 20151 452 2 2010-02-12 $114.25 7 8100839745 98101 452 2 2010-02-06 $28.00 0 8101142362 06032 452 2 2010-02-09 $15.01 0 8100274453 06040 452 18 2010-02-13 $113.24 0 8100274453 06040 452 2 2010-02-13 $31.80 0 8100274453 06040 Here is the code I've tried out to calculate sum of amt by id: import sys from itertools import groupby from operator import itemgetter t = () tot = [] for line in open ('test.txt','r'): aline = line.rstrip().split() a = aline[5] b = (aline[3].strip('$')) t = (a,b) t1 = str(t) tot.append(t1) print tot def summary(data, key=itemgetter(0), value=itemgetter(1)): for k, group in groupby(data, key): yield (k, sum(value(row) for row in group)) if __name__ == "__main__": for id, tot_spend in summary(tot, key=itemgetter(0), value=itemgetter(1)): print id, tot_spend Error: Traceback (most recent call last): File "", line 2, in File "", line 3, in summary TypeError: unsupported operand type(s) for +: 'int' and 'str' Of course I first have to commend you for including the full traceback with the code because it makes this entirely easy to answer. In general, the traceback tells you the most important stuff last, so I'll start with this line: > TypeError: unsupported operand type(s) for +: 'int' and 'str' That tells us that the problem is you are trying to use + (addition) on an integer and a string - which you can't do because of the type mismatch (TypeError). The next line > File "", line 3, in summary tells us that the error occurred on line3 in summary: 1 | def summary(data, key=itemgetter(0), value=itemgetter(1)): 2 | for k, group in groupby(data, key): 3 | yield (k, sum(value(row) for row in group)) Well, there's no '+', but you do have 'sum', which uses addition under the hood. So how do you go about fixing it? Well, you change the value getting passed to sum to an integer (or other number): sum(int(value(row)) for row in group) Should either fix your problem, or throw a differen error if you try to convert a string like 'Hello' to an integer. (Alternatively, use float if you're interested in decimals) HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculate the sum of a variable - python
On Sun, Mar 6, 2011 at 10:46 PM, nookasree ponamala wrote: > Thanks for the reply Wayne, but still it is not working, > > when I used int It throws the below error: > File "", line 2, in > File "", line 3, in summary > File "", line 3, in > ValueError: invalid literal for int() with base 10: "'" > It gives you a single-quote character that apparently you are trying to turn into a number. I'm not exactly sure what the problem is there - you should probably unwrap your generator expression into the equivalent loop(s) and either insert some strategic print statements or use a debugger, such as the python debugger. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculate the sum of a variable - python
On Sun, Mar 6, 2011 at 8:46 PM, nookasree ponamala wrote: > Thanks for the reply Wayne, but still it is not working, > > when I used int It throws the below error: > File "", line 2, in > File "", line 3, in summary > File "", line 3, in > ValueError: invalid literal for int() with base 10: "'" > > I tried using float and the error is: > Traceback (most recent call last): > File "", line 2, in > File "", line 3, in summary > File "", line 3, in > ValueError: invalid literal for float(): ' > > Thanks, > Sree. > > I played with it a bit and simplified things a (little) bit: >b = (aline[3].strip('$')) >t = (a, float(b)) >tot.append(t) >print tot > You were converting the tuple to a string before adding it to the list; you don't need to do that, and it was concealing the real cause of your problem, which is that you either need to skip/get rid of the top line of your file, or write some error-handling code to deal with it. Currently, you're trying to convert the string 'amt' into a number, and you just can't do that. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculate the sum of a variable - python
Thanks a lot Marc. This works now. Sree. --- On Mon, 3/7/11, Marc Tompkins wrote: From: Marc Tompkins Subject: Re: [Tutor] calculate the sum of a variable - python To: "nookasree ponamala" Cc: "Wayne Werner" , tutor@python.org Date: Monday, March 7, 2011, 10:54 AM On Sun, Mar 6, 2011 at 8:46 PM, nookasree ponamala wrote: Thanks for the reply Wayne, but still it is not working, when I used int It throws the below error: File "", line 2, in File "", line 3, in summary File "", line 3, in ValueError: invalid literal for int() with base 10: "'" I tried using float and the error is: Traceback (most recent call last): File "", line 2, in File "", line 3, in summary File "", line 3, in ValueError: invalid literal for float(): ' Thanks, Sree. I played with it a bit and simplified things a (little) bit: b = (aline[3].strip('$')) t = (a, float(b)) tot.append(t) print tot You were converting the tuple to a string before adding it to the list; you don't need to do that, and it was concealing the real cause of your problem, which is that you either need to skip/get rid of the top line of your file, or write some error-handling code to deal with it. Currently, you're trying to convert the string 'amt' into a number, and you just can't do that. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculate the sum of a variable - python
On Sun, Mar 6, 2011 at 11:27 PM, nookasree ponamala wrote: > Thanks a lot Marc. This works now. > > Sree. > > Glad to hear it. Welcome to the list, by the way. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] help with re module and parsing data
Hello all I am doing some analysis on my trace file. I am finding the lines Recvd-Content and Published-Content. I am able to find those lines but the re module as predicted just gives the word that is being searched. But I require the entire line similar to a grep in unix. Can some one tell me how to do this. I am doing the following way. import re file = open('file.txt','r') file2 = open('newfile.txt','w') LineFile = ' ' for line in file: LineFile += line StripRcvdCnt = re.compile('(P\w+\S\Content|Re\w+\S\Content)') FindRcvdCnt = re.findall(StripRcvdCnt, LineFile) for SrcStr in FindRcvdCnt: file2.write(SrcStr) Thanks Vin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor