Newbie: list comprehension troubles..

2009-08-23 Thread mm
Hi, I'm trying to replace this...

# this works but there must be a more pythonic way, right?
tlist = []
for obj in self.objs:
t = obj.intersect(ray)
if (t != None):
tlist.append((obj,t))

with a list comprehension- can it be done?

What I need to do is iterate over a list of graphics primitives and
call their intersect method. If the returned t value is not None then
I want to store the obj refernence and its t value in a list of tuples
for further processing. I've tried stuff like ...

tlist = [(obj,t) for obj,t in (self.objs, obj.intersect(ray))
if (t != None)]
tlist = [(obj,t) for obj in self.objs for t in obj.intersect
(ray) ]
print ">>> ",len(tlist), tlist

but they don't work. Any help greatly appreciated. matt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: csv to excel format problem

2008-10-14 Thread MM
On 14 Ott, 12:03, MM <[EMAIL PROTECTED]> wrote:
> Hi to all,
>
> I'm trying to import a tab separated values file onto Excel with the
> following script:
>
> import csv
> from pyExcelerator import *
>
> w = Workbook()
> worksheet = w.add_sheet('sim1')
>
> def writeExcelRow(worksheet, lno, columns):
>   style = XFStyle()
>   style.num_format_str = '0.00E+00'
>   cno = 0
>   for column in columns:
>     worksheet.write(lno, cno, column,style)
>     cno = cno + 1
>
> nrow = 0
> csvfile = file('res1.txt','r')
> csvreader = csv.reader(csvfile, delimiter='\t')
>
> for line in csvreader:
>     writeExcelRow(worksheet,nrow,line)
>     nrow += 1
>
> csvfile.close()
> w.save('numbers.xls')
>
> All goes well and the resulting file "numbers.xls" has all the numbers
> in the right place
>
> The problem is that excel sees the numbers as text and gives the error
> "numbers stored as text" that I have to correct manually.
>
> The file res1.txt has the structure of a tab separated values of
> floating point numbers.
>
> Thank you for the help.
>
> Marco

I've found the answer by myself...
Maybe for you it would be simple!


for line in csvreader:
writeExcelRow(worksheet,nrow,map(float,line))
nrow += 1


Thank you anyway
--
http://mail.python.org/mailman/listinfo/python-list


[NEWBIE] csv to excel format problem

2008-10-14 Thread MM
Hi to all,

I'm trying to import a tab separated values file onto Excel with the
following script:

import csv
from pyExcelerator import *

w = Workbook()
worksheet = w.add_sheet('sim1')

def writeExcelRow(worksheet, lno, columns):
  style = XFStyle()
  style.num_format_str = '0.00E+00'
  cno = 0
  for column in columns:
worksheet.write(lno, cno, column,style)
cno = cno + 1

nrow = 0
csvfile = file('res1.txt','r')
csvreader = csv.reader(csvfile, delimiter='\t')

for line in csvreader:
writeExcelRow(worksheet,nrow,line)
nrow += 1

csvfile.close()
w.save('numbers.xls')

All goes well and the resulting file "numbers.xls" has all the numbers
in the right place

The problem is that excel sees the numbers as text and gives the error
"numbers stored as text" that I have to correct manually.

The file res1.txt has the structure of a tab separated values of
floating point numbers.

Thank you for the help.

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


Re: code optimization (calc PI)

2007-01-03 Thread mm

Hmm... it's a question. It was not that easy to translate this [EMAIL 
PROTECTED] 
C-Program into readable code and then to Python. But it works.

There are only two while-loops (a while within an other while).

I konw, that for example while-loops in Perl are very slow. Maybe this 
is also known in Pyhton. Then, I can translate the while-loops in to 
for-loops, for example.
More general, maybe there is a speed optimazation docu out there.

(Anyway, this code for C-calc is complex. And I realy don't understand 
it right now... 8-)


But anyway, there is not that much calculation, it has more something to 
do with the too while-loops. Here is some code:

(In general, it has basically nothing to do with PI-calc.)


c=2800  ## a counter


while c*2:
## do some calc. did not change c here.
...


b=c  ## number of elements

while (b-1):
   ## so some calc.
   ...
   b=b-1 ## just for while-loop condition.


   c = c-14;  ## this is code vor the 1st while-loop, BUT bust run after 
the 2nd-while-loop.
   pi = pi + str("%04d" % int(e + d/a))  ## this should be fast?! I dont 
know.


There are a output string, a list, and integers. No complex data 
structures; and no complex calculations.



Diez B. Roggisch wrote:

> mm wrote:
> 
> 
>>(Yes, I konw whats an object is...)
>>BTW. I did a translation of a pi callculation programm in C to Python.
>>(Do it by your own... ;-)
> 
> 
> Is that a question on how to optimize code you won't show us? If yes, I'm
> sorry to tell you that crystal balls are short these days. Too much
> new-year-outlooks.
> 
> Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


C/C++, Perl, etc. to Python converter

2007-01-03 Thread mm

