Re: [Tutor] Python and rpy

2006-12-22 Thread Geoframer

Thanks Kent that helps some, at least i can do the basic stuff i can do in R
now.

But you kinda hit the nail on the head with your statement "This seems to
work, it keeps a in the internal R representation instead
of converting it to a list of lists" This all started with me trying to get
R to do a kmeans algorithm on a list of lists (a list composed of vectors
containing integers). What i want to do is convert a python list of lists to
the approperiate R object so that i can use the r.kmeans algorithm on it.
I'll write the basic program below.

As i stated i think the conversion from Python to R is going wrong, but i
have no clue on how to properly address that.
The code snippet i was talking about is on page 15 and 16 of the rpy
reference guide http://rpy.sourceforge.net/rpy/doc/rpy.pdf ; the examples
just don't work and i am lacking enough python experience to see why :-S.

What i'm trying to do now is :


from rpy import *

class Test:
   def as_r(self):
   return [[1,2,3,4,5],[2,3,4,5,1],[3,4,5,1,2],[4,5,1,2,3],[5,1,2,3,4]]

if __name__ == "__main__":
   a=with_mode(NO_CONVERSION, Test)()
   r.kmeans(a, 2, 5, 10, "Forgy")


Which gives as a result :

RHOME= C:\Program Files\R\R-2.4.0
RVERSION= 2.4.0
RVER= 2040
RUSER= C:\Documents and Settings\Ronald
Loading the R DLL C:\Program Files\R\R-2.4.0\bin\R.dll .. Done.
Loading Rpy version 2040 .. Done.
Creating the R object 'r' ..  Done
Traceback (most recent call last):
 File "rpy-test2.py", line 9, in ?
   r.kmeans(a, 2, 5, 10, "Forgy")
rpy.RException: Error in as.double.default(x) : (list) object cannot be
coerced
to 'double'

Hope you can shed more light on it.

Many thanx for your efforts - Geofram


On 12/21/06, Kent Johnson <[EMAIL PROTECTED]> wrote:


Geoframer wrote:
> R is a statistical language and Rpy is the python interface for it.
> However somehow I'm failing to see a step in the python code with which
I
> address the R language.
>
> in R I can do :
>
> a=diag(10)  #produces an identity matrix of
> size 10
> b=kmeans(a,2,5,10,"Forgy")#calculate a kmeans clustering algorithm
> on the 10 vectors contained by the matrix just declared.
>
>
> in Ipython this does :
>
> -
> In [1]: from rpy import *
> RHOME= C:\Program Files\R\R-2.4.0
> RVERSION= 2.4.0
> RVER= 2040
> RUSER= C:\Documents and Settings\Ronald
> Loading the R DLL C:\Program Files\R\R-2.4.0\bin\R.dll .. Done.
> Loading Rpy version 2040 .. Done.
> Creating the R object 'r' ..  Done
>
> In [2]: a = r.diag(10)
>
> In [3]: b = r.kmeans(a,2,10,5,"Forgy")
>
---
> rpy.RExceptionTraceback (most recent
> call last)
>
> C:\Python24\
>
> RException: Error in as.double.default(x) : (list) object cannot be
> coerced to '
> double'
> -

This seems to work, it keeps a in the internal R representation instead
of converting it to a list of lists:

In [1]: from rpy import *
RHOME= C:\Program Files\R\R-2.3.1
RVERSION= 2.3.1
RVER= 2031
RUSER= G:\
Loading the R DLL C:\Program Files\R\R-2.3.1\bin\R.dll .. Done.
Loading Rpy version 2031 .. Done.
Creating the R object 'r' ..  Done

In [22]: aa=with_mode(NO_CONVERSION, r.diag)(10)

In [25]: b=r.kmeans(aa,2,10,5,"Forgy")

In [26]: b
Out[26]:
{'centers': [[0.,
   0.,
   0.,
   0.,
   0.0,
   0.,
   0.,
   0.,
   0.,
   0.],
  [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]],
  'cluster': [1, 1, 1, 1, 2, 1, 1, 1, 1, 1],
  'size': [9, 1],
  'withinss': [8.0018, 0.0]}

> I've tried numerous things to get it to work, but i basically can not
> find out how i do something as simple as the two statements in R in
> RPython. Apparently something is going wrong somewhere in the conversion
> of python objects to R objects but i can't seem to fix it. There is a
> code snippet in the RPy-reference manual but it's only valid for python
> 2.2 and 2.1 and i couldn't get it to work on 2.4.

Where is the snippet?

Cheers,
Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to permanently add a module path

2006-12-22 Thread shawn bright

lo there,

i am working with python in ubuntu, my app has some modules that i would
like to import from anywhere.
i can sys.path.append(my_module_dir)but it only lasts as long as that
python session.
how can i add a directory to the import path permantly

?

thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to permanently add a module path

2006-12-22 Thread Christopher Arndt
shawn bright schrieb:
> lo there,
> 
> i am working with python in ubuntu, my app has some modules that i would
> like to import from anywhere.
> i can sys.path.append(my_module_dir)but it only lasts as long as
> that python session.
> how can i add a directory to the import path permantly

http://article.gmane.org/gmane.comp.python.tutor/36916

See point 2)


Addendum:

To set the PYTHONPATH environment variable:

Windows: "Control panel/System/Advanced/Envirnment..." or something similar and
change/add value of PYTHONPATH (entries are separated by semicolons ';')

Linux:

Depends on your distribution:

- Standard way would be to edit ~/.bash_profile and add e.g.

# entries are separated by colons ':'
PYTHONPATH=$HOME/lib/python:/where/my/other/python/modules/live
export PYTHONPATH

but not all desktop environment read this file.

- On Debian, for the system wide environment, you can add the same snippet to
/etc/environment or add a file with the same contents to /etc/profile.d


HTH, Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Lists on the fly?

2006-12-22 Thread Carlos
Hello,

I am wondering if it is possible to create lists on the fly. The script 
that I'm working on needs a number of parameters, one of those is 
population, and this corresponds to the number of solutions that a 
genetic algorithm generates on each generation (iteration). The thing is 
that I need to generate one list for each population member and then 
append the corresponding population members to that particular list.

This is only so you can have an idea:

for i in range(5):
'list_%i' % (i) = []

or:

for i in range(5):
lista_+'%i' % (i) = []

:-[

Is this possible?

Thanks in advance,
And Merry Christmas,
Carlos


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-22 Thread Kent Johnson
Carlos wrote:
> Hello,
> 
> I am wondering if it is possible to create lists on the fly. The script 
> that I'm working on needs a number of parameters, one of those is 
> population, and this corresponds to the number of solutions that a 
> genetic algorithm generates on each generation (iteration). The thing is 
> that I need to generate one list for each population member and then 
> append the corresponding population members to that particular list.
> 
> This is only so you can have an idea:
> 
> for i in range(5):
> 'list_%i' % (i) = []
> 
> or:
> 
> for i in range(5):
> lista_+'%i' % (i) = []
> 
> :-[
> 
> Is this possible?

It is possible, using exec, but it is not the right solution to your 
problem. You want a bunch of lists each associated with a name. The way 
to do this is put the lists in a dict; the name is the key, the list is 
the value. Your example would look like this:

lists = {}
for i in range(5):
   lists['list_%i' % (i)] = []

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to permanently add a module path

2006-12-22 Thread Christopher Arndt
shawn bright schrieb:
> ok,
> i am on ubuntu and there is no /etc/profile.d directory
> i put this in the /etc/environment file
> 
> PYTHONPATH=/usr/lib/python2.4/site-packages/pivotrac
> export PYTHONPATH
> 
> but it doesn't seem to be working.
> There is no master python config file somewhere where all these are listed ?
> i wonder becuase i add modules via apt, and they are put where they need
> to be.

Ok, I see, you're trying to add you module in the standard site modules path
(/usr/lib/pythonX.Y/site-packages on Unices). This is an entirely different 
matter.

You have two possibilities:

1) Turn your module directory into a paackage by adding a '__init__.py' file to
it. This file may be empty. You can then iumport from your module like this:

from pivotrac import mymodule


For more information on Python module packages, see here:

http://www.python.org/doc/current/tut/node8.html#SECTION00840

2) You can add a .pth file to the site-packages directory that points to your
module directory. E.g. ass a file pivotrac.pth to
/usr/lib/python2.4/site-packages/ that just contains one line:

pivotrac

For more information about .pth files, see here:

http://www.python.org/doc/current/lib/module-site.html


With both methods you don't need to change PYTHONPATH at all.


Chris

P.S. Please don't answer privately to somebody writing on the list, unless
asked to do so. Reply to the list instead.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-22 Thread Carlos

Kent,

Thanks a lot, that solves it... again

Carlos
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-22 Thread Steve Oldner
Hi guys,

I am reading and doing examples from Python Web Programming and 
Have a question about the dictionary:

counter = {}
file = open("d:\myfile.txt") # the time has come the walrus said
while 1:
line = file.readline()
if line == "":
break
for w in line.split():
if counter.has_key(w):
counter[w] += 1
else:
counter[w] = 1
file.close()
words = counter.keys()
words.sort()
wor w in words:
print w, counter{w}

Output is:

Come 1
Has 1
Said 1
The 2
Time 1
Walaus 1 


Okay, I understand counter is set up as a dictionary and a dictionary
has a key and a value.
How does the dictionary get built?  I'm not following the logic in how
the key, the actual word, is having a value assigned.


Thanks,

SteveO 
  




-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Kent Johnson
Sent: Friday, December 22, 2006 10:38 AM
To: Carlos
Cc: tutor@python.org
Subject: Re: [Tutor] Lists on the fly?

Carlos wrote:
> Hello,
> 
> I am wondering if it is possible to create lists on the fly. The 
> script that I'm working on needs a number of parameters, one of those 
> is population, and this corresponds to the number of solutions that a 
> genetic algorithm generates on each generation (iteration). The thing 
> is that I need to generate one list for each population member and 
> then append the corresponding population members to that particular
list.
> 
> This is only so you can have an idea:
> 
> for i in range(5):
> 'list_%i' % (i) = []
> 
> or:
> 
> for i in range(5):
> lista_+'%i' % (i) = []
> 
> :-[
> 
> Is this possible?

It is possible, using exec, but it is not the right solution to your
problem. You want a bunch of lists each associated with a name. The way
to do this is put the lists in a dict; the name is the key, the list is
the value. Your example would look like this:

lists = {}
for i in range(5):
   lists['list_%i' % (i)] = []

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-22 Thread Bob Gailer
Carlos wrote:
> Hello,
>
> I am wondering if it is possible to create lists on the fly. 
This is an FAQ. The more general question is "Can I create variables 
with dynamically generated names." The answers are:
(1) Yes
(2) This is rarely a good idea.
The preferred solution is to use a dict or list, each item of which is 
one of the "variables" you want to create. Use a list if the names, like 
yours, are of the form

list_1, list_2, ..., list_n. Then refer to the list with n as the list index. 
To apply to your example:

list_ = [[] for x in xrange(5)] # creates [[], [], [], [], []].
Then you may refer to list_[0], list_[1], etc.
> The script 
> that I'm working on needs a number of parameters, one of those is 
> population, and this corresponds to the number of solutions that a 
> genetic algorithm generates on each generation (iteration). The thing is 
> that I need to generate one list for each population member and then 
> append the corresponding population members to that particular list.
>
> This is only so you can have an idea:
>
> for i in range(5):
> 'list_%i' % (i) = []
>
> or:
>
> for i in range(5):
> lista_+'%i' % (i) = []
>
> :-[
>
> Is this possible?
>
> Thanks in advance,
> And Merry Christmas,
> Carlos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   


-- 
Bob Gailer
510-978-4454

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to permanently add a module path

2006-12-22 Thread shawn bright

thanks, i usually intend to, getting used to gmail.
thanks for all the help, all working now.
shawn

On 12/22/06, Christopher Arndt <[EMAIL PROTECTED]> wrote:


shawn bright schrieb:
> ok,
> i am on ubuntu and there is no /etc/profile.d directory
> i put this in the /etc/environment file
>
> PYTHONPATH=/usr/lib/python2.4/site-packages/pivotrac
> export PYTHONPATH
>
> but it doesn't seem to be working.
> There is no master python config file somewhere where all these are
listed ?
> i wonder becuase i add modules via apt, and they are put where they need
> to be.

Ok, I see, you're trying to add you module in the standard site modules
path
(/usr/lib/pythonX.Y/site-packages on Unices). This is an entirely
different matter.

You have two possibilities:

1) Turn your module directory into a paackage by adding a '__init__.py'
file to
it. This file may be empty. You can then iumport from your module like
this:

from pivotrac import mymodule


For more information on Python module packages, see here:


http://www.python.org/doc/current/tut/node8.html#SECTION00840

2) You can add a .pth file to the site-packages directory that points to
your
module directory. E.g. ass a file pivotrac.pth to
/usr/lib/python2.4/site-packages/ that just contains one line:

pivotrac

For more information about .pth files, see here:

http://www.python.org/doc/current/lib/module-site.html


With both methods you don't need to change PYTHONPATH at all.


Chris

P.S. Please don't answer privately to somebody writing on the list, unless
asked to do so. Reply to the list instead.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-22 Thread Bob Gailer
Steve Oldner wrote:
> Hi guys,
>
> I am reading and doing examples from Python Web Programming and 
> Have a question about the dictionary:
>
> counter = {}
> file = open("d:\myfile.txt") # the time has come the walrus said
> while 1:
> line = file.readline()
> if line == "":
> break
> for w in line.split():
> if counter.has_key(w):
> counter[w] += 1
> else:
> counter[w] = 1
> file.close()
> words = counter.keys()
> words.sort()
> wor w in words:
> print w, counter{w}
>
> Output is:
>
> Come 1
> Has 1
> Said 1
> The 2
> Time 1
> Walaus 1 
>
> 
> Okay, I understand counter is set up as a dictionary and a dictionary
> has a key and a value.
> How does the dictionary get built?  I'm not following the logic in how
> the key, the actual word, is having a value assigned.
>   

counter[w] = 1 # if w is not a key in the dictionary, this assignment adds it, 
with a value of 1

> 


-- 
Bob Gailer
510-978-4454

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-22 Thread Steve Oldner
Thank you!  I makes sense now and shows me I need to research more on
file methods.  

 

-Original Message-
From: Bob Gailer [mailto:[EMAIL PROTECTED] 
Sent: Friday, December 22, 2006 11:46 AM
To: Steve Oldner
Cc: tutor@python.org
Subject: Re: [Tutor] Lists on the fly?

Steve Oldner wrote:
> Hi guys,
>
> I am reading and doing examples from Python Web Programming and Have a

> question about the dictionary:
>
> counter = {}
> file = open("d:\myfile.txt") # the time has come the walrus said while

> 1:
> line = file.readline()
> if line == "":
> break
> for w in line.split():
> if counter.has_key(w):
> counter[w] += 1
> else:
> counter[w] = 1
> file.close()
> words = counter.keys()
> words.sort()
> wor w in words:
> print w, counter{w}
>
> Output is:
>
> Come 1
> Has 1
> Said 1
> The 2
> Time 1
> Walaus 1
>
> 
> Okay, I understand counter is set up as a dictionary and a dictionary 
> has a key and a value.
> How does the dictionary get built?  I'm not following the logic in how

> the key, the actual word, is having a value assigned.
>   

counter[w] = 1 # if w is not a key in the dictionary, this assignment
adds it, with a value of 1

> 


--
Bob Gailer
510-978-4454
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-22 Thread Bob Gailer
Steve Oldner wrote:
> Thank you!  I makes sense now and shows me I need to research more on
> file methods. 
Good.

One other advisory: don't use file as a variable name. file is a 
built-in function; reassigning it makes the function unavailable.

-- 
Bob Gailer
510-978-4454

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] winreg question

2006-12-22 Thread Toon Pieton

Hey friendly users!

I'm trying to connect to a certain key in my registery using winreg.
However, I have run into some problems.

1) I'm trying to open HKEY_CURRENT_USER\Softwar\PartyGaming\PartyPoker. Is
this (see below) the correct way to open that key?
from winreg import *

PartyPoker = Key(
   HKCU,
   "Software\\PartyGaming\\PartyPoker"
   )

2) From this set of keys just opened, I want to get the data stored under
one called AppPath. How do I do that?

Thanks in advance!
Toon Pieton
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] winreg question

2006-12-22 Thread Terry Carroll
On Fri, 22 Dec 2006, Toon Pieton wrote:

> I'm trying to connect to a certain key in my registery using winreg.

I don't know winreg, but I know of _winreg; Same thing?  (I'm on
Activestate Python 2.4).

> from winreg import *

"from foo import *" is generally a bad idea, because you'll get a lot of 
names imported you may not be expecting.  Better to use:

  import winreg

and then prefix the imported names with "winreg."

> PartyPoker = Key(
> HKCU,
> "Software\\PartyGaming\\PartyPoker"
> )

Using _winreg, I'd do:

>>> import _winreg
>>> subkey = "Software\\Google\\Picasa\\Picasa2\\Runtime"
>>> #substitute your subkey above
>>> mykey = _winreg.OpenKey(
... _winreg.HKEY_CURRENT_USER,
... subkey)
>>>

> 2) From this set of keys just opened, I want to get the data stored under
> one called AppPath. How do I do that?

>>> (apppath, type) = _winreg.QueryValueEx(mykey, "AppPath")
>>> print apppath
C:\Program Files\Picasa2\Picasa2.exe
>>>

Of course, if your "winreg" is not the same as my "_winreg", none of this 
helps you!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] winreg question

2006-12-22 Thread Christopher Arndt
Toon Pieton schrieb:
> I'm trying to connect to a certain key in my registery using winreg.
> However, I have run into some problems.
> 
> 1) I'm trying to open HKEY_CURRENT_USER\Softwar\PartyGaming\PartyPoker.
> Is this (see below) the correct way to open that key?

See here for some examples of using the windows registry:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146305
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66011
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473846

> 2) From this set of keys just opened, I want to get the data stored
> under one called AppPath. How do I do that?

See for example the function '_get_reg_value' in the third recipe listed above.

Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] winreg question

2006-12-22 Thread Terry Carroll
On Fri, 22 Dec 2006, Terry Carroll wrote:

> On Fri, 22 Dec 2006, Toon Pieton wrote:
> 
> > I'm trying to connect to a certain key in my registery using winreg.
> 
> I don't know winreg, but I know of _winreg; Same thing?  (I'm on
> Activestate Python 2.4).

Ah, I see winreg is no doubt the third-party module described here:

http://www.rutherfurd.net/python/winreg/index.html

Based on this, I think what you want to do is:

import winreg
PartyPokerKey = Key(
 winreg.HKCU,
 "Software\\PartyGaming\\PartyPoker"
 )

PartyPokerAppPath = PartyPokerKey.values["AppPath"]

I can't be sure, though, because I don't have winreg installed to try it 
out.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-22 Thread Luke Paireepinart
Kent Johnson wrote:
> Carlos wrote:
>   
>> Hello,
>>
>> I am wondering if it is possible to create lists on the fly. The script 
>> that I'm working on needs a number of parameters, one of those is 
>> population, and this corresponds to the number of solutions that a 
>> genetic algorithm generates on each generation (iteration). The thing is 
>> that I need to generate one list for each population member and then 
>> append the corresponding population members to that particular list.
>>
>> This is only so you can have an idea:
>>
>> for i in range(5):
>> 'list_%i' % (i) = []
>> 
Note, though this is pretty arbitrary  :) and only saves you 2 characters...
You don't need to create a tuple if you're only packing in one value.
 >>> i = 1
 >>> 'list_%i' % i
'list_1'

works just fine.
HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor