[Tutor] How do I make a variable that refers to an object have the same label as the object's name ?

2006-10-05 Thread tpc247
question for the Python crowd:when creating a series of objects, where each object represents a field from a submitted html form, how do I make variables that reference the objects be the same as the names of the respective objects ?
For example, I have a class called Datum:class Datum:    def __init__(self, key, value, required=None, label=None):    self.key = key    self.value = value    self.required = required
    self.label = labeland a dictionary of submitted values:some_dict = {'applicant_full_name':'John Smith', 'applicant_address_line_1':'100 Main Street', 'applicant_city':'Anytown', 'applicant_state':'WY', 'applicant_postal_code':'5', 'country':'US', '
applicant_email':'[EMAIL PROTECTED]', 'applicant_telephone':'555 555 '} and I created a list of objects like so:some_list = []for key, value in some_dict.items():
    some_datum = Datum(key, value)    some_list.append(some_datum)so now I have a list of Datum instances called some_list, and I would like to make it so I can refer to each respective Datum instance by its 
Datum.key, e.g., applicant_full_name would refer to the Datum instance with key 'applicant_full_name' and applicant_full_name.value would give me "John Smith."  I tried:for x in some_list:    some_string = "%s = %s"  % (
x.key, x)    eval(some_string)and got:Traceback (most recent call last):  File "", line 4, in -toplevel-    eval(some_string)  File "", line 1
    applicant_state = <__main__.Datum instance at 0x009745F8>    ^SyntaxError: invalid syntaxAm I doing something wrong ?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I make a variable that refers to an object have thesame label as the object's name ?

2006-10-05 Thread Alan Gauld
> when creating a series of objects, where each object represents a 
> field from
> a submitted html form, how do I make variables that reference the 
> objects be
> the same as the names of the respective objects ?

The usual solution for this is to use a dictionary keyed by object 
name.
There is an example of this technique in my OOP topic in my tutor
using bank accounts keyed by account number - under the heading
"Collections of Objects".

There are other solutions based on modifying the global namerspace
but thats messy IMHO and makes your code very dependant on the
pre-knowledge of the fields passed which can introduce maintenance
problems later.


Using a dictionary your code becomes:

> class Datum:
>def __init__(self, value, required=None, label=None):
>self.value = value
>self.required = required
>self.label = label
>
> and a dictionary of submitted values:
>
> some_dict = {'applicant_full_name':'John Smith',
> 'applicant_address_line_1':'100 Main Street', 
> 'applicant_city':'Anytown',
> 'applicant_state':'WY', 'applicant_postal_code':'5', 
> 'country':'US', '
> applicant_email':'[EMAIL PROTECTED]', 'applicant_telephone':'555 555 
> '}
>
> and I created a list of objects like so:
>
> some_list = {}
> for key, value in some_dict.items():
>some_list[key] = Datum(value)

> to make it so I can refer to each respective Datum instance by its 
> Datum.key,
> e.g., applicant_full_name would refer to the Datum instance with key
> 'applicant_full_name' and applicant_full_name.value would give me 
> "John
> Smith."  I tried:

print some_list['applicant_full_name'].value

> for x in some_list:
>some_string = "%s = %s"  % (x.key, x)
>eval(some_string)

You would need exec() not eval but...
eval and exec both have serious security implications and should
be avoided where possible. Hopefully the dictionary technique achieves 
what you want?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


[Tutor] module loading

2006-10-05 Thread halamillo
Newbie BioGrad student here (again):

First, thanks for the help on the root node variable last time. It was
most helpful!

Now, I am trying to load a new module into MacPython, but after doing $
python setup.py install, or $ python setup.py build, then $ python
setup.py

the files on the module I downloaded are the following:

DEVELOPERS  Legal.htm   PKG-INFOSrc
customize.pyrpm_build.sh
DemoLib PackagesTest   
makeclean.shrpm_install.sh
INSTALL MANIFESTREADME  build  
makedist.batsetup.cfg
Include MiscRPM.README  changes.txt
makedist.sh setup.py


The INSTALL file says I don't need to configure the module if I'm running
versions greater than OSX, which I am, but when I run the setup file in
the last line of a bunch of erros it tells me the gcc failed.


Thanks again for your help. You guys rock.



Hugo Alamillo
265 Eastlick
Biological Sciences
PO Box 644236
Pullman, WA 99164-4236
509-432-6869
https://www.wsu.edu/~halamillo/


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


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-05 Thread Asrarahmed Kadri
Its something very close.
What I want is:
 
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
 
This can be done using arrays, right or is there any other way??
My logic in C is:
 
int arr[5][5];
for (i=0;i<5;i++)
{
   flag = 0;
   for (j=0;j<=i;j++)
{
   if (flag == 0 || j == i)  // print 1 if the array element is the first or the last number
 {
  arr[i][j] = 1;
  flag = 1;
    }
  arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
 
thanks.
   
On 10/4/06, Matthew White <[EMAIL PROTECTED]> wrote:
Asrarahmed,How about something like this:>>> arr = []>>> arr.append(['Blue', 'Yellow', 'Green', 'Brown', 'White'])
>>> arr[0][0]'Blue'>>> arr.append(['Up', 'Down', 'Left', 'Right', 'Center'])>>> arr[1][4]'Center'Is this what you were thinking of?-mtwOn Wed, Oct 04, 2006 at 06:38:09PM +0100, Asrarahmed Kadri (
[EMAIL PROTECTED]) wrote:> I am looking for something like this:>> int arr[5][5]; // C statement; declaring an array of 5 * 5 size>> Is there an easy and clean way to achieve this in python???
>> Thanks On 10/4/06, Kent Johnson <[EMAIL PROTECTED]> wrote:> >> >Asrarahmed Kadri wrote:> >>> >> Hi folks,
> >>> >> I am stuck.. Please help me with implementing two dimensional array in> >> Python.> >> >Nested lists are the simplest way though beware the gotcha shown here:
> >> >http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list> >
> >You can also use a dict with tuple indices, e.g.> >d[1,2] = 3> >> >Numpy is an extension designed for high-performance numerical work, it> >includes a multidimensional array type:
> >http://numpy.scipy.org//> >> >Kent> >>  --> To HIM you shall return.> ___
> Tutor maillist  -  Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] from string to variable name

2006-10-05 Thread frank h.
hello, i have a string variable that  contains a name that I want to use as a variablenameputting aside questions of why I would like to do that - i this possible at all?so I want to assign a value to a variable whos name is available only as a string to me.
that variable does not exist yet in the local namespace.from: t = "myvar"to: myvar = 3is this possible? something like setattr?thanks for any insight you might have-frank

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


Re: [Tutor] from string to variable name

2006-10-05 Thread Luke Paireepinart
frank h. wrote:
> hello, i have a string variable that  contains a name that I want to 
> use as a variablename
> putting aside questions of why I would like to do that - i this 
> possible at all?
>
> so I want to assign a value to a variable whos name is available only 
> as a string to me.
> that variable does not exist yet in the local namespace.
>
> from: t = "myvar"
> to: myvar = 3
>
> is this possible? something like setattr?
> thanks for any insight you might have
I think the point of dictionaries is to get this same basic 
functionality without polluting the namespaces.
You won't let us ask you 'why' you want to do this,
but I'll ask you: Why don't you want to use a dictionary?
Do you want to know if it's _Possible_ just so you'll know,
or do you actually want to use this for something?
If you just want to know if it's possible, I believe it is.
But consider:
if you don't know the variable name until runtime,
how are you going to refer to the variable later in your code?
It would be, insofar as I can tell, useless to do this.
> -frank
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-05 Thread Luke Paireepinart
Asrarahmed Kadri wrote:
> Its something very close.
> What I want is:
>  
> 1
> 1 1
> 1 2 1
> 1 3 3 1
> 1 4 6 4 1
>  
> This can be done using arrays, right or is there any other way??
They're not arrays, they're lists!
arrays imply contiguous memory blocks and single data types.
Lists are neither of these.
> My logic in C is:
>  
> int arr[5][5];
> for (i=0;i<5;i++)
> {
>flag = 0;
>for (j=0;j<=i;j++)
> {
>if (flag == 0 || j == i)  // print 1 if the array element is the 
> first or the last number
>  {
>   arr[i][j] = 1;
>   flag = 1;
> }
>   arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
> }
Yes, you could do this in Python.
I'm guessing it'd be something like this:

lst = []
for x in range(5):
tmp = []
for y in range(x):
   if y == 0:
  tmp.append(1)
   else:
  tmp.append(tmp[y-1]) #this line would be different
lst.append(tmp)

That's the best I can do.

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


Re: [Tutor] module loading

2006-10-05 Thread Mike Hansen
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED]
> Sent: Thursday, October 05, 2006 12:06 AM
> To: tutor@python.org
> Subject: [Tutor] module loading
> 
> Newbie BioGrad student here (again):
> 
> First, thanks for the help on the root node variable last time. It was
> most helpful!
> 
> Now, I am trying to load a new module into MacPython, but 
> after doing $
> python setup.py install, or $ python setup.py build, then $ python
> setup.py
> 
> the files on the module I downloaded are the following:
> 
> DEVELOPERS  Legal.htm   PKG-INFOSrc
> customize.pyrpm_build.sh
> DemoLib PackagesTest   
> makeclean.shrpm_install.sh
> INSTALL MANIFESTREADME  build  
> makedist.batsetup.cfg
> Include MiscRPM.README  changes.txt
> makedist.sh setup.py
> 
> 
> The INSTALL file says I don't need to configure the module if 
> I'm running
> versions greater than OSX, which I am, but when I run the 
> setup file in
> the last line of a bunch of erros it tells me the gcc failed.
> 
> 
> Thanks again for your help. You guys rock.
> 
> 
> 
> Hugo Alamillo
> 265 Eastlick
> Biological Sciences
> PO Box 644236
> Pullman, WA 99164-4236
> 509-432-6869
> https://www.wsu.edu/~halamillo/
> 
> 

You might try asking this on the python mac mail list. They are
extremely helpful.

http://www.python.org/community/sigs/current/pythonmac-sig/

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


Re: [Tutor] CGKit

2006-10-05 Thread Carlos
Hi Kent,

Probably I'm totally missing something, but...

I think there is not a compiled plug-in, if you go here 
http://cgkit.sourceforge.net/#download you are going to see that : " The 
Maya Python package (i.e. the Maya plug in and the actual Python 
package) is a separate package that can be downloaded from the above 
page as well. Currently, only the sources are available."

The pages that you are pointing are installation instructions, but they 
not point to the compiled plug-in. If you go to the project download 
page   you will see the cgkit and 
maya sections, inside the maya section there are some files, but none of 
them is the plug-in itself, only the sources (I think). I have found 
that you can compile the plug-ins with scons instead of boost and seems 
to be easier. I just need to now how to set some paths. Here is what the 
readme of the file says:


"The plugin is built using SCons (www.scons.org). If necessary, you can 
do customizations in a file config.py where you can set additional 
include paths and library paths in the lists CPPPATH and LIBPATH. Example:

CPPPATH = ["...path1...", "...path2...", ...]
LIBPATH = ["...path1...", "...path2...", ...]

You have to make sure that the Maya SDK and the Python SDK can be found. 
To compile the plugin you have to call "scons" in the root directory of 
the sourcepy plugin (where the SConstruct file is located). If 
everything went fine, the result will be in the bin subdirectory (the 
file will be called sourcepy.mll or sourcepy.so) from where you can copy 
it to any convenient place where Maya is able to find it."

I hope that someone can tell me how to do this.
Carlos



""

Thanks
Carlos

Kent Johnson wrote:
> Carlos wrote:
>> Thanks Luke and Kent,
>>
>> To get CGKit running I need to do two different things:
>>
>> First the Python Computer Graphics Kit is composed by an 
>> "cgkit-2.0.0alpha6.win32-py2.4.exe" file and some dependencies that 
>> are PyProtocols, numarray, PyOpenGL, PIL, pygame, PyODE and pySerial, 
>> with the exception of PyProtocols I have installed all of them. When 
>> I try to install PyProtocols from the command prompt it tells me that 
>> " The .NET Framework SDK needs to be installed before building 
>> extensions for Python." I went to Microsoft and installed the 
>> "Microsoft .NET Framework SDK v2.0", but the problem persists.
>>
>> The second part is the Python Maya Plug-In. I think that this is the 
>> tricky one, it is asking me to install Py++, that I have already 
>> installed. Now my problem comes when I have to compile the source 
>> code with Boost.Python, this one asks for Boost Jam and Boost. I have 
>> been trying to install this things but still have no clue at how.
>
> Use the compiled plug-in. This page:
> http://cgkit.sourceforge.net/mayadoc/install.html
> says to copy sourcepy.mll to the Maya plug-in directory.
>
> Python extensions for Windows are usually distributed as binaries 
> because so many people do not have the resources and knowledge to 
> compile them. I don't see anything in the Maya plugin docs to indicate 
> that you have to build it from source.
>
> Kent
>
>
>


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


[Tutor] [Fwd: Re: from string to variable name]

2006-10-05 Thread Luke Paireepinart

OOPS, forwarding my reply to frank.  I accidentally sent it straight to him.
--- Begin Message ---

frank h. wrote:
why I am not using a dictionary: well I am lazy, I do not want to 
explicitly construct a dictionary :)

You don't have to explicitly construct it.
You can add stuff to it.
If by explicitly construct you meant
d = {'a':'b','b':'c'} or something.
you can just do
d = {}
d['a'] = 'b'
d['b'] = 'c'
etc.
Why is this too hard?
it seems like it'd be faster than saying
tmp = 'myvar'
#put something here that converts the string 'myvar' into a variable name,
#such as exec() or other scary things :)
myvar = 3

I have a parameterized string template, I publish it using

  print mytemplate % locals()

This looks frighteningly dangerous!
So you have to make sure you don't define any variables before this 
statement?
That sounds like a bad idea to me, no matter how much more convenient it 
is :)


that saves me explicetly collecting all name value pairs in a 
dictionary first (there are quite a few)
well given my current roadblock and your answer, it looks like I am 
better of creating a dictionary, so I can then do

yes, create a dictionary, please!  :D

In general, when you're presented with a problem like this, consider,
if it's hard to do, either
1) It's just hard to do.
or
2) It's hard to do because you _shouldn't be doing it_ and there's an 
easier way you're supposed to be using.