Is there a Perl to Python converter?
Or in general: a XY to Python converter?

Is see, that Python is much better then Perl anyway.
But for beginners, they whant to konw how is this done with Python etc.

Sure, there are some docus out there in the internet. But a converter?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unsubscribing from the list

2007-01-03 Thread mm
Fredrik Lundh wrote:

> if you're unable to follow written instructions, how on earth did you manage
> to subscribe to this list ?
> 
>  
> 
> 
> 

*lol*   Just click ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: array of class / code optimization

2007-01-03 Thread mm

Yes, it was the (), equivalent to thiks like new() create new object 
from class xy.
>   s1.append(Word)
s1.append(Word())

But I was looking for a "struct" equivalent like in c/c++.
And/or "union". I can't find it.

Maybe you know a source (URL) "Python for c/c++ programmers" or things 
like that.


Yes, I konw whats an object is...
-- 
http://mail.python.org/mailman/listinfo/python-list


code optimization (calc PI)

2007-01-03 Thread mm
(Yes, I konw whats an object is...)
BTW. I did a translation of a pi callculation programm in C to Python. 
(Do it by your own... ;-)

Calc PI for 800 digs(?). (german: Stellen)
--
int a=1,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;
 for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,
 f[b]=d%--g,d/=g--,--b;d*=b);}

$ ./a.exe
31415926535897932384626433832795028841971693993751058209749445923078164062862089
98628034825342117067982148086513282306647093844609550582231725359408128481117450
28410270193852110555964462294895493038196442881097566593344612847564823378678316
52712019091456485669234603486104543266482133936072602491412737245870066063155881
74881520920962829254091715364367892590360011330530548820466521384146951941511609
43305727036575959195309218611738193261179310511854807446237996274956735188575272
48912279381830119491298336733624406566430860213949463952247371907021798609437027
70539217176293176752384674818467669405132000568127145263560827785771342757789609
17363717872146844090122495343014654958537105079227968925892354201995611212902196
0864034418159813629774771309960518707211349983729780499510597317328160963185


But Python is much slower here then C here. I used a while-loop within a 
while-loop. Not that much calculation here.
-- 
http://mail.python.org/mailman/listinfo/python-list


array of class

2007-01-02 Thread mm

How can I do a array of class?

s1=[]  ## this array should hold classes

## class definition
class Word:
   word=""


## empty words... INIT
for i in range(100):  ## 0..99
   s1.append(Wort)

s1[0].word="There"
s1[1].word="should"
s1[2].word="be"
s1[3].word="different"
s1[4].word="classes"

... but it's not.


print s1

[,
,
,
,
,
,

---

Here, this "classes" are all at the same position in memory. So there 
are no different classes in the array.

So I access with s1[0], s1[1], s1[2], etc. always the same data.

Any idea?

--
Michael

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


Re: Question about tuple lengths

2005-12-14 Thread MM Zeeman
Carl J. Van Arsdall wrote:

> 
>  From my interpreter prompt:
> 
>  >>> tuple = ("blah")
>  >>> len(tuple)
> 4
>  >>> tuple2 = ("blah",)
>  >>> len (tuple2)
> 1
> 
> So why is a tuple containing the string "blah" without the comma of
> length four? Is there a good reason for this or is this a bug?
> 

Hello,

Thats because the expression ("blah") actually resolves to "blah" instead of
a tuple containing the string "blah".

>>> type(("spam"))


Adding a comma after spam results in the tuple being created.

>>> type(("spam",))


And to make things even more confusing, just adding a comma without braces
will give you a tuple too.

>>> "spam",
('spam',)

The explanation for this all can be found at:
http://docs.python.org/ref/parenthesized.html

Regards,

Maas


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


odbc and python

2005-06-02 Thread MM
Are there any other odbc packages other than the win32all and mxodbc 
ones? The win32all odbc.pyd can't access table structure info like 
SQLColumns, and mxobdc requires a commercial license which is 
unjustifiable for this tiny project. Any other OS alternatives for 
win32?. Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: array of bits?

2005-02-14 Thread MM
Thanks people. List it is. matthew.
--
http://mail.python.org/mailman/listinfo/python-list


array of bits?

2005-02-14 Thread MM
Hi,
What is the best structure/way to create an array of bits (actually 
true/false flags) of an arbitrary length ranging from about 20 upto 
about 500. Speed of access more of an issue than compactness.

eg:
[0] 0
[1] 0
[2] 1
[3] 0
[4] 1
...
[n] 0
etc.
Thanks for your input and advice. matthew.
--
http://mail.python.org/mailman/listinfo/python-list


win32 extension install hiccup

2005-02-13 Thread MM
Hi,
I downloaded the latest win32all build 202 and tried to install under 
win2000 with Py2.4. Install complains about 'couldn't open py2.4 to run 
script pywin32-preinstall.py'. I checked the directories and there was 
no sign of this file (preinstall.py) so I presume this is why it bombed. 
How do I fix this? Thanks, matthew.
--
http://mail.python.org/mailman/listinfo/python-list