I think this is a case for #2.

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


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-05 Thread Asrarahmed Kadri
Thank you so much.
I am trying to implement. I hope I can do it.
 
A gentle note: This is not a homework question.
I am learning python as a part of my Project work.
 
On 10/5/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
Asrarahmed Kadri wrote:> Its something very close.> What I want is:>> 1> 1 1
> 1 2 1> 1 3 3 1> 1 4 6 4 1>> This can be done using arrays, right or is there any other way??They're not arrays, they're lists!arrays imply contiguous memory blocks and single data types.
Lists are neither of these.> My logic in C is:>> int arr[5][5];> for (i=0;i<5;i++)> {>flag = 0;>for (j=0;j<=i;j++)> {>if (flag == 0 || j == i)  // print 1 if the array element is the
> first or the last number>  {>   arr[i][j] = 1;>   flag = 1;> }>   arr[i][j] = arr[i-1][j-1] + arr[i-1][j];> }Yes, you could do this in Python.I'm guessing it'd be something like this:
lst = []for x in range(5):   tmp = []   for y in range(x):  if y == 0: tmp.append(1)  else: tmp.append(tmp[y-1]) #this line would be different   lst.append(tmp)
That's the best I can do.HTH,-Luke-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me with two dimensional arrays in Python

2006-10-05 Thread Kent Johnson
Asrarahmed Kadri wrote:
> Its something very close.
> What I want is:
>  
> 1
> 1 1
> 1 2 1
> 1 3 3 1
> 1 4 6 4 1
>  
> This can be done using arrays, right or is there any other way??

Is this a homework assignment?

A list of lists seems like the appropriate way to represent this, 
especially since it is not actually a square array.

Here is a simpler example that might give you some ideas:
In [5]: b=[]

In [6]: for i in range(5):
...: b.append(range(i))
...:
...:

In [7]: b
Out[7]: [[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]

Kent

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


Re: [Tutor] CGKit

2006-10-05 Thread Kent Johnson
Carlos wrote:
> Hi Kent,
> 
> Probably I'm totally missing something, but...

No, your analysis looks correct to me. Sorry, this goes beyond my 
ability to help you. Is there a Maya or CGKit mailing list where you can 
ask? The compiled plugin may be available from another source.

Kent

> 
> I think there is not a compiled plug-in, if you go here 
> http://cgkit.sourceforge.net/#download you are going to see that : " The 
> Maya Python package (i.e. the Maya plug in and the actual Python 
> package) is a separate package that can be downloaded from the above 
> page as well. Currently, only the sources are available."
> 
> The pages that you are pointing are installation instructions, but they 
> not point to the compiled plug-in. If you go to the project download 
> page   you will see the cgkit and 
> maya sections, inside the maya section there are some files, but none of 
> them is the plug-in itself, only the sources (I think). I have found 
> that you can compile the plug-ins with scons instead of boost and seems 
> to be easier. I just need to now how to set some paths. Here is what the 
> readme of the file says:
> 
> 
> "The plugin is built using SCons (www.scons.org). If necessary, you can 
> do customizations in a file config.py where you can set additional 
> include paths and library paths in the lists CPPPATH and LIBPATH. Example:
> 
> CPPPATH = ["...path1...", "...path2...", ...]
> LIBPATH = ["...path1...", "...path2...", ...]
> 
> You have to make sure that the Maya SDK and the Python SDK can be found. 
> To compile the plugin you have to call "scons" in the root directory of 
> the sourcepy plugin (where the SConstruct file is located). If 
> everything went fine, the result will be in the bin subdirectory (the 
> file will be called sourcepy.mll or sourcepy.so) from where you can copy 
> it to any convenient place where Maya is able to find it."
> 
> I hope that someone can tell me how to do this.
> Carlos
> 
> 
> 
> ""
> 
> Thanks
> Carlos
> 
> Kent Johnson wrote:
>> Carlos wrote:
>>> Thanks Luke and Kent,
>>>
>>> To get CGKit running I need to do two different things:
>>>
>>> First the Python Computer Graphics Kit is composed by an 
>>> "cgkit-2.0.0alpha6.win32-py2.4.exe" file and some dependencies that 
>>> are PyProtocols, numarray, PyOpenGL, PIL, pygame, PyODE and pySerial, 
>>> with the exception of PyProtocols I have installed all of them. When 
>>> I try to install PyProtocols from the command prompt it tells me that 
>>> " The .NET Framework SDK needs to be installed before building 
>>> extensions for Python." I went to Microsoft and installed the 
>>> "Microsoft .NET Framework SDK v2.0", but the problem persists.
>>>
>>> The second part is the Python Maya Plug-In. I think that this is the 
>>> tricky one, it is asking me to install Py++, that I have already 
>>> installed. Now my problem comes when I have to compile the source 
>>> code with Boost.Python, this one asks for Boost Jam and Boost. I have 
>>> been trying to install this things but still have no clue at how.
>> Use the compiled plug-in. This page:
>> http://cgkit.sourceforge.net/mayadoc/install.html
>> says to copy sourcepy.mll to the Maya plug-in directory.
>>
>> Python extensions for Windows are usually distributed as binaries 
>> because so many people do not have the resources and knowledge to 
>> compile them. I don't see anything in the Maya plugin docs to indicate 
>> that you have to build it from source.
>>
>> 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


[Tutor] Luke, thanks a lot , here is the perfect code (as per ur suggestion)

2006-10-05 Thread Asrarahmed Kadri
#Implementation of Pascal Triangle
num_of_lines = input("How many lines you want to display")
list1 = []
for i in range(num_of_lines):    flag = 0    tmp = []    for j in range(i+1):    if flag == 0 or j == i:    tmp.append(1)    # for the first or teh last element of the line    flag = 1
    else:    tmp.append(list1[i-1][j-1]+list1[i-1][j])   # for rest, add teh numbers in previous  row  list1.append(tmp) 
# this code prints the Pascal triangle
for i in range(num_of_lines):    for j in range(i+1):    print list1[i][j],    print
Output for num_lines = 5:
11 11 2 11 3 3 11 4 6 4 1-
THNAKS ONCE MORE. 
On 10/5/06, Asrarahmed Kadri <[EMAIL PROTECTED]> wrote:

Thank you so much.
I am trying to implement. I hope I can do it.
 
A gentle note: This is not a homework question.
I am learning python as a part of my Project work.

 
On 10/5/06, Luke Paireepinart <[EMAIL PROTECTED]
> wrote: 
Asrarahmed Kadri wrote:> Its something very close.> What I want is:>> 1> 1 1 
> 1 2 1> 1 3 3 1> 1 4 6 4 1>> This can be done using arrays, right or is there any other way??They're not arrays, they're lists!arrays imply contiguous memory blocks and single data types. 
Lists are neither of these.> My logic in C is:>> int arr[5][5];> for (i=0;i<5;i++)> {>flag = 0;>for (j=0;j<=i;j++)> {>if (flag == 0 || j == i)  // print 1 if the array element is the 
> first or the last number>  {>   arr[i][j] = 1;>   flag = 1;> }>   arr[i][j] = arr[i-1][j-1] + arr[i-1][j];> }Yes, you could do this in Python.I'm guessing it'd be something like this: 
lst = []for x in range(5):   tmp = []   for y in range(x):  if y == 0: tmp.append(1)  else: tmp.append(tmp[y-1]) #this line would be different   lst.append(tmp) 
That's the best I can do.HTH,-Luke-- 
To HIM you shall return. -- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Fwd: Re: from string to variable name]

2006-10-05 Thread Python
On Thu, 2006-10-05 at 09:17 -0500, Luke Paireepinart wrote:
(actually from Frank)
> > I have a parameterized string template, I publish it using
> >
> >   print mytemplate % locals()
Original post asked to create variable in local namespace from string.
> so I want to assign a value to a variable whos name is available only
> as a string to me. 
> that variable does not exist yet in the local namespace.
> 
> from: t = "myvar"
> to: myvar = 3

You can do something like:
templatevars = dict(locals())   # copy of locals
templatevars[t] = 3
print mytemplate % templatevars

It is OK for templatevars to have extra variables.  You will get a
KeyError if templatevars is missing a value.

-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Luke, thanks a lot , here is the perfect code (as per ur suggestion)

2006-10-05 Thread Kent Johnson
Asrarahmed Kadri wrote:
> #Implementation of Pascal Triangle
> 
> num_of_lines = input("How many lines you want to display")
> 
> list1 = []
> 
> for i in range(num_of_lines):
> flag = 0
> tmp = []
> for j in range(i+1):
> if flag == 0 or j == i:
> tmp.append(1)# for the first or teh last element of the line
> flag = 1
> else:
> tmp.append(list1[i-1][j-1]+list1[i-1][j])   # for rest, add 
> teh numbers in previous  row
>   
> list1.append(tmp)

That's good. You could shorten it a little by getting rid of flag and 
just testing for j==0 or j==i. Here is another way to do it, it requires 
priming the output with the first row but the loop is a little shorter:

In [1]: binom = [[1]]

In [3]: for i in range(5):
...: prev = binom[i]
...: next = [1] + [ prev[j]+prev[j+1] for j in 
range(len(prev)-1) ] + [1]
...: binom.append(next)
...:
...:

In [4]: binom
Out[4]: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 
10, 10, 5, 1]]

There's a truly hideous one-liner hidden in your solution. It uses 
nested list comprehensions, a hack to refer to the list being built 
within a list comp, and the new conditional expression. Be careful who 
you show this too, it may scare dogs and small children ;) It does 
actually work:

list1 = [ [ locals()["_[1]"][i-1][j-1]+locals()["_[1]"][i-1][j] if (j != 
0 and j != i) else 1 for j in range(i+1) ] for i in range(num_of_lines) ]

Now we know why conditional expressions were added to Python 2.5! See 
this recipe for details about the locals() hack:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297

Sorry I couldn't resist :-)
Kent

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


[Tutor] OT: Book(s) about linux

2006-10-05 Thread Bernard Lebel
Hello,

Sorry to use this list for such an OT subject. But I want to get hands
down with Linux, and am looking for a book or two on the subject.

I'm looking for information about installation, configuration,
optimisation, and management of the Linux OS.



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


Re: [Tutor] [Fwd: Re: from string to variable name]

2006-10-05 Thread Kent Johnson
Python wrote:
> On Thu, 2006-10-05 at 09:17 -0500, Luke Paireepinart wrote:
> (actually from Frank)
>>> I have a parameterized string template, I publish it using
>>>
>>>   print mytemplate % locals()
> Original post asked to create variable in local namespace from string.
>> so I want to assign a value to a variable whos name is available only
>> as a string to me. 
>> that variable does not exist yet in the local namespace.
>>
>> from: t = "myvar"
>> to: myvar = 3
> 
> You can do something like:
>   templatevars = dict(locals())   # copy of locals
>   templatevars[t] = 3
>   print mytemplate % templatevars
> 
> It is OK for templatevars to have extra variables.  You will get a
> KeyError if templatevars is missing a value.

A slight variation, if you have multiple values you want to accumulate 
in a dict, would be something like this using dict.update():

templatevars = {}
...
templatevars['t'] = 3
...
templatevars['u'] = 4
...
templatevars.update(locals())
print mytemplate % templatevars

Kent

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


Re: [Tutor] OT: Book(s) about linux

2006-10-05 Thread Dave Kuhlman
On Thu, Oct 05, 2006 at 11:33:17AM -0400, Bernard Lebel wrote:
> Hello,
> 
> Sorry to use this list for such an OT subject. But I want to get hands
> down with Linux, and am looking for a book or two on the subject.
> 
> I'm looking for information about installation, configuration,
> optimisation, and management of the Linux OS.
> 

You are right.  It's off-topic for us.

Once you have picked a Linux distribution, you are likely to get
more help at the Web site dedicated to that distribution.  For
example, I'm on Ubuntu/Kubuntu, so http://www.ubuntu.com/
is helpful.

For lots of documentation, start here:

http://www.linux.org/books/index.html
http://www.linux.org/docs/
http://tldp.org/

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: Book(s) about linux

2006-10-05 Thread Python
On Thu, 2006-10-05 at 11:33 -0400, Bernard Lebel wrote:
> Hello,
> 
> Sorry to use this list for such an OT subject. But I want to get hands
> down with Linux, and am looking for a book or two on the subject.
> 
> I'm looking for information about installation, configuration,
> optimisation, and management of the Linux OS.

If you are new to Linux, you're likely to be best helped by a book that
matches your distribution, e.g. "Ubuntu Linux" or "Ubuntu Unleashed".
(Not to push you to Ubuntu; I'm running Fedora myself.)  These books
provide shallow coverage of everything and will give the background
needed to make good use of the system documentation.  They will also
point you to yum, synaptic, smart or whatever package management system
makes sense with your chosen distribution.  The package managers greatly
simplify system administration.

There are books devoted to packages: SSH, Sendmail, DNS/Bind, Apache,
Samba, etc. so you may need to get additional books depending on how you
use your computer.  "Linux in a Nutshell" (mine is 5th edition) provides
a useful reference.  There are also books like "Linux Debugging and
Performance Tuning" which are more system oriented.  "Moving to the
Linux Business Desktop" is useful for finding your way among all of the
available desktop applications.  Linux is a pretty broad subject so
there's a huge range of books from which to pick.

> 
> 
> 
> Thanks
> Bernard
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] What is a Python "project"?

2006-10-05 Thread Dick Moores
At 03:00 PM 10/3/2006, Brian van den Broek wrote:
>I've never used Wing, but unless its `project' concept is radically
>different than many other editors, it isn't about organizing the files
>on disk. Rather, it is about organizing a group of files into a
>collection the editor can open in one swell foop. The idea is to
>liberate you from having to recall just where the files live and allow
>you to merely open a group of related files and get on with what
>you're up to. (It is sort of like saving a group of tabs in firefox.)
>
>Best,
>
>Brian vdB

I thought I'd check with Wingware support:

email to Winware support
I asked the Python Tutor list what a project is, and got many answers.
Does this one correctly characterize what a project means with
WingIDE?

"I've never used Wing, but unless its `project' concept is radically
different than many other editors, it isn't about organizing the files
on disk. Rather, it is about organizing a group of files into a
collection the editor can open in one swell foop. The idea is to
liberate you from having to recall just where the files live and allow
you to merely open a group of related files and get on with what
you're up to. (It is sort of like saving a group of tabs in firefox.)"

Thanks,

Dick Moores
===

=reply from Wingware support
Not quite.  A project is the set of files that you write that compose an
application -- or a set of related scripts.  They're files that are
related to one, well, project ;).  Wing displays all of the files in the
project pane, lets you search all files in the project easily, displays
all classes defined in the project, among other things (at least in the
Pro version).  Project files also can share common setting such as
python executable, environment variable.  One file may also be set as
the main debug file so it's the one always run when you press the start
debug icon or F5.

You might have all files in a project open at once in a small project,
but you probably don't want them all open once you get over maybe 10 files.

John
=

===email to Wingware support===
Thanks for all that info, John. But is his point correct that the
files in a project don't need to be together in the same folder,
nor do they need to be moved at all in order to participate in the
project (I've fleshed his point out a bit)?

Dick
==

=reply from Wingware support
Yes, the files don't need to be in the same folder.  As a practical
matter, it's a good idea to put them in a small number of folders
though.  Otherwise, they'd be scattered all over the disk.

Cheers,

John
===

Dick Moores

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


Re: [Tutor] CGKit

2006-10-05 Thread Carlos
Hi Kent,

Thanks for the info, I will send a mail to the plug-in developer.

I didnt mention this before, Im an architect and my master thesis 
semester is beginning now. Last semester I was introduced to what is 
somehow known as morphogenetic design, to programming and to python. 
 From that semester a small python script came, that generated a 
structure composed by struts and tensor cables inside of maya, anyway, 
this semester I want to use programming again, this stuff has really 
convinced me that can be useful in the design field. My idea is to 
create a script that will deal with the spatial organization of a 
housing block, it would be very interesting to see it generating a 
number of solutions and evolving them a number of times until it can 
find an "optimum" solution.

What I would like to know is, does this sounds too complex for a 
newcomer like me? If by chance anyone has some experience that can share 
I would be grateful.

I know that this is a little off topic, in the future I will try to keep 
this more centered on python.

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


Re: [Tutor] Luke, thanks a lot , here is the perfect code (as per ur suggestion)

2006-10-05 Thread Asrarahmed Kadri

On 10/5/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
Asrarahmed Kadri wrote:> #Implementation of Pascal Triangle>> num_of_lines = input("How many lines you want to display")
>> list1 = []>> for i in range(num_of_lines):> flag = 0> tmp = []> for j in range(i+1):> if flag == 0 or j == i:> tmp.append(1)# for the first or teh last element of the line
> flag = 1> else:> tmp.append(list1[i-1][j-1]+list1[i-1][j])   # for rest, add> teh numbers in previous  row>> list1.append(tmp)That's good. You could shorten it a little by getting rid of flag and
just testing for j==0 or j==i. Here is another way to do it, it requirespriming the output with the first row but the loop is a little shorter:In [1]: binom = [[1]]In [3]: for i in range(5):   ...: prev = binom[i]
   ...: next = [1] + [ prev[j]+prev[j+1] for j inrange(len(prev)-1) ] + [1]   ...: binom.append(next)   ...:   ...:In [4]: binomOut[4]: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5,
10, 10, 5, 1]]There's a truly hideous one-liner hidden in your solution. It usesnested list comprehensions, a hack to refer to the list being builtwithin a list comp, and the new conditional _expression_. Be careful who
you show this too, it may scare dogs and small children ;) It doesactually work:
 
What is this??? I cannot understand a single character.. Explain this in length.
list1 = [ [ locals()["_[1]"][i-1][j-1]+locals()["_[1]"][i-1][j] if (j !=
0 and j != i) else 1 for j in range(i+1) ] for i in range(num_of_lines) ]Now we know why conditional expressions were added to Python 2.5! Seethis recipe for details about the locals() hack:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297Sorry I couldn't resist :-)Kent___
Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OT: Book(s) about linux

2006-10-05 Thread Jonathon Sisson
Hello Bernard...

Just to give you a pointer about Linux:  If you're new, Fedora and
Ubuntu are both relatively easy to learn, but powerful (I've never used
Ubuntu (or Debian, for that matter), but I hear that Ubuntu is a really
great distro).  Stay away from Slackware and Gentoo, at least until
you've had the chance to familiarize yourself with Linux.  I made the
mistake of attempting a stage 1 Gentoo install on a PII machine with
hardly any experience.  (Fortunately, the PII was so slow that I had
time to read the manual completely before each step).  It booted up fine
when I was done, but it was rough (even with extensive documentation ).

I'm not saying that Slackware and Gentoo are useless, as both are
*extremely* powerful OS's (Gentoo is my favorite OS other than OpenBSD).
 They do require quite a bit of knowledge just to get them installed,
however, and therefore are not good choices for someone just beginning
with Linux.

Jonathon


Python wrote:
> On Thu, 2006-10-05 at 11:33 -0400, Bernard Lebel wrote:
>> Hello,
>>
>> Sorry to use this list for such an OT subject. But I want to get hands
>> down with Linux, and am looking for a book or two on the subject.
>>
>> I'm looking for information about installation, configuration,
>> optimisation, and management of the Linux OS.
> 
> If you are new to Linux, you're likely to be best helped by a book that
> matches your distribution, e.g. "Ubuntu Linux" or "Ubuntu Unleashed".
> (Not to push you to Ubuntu; I'm running Fedora myself.)  These books
> provide shallow coverage of everything and will give the background
> needed to make good use of the system documentation.  They will also
> point you to yum, synaptic, smart or whatever package management system
> makes sense with your chosen distribution.  The package managers greatly
> simplify system administration.
> 
> There are books devoted to packages: SSH, Sendmail, DNS/Bind, Apache,
> Samba, etc. so you may need to get additional books depending on how you
> use your computer.  "Linux in a Nutshell" (mine is 5th edition) provides
> a useful reference.  There are also books like "Linux Debugging and
> Performance Tuning" which are more system oriented.  "Moving to the
> Linux Business Desktop" is useful for finding your way among all of the
> available desktop applications.  Linux is a pretty broad subject so
> there's a huge range of books from which to pick.
> 
>>
>>
>> Thanks
>> Bernard
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] from string to variable name

2006-10-05 Thread Jonathon Sisson
By "string variable that contains a name that I want to use as a
variablename" do you mean something like this:

myString = "rotationalSpeed"
rotationalSpeed = 4500

??

In Python a dictionary is an excellent solution to this problem.  The
only other way to accomplish this (to my knowledge) is in PHP (not
trying to steer you away from Python, just giving some info):

var myString = "rotationalSpeed";
$$myString = 4500;
echo $rotationalSpeed;

results in 4500.  This is called "variable variables" in PHP and it can
get hairy if done in a sloppy manner.

I'm going to make the same recommendation that Luke did.  Dictionaries
are powerful structures.  I recently wrote a Python script to parse an
English dictionary file and build Markov models out of the words
contained in the file (no, it wasn't for school, or work, or
anything...I was just really, really bored).  Rather than declaring an
int to hold the frequency information for every letter, digram, and
trigram possible (a total of 18,278 declarations), I used the letter,
digram, or trigram as a key into a dictionary.  Now I can do simple
lookups by individual letters, digrams, or trigrams and see the
frequency information without having to reference thousands of
variables, and as an added side effect, only the letters, digrams, and
trigrams that actually occur require storage.

Jonathon


Luke Paireepinart wrote:
> frank h. wrote:
>> hello, i have a string variable that  contains a name that I want to 
>> use as a variablename
>> putting aside questions of why I would like to do that - i this 
>> possible at all?
>>
>> so I want to assign a value to a variable whos name is available only 
>> as a string to me.
>> that variable does not exist yet in the local namespace.
>>
>> from: t = "myvar"
>> to: myvar = 3
>>
>> is this possible? something like setattr?
>> thanks for any insight you might have
> I think the point of dictionaries is to get this same basic 
> functionality without polluting the namespaces.
> You won't let us ask you 'why' you want to do this,
> but I'll ask you: Why don't you want to use a dictionary?
> Do you want to know if it's _Possible_ just so you'll know,
> or do you actually want to use this for something?
> If you just want to know if it's possible, I believe it is.
> But consider:
> if you don't know the variable name until runtime,
> how are you going to refer to the variable later in your code?
> It would be, insofar as I can tell, useless to do this.
>> -frank
> -Luke
> ___
> 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] Book(s) about linux

2006-10-05 Thread Alan Gauld
"Bernard Lebel" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Sorry to use this list for such an OT subject. But I want to get 
> hands
> down with Linux, and am looking for a book or two on the subject.
>
> I'm looking for information about installation, configuration,
> optimisation, and management of the Linux OS.

Personally I think the best general guide is the O'Reilly book
"Running Linux" I've bought the 1st, 2nd and 3rd editions  of
this book although I suspect there may be a 4th by now.

I also bought Red Hat Linux unleashed and thought it a waste
of money, so beware, there is junk out there as well as good stuff.

Although there are differences between distros these are mainly
around package formats and installation tools. I have used 4 distros
over the years (since 1992) and there is little difference once it is
up and running. The main things to learn are the generic Unix
principles and these are common to all distros. So any good
generic Unix book will be helpful.

Personally I've stopped using Linux since I bought an Apple Mac
and installed Cygwin on my XP box. Between them these provide
all the Unix tools I need. I still have Mandrake 9 (now madriva?)
installed on an old box but it hasn't spun its disks for nearly a
year...

HTH,

Alan G. 


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


Re: [Tutor] Luke, thanks a lot , here is the perfect code (as per ur suggestion)

2006-10-05 Thread Kent Johnson
Asrarahmed Kadri wrote:
> What is this??? I cannot understand a single character.. Explain this in 
> length.
> 
> *list1 = [ [ locals()["_[1]"][i-1][j-1]+locals()["_[1]"][i-1][j] if
> (j !=
> 0 and j != i) else 1 for j in range(i+1) ] for i in
> range(num_of_lines) ]

OK, I guess I asked for that. Remember, I did say this was hideous, I 
would never use this for anything other than a newsgroup posting.

Here is your original code, more or less:

list1 = []
for i in range(5):
 flag = 0
 tmp = []
 for j in range(i+1):
 if flag == 0 or j == i:
 tmp.append(1)
 flag = 1
 else:
 tmp.append(list1[i-1][j-1]+list1[i-1][j])
 list1.append(tmp)

First let's get rid of flag, it isn't needed:

list1 = []
for i in range(5):
 tmp = []
 for j in range(i+1):
 if j == 0 or j == i:
 tmp.append(1)
 else:
 tmp.append(list1[i-1][j-1]+list1[i-1][j])
 list1.append(tmp)

Now replace the inner if/else with a conditional expression inside the 
call to append(). A conditional expression has the form (a if b else c) 
where b is the condition being tested and a and c are the two values. 
The if/else becomes this monster:

tmp.append(list1[i-1][j-1]+list1[i-1][j] if (j!=0 and j!=i) else 1)

I inverted the condition so I could put the more common case first. Now 
the whole program looks like this:

list1 = []
for i in range(5):
 tmp = []
 for j in range(i+1):
 tmp.append(list1[i-1][j-1]+list1[i-1][j] if (j!=0 and j!=i) else 1)
 list1.append(tmp)

The inner loop is now ready to be replaced with a list comprehension. In 
general, a loop of the form

tmp = []
for i in x:
   tmp.append(f(i))

can be replaced with the equivalent list comprehension

tmp = [ f(i) for i in x ]

With this change the program is down to this:

list1 = []
for i in range(5):
 tmp = [ list1[i-1][j-1]+list1[i-1][j] if (j!=0 and j!=i) else 1 for 
j in range(i+1) ]
 list1.append(tmp)

This is again in the form of a loop that can be replaced by a list 
comprehension, this time to create list1. The problem is that the 
expression in the list comprehension has to refer to the list being 
built, and this list is not normally available because the name has not 
yet been bound. This is where the cookbook hack comes in play - within a 
list comprehension, the list being built can be referenced as 
locals()["_[1]"]. Refer to the cookbook recipe for details.

So within the list comp,
list1[i-1][j-1] becomes locals()["_[1]"][i-1][j-1] and
list1[i-1][j] becomes locals()["_[1]"][i-1][j].

This brings us to the final form:

list1 = [ [ locals()["_[1]"][i-1][j-1]+locals()["_[1]"][i-1][j] if (j!=0 
and j!=i) else 1 for j in range(i+1) ] for i in range(5) ]

or, with slightly nicer formatting:

list1 = [
 [ locals()["_[1]"][i-1][j-1]+locals()["_[1]"][i-1][j] if (j!=0 and 
j!=i) else 1
 for j in range(i+1)
 ] for i in range(5)
]

Kent

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


Re: [Tutor] from string to variable name

2006-10-05 Thread wesley chun
On 10/5/06, frank h. <[EMAIL PROTECTED]> wrote:
> hello, i have a string variable that  contains a name that I want to use as
> a variablename


setattr() won't work because that's only for objects which have
attributes.  you're talking about creating a (global or local)
variable.  i will also recommend you use a dictionary too... it's
flexible and powerful.

but if you *have* to do it, to paraphrase jonathon's example:

>>> myString = 'rotationalSpeed'
>>> exec '%s = 4500' % myString
>>> print rotationalSpeed
4500

it's ugly but works. this example is more useful if you're planning on
dynamically-generated lots of Python code to execute, not just a
single assignment like this.

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor