Re: [Tutor] passing form data into a class

2007-07-31 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
> dear fellow Python enthusiasts, let's say I have a dictionary of keys 
> and values obtained from a form submitted by a user.  For each submitted 
> form I will create the Application and Candidate classes, and I want to 
> be able to call Application(Candidate(**submitted_data)).display() to 
> create an html representation of the user submitted data.  Depending on 
> the applicant's preferences and location, the number of fields on the 
> form, and thus the size of this dictionary, is variable, and not all 
> fields are required.  I seem to recall a setargs function in Python that 
> would take any argument passed into a class and set that argument as a 
> member variable in a class.  A Google search turned up no such 
> construct, so here I am asking you, is there a way for me to create a 
> class that accepts a dictionary of submitted data and uses each key ad 
> value to create a corresponding member variable ?

I occasionally find this handy:

class Bunch(object):
 ''' A container for data that supports attribute access.
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308
 '''
 def __init__(self, **kwds):
 self.__dict__.update(kwds)

 def __repr__(self):
 state = ["\n  %s=%r" % (attribute, value)
  for (attribute, value)
  in sorted(self.__dict__.items())]
 return self.__class__.__name__ + ':' + ''.join(state)


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


Re: [Tutor] passing form data into a class

2007-07-31 Thread Dave Kuhlman
On Mon, Jul 30, 2007 at 08:32:55PM -0700, [EMAIL PROTECTED] wrote:
> dear fellow Python enthusiasts, let's say I have a dictionary of keys and
> values obtained from a form submitted by a user.  For each submitted form I
> will create the Application and Candidate classes, and I want to be able to
> call Application(Candidate(**submitted_data)).display() to create an html
> representation of the user submitted data.  Depending on the applicant's
> preferences and location, the number of fields on the form, and thus the
> size of this dictionary, is variable, and not all fields are required.  I
> seem to recall a setargs function in Python that would take any argument
> passed into a class and set that argument as a member variable in a class.
> A Google search turned up no such construct, so here I am asking you, is
> there a way for me to create a class that accepts a dictionary of submitted
> data and uses each key ad value to create a corresponding member variable ?
> The current way I do this is with a very long argument list, and line by
> line:

You have already gotten one good solution from Kent.  But,
depending on your needs, you might also focus on the request side
rather than on when the class or instance is created.  That might
enable you to add a bit of the security checking that Ken Fouey was
concerned about.

Here is an example of a class which looks a name up in a dictionary
when the attribute is requested.  It does not create attributes for
the keys in the dictionary:

class Bunch(object):
def __init__(self, vardict=None):
if vardict is None:
self.vardict = vardict
else:
self.vardict = vardict
def __getattr__(self, name):
if name in self.vardict:
return self.vardict[name]
else:
raise AttributeError, 'Bunch has no attribute: %s' % name

def test():
d = {'aaa': 111, 'bbb': 222, }
b = Bunch(d)
print b.aaa
print b.ccc

test()

Dave


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


Re: [Tutor] passing form data into a class

2007-07-31 Thread Eric Brunson
Dave Kuhlman wrote:
> On Mon, Jul 30, 2007 at 08:32:55PM -0700, [EMAIL PROTECTED] wrote:
>   
>> dear fellow Python enthusiasts, let's say I have a dictionary of keys and
>> values obtained from a form submitted by a user.  For each submitted form I
>> will create the Application and Candidate classes, and I want to be able to
>> call Application(Candidate(**submitted_data)).display() to create an html
>> representation of the user submitted data.  Depending on the applicant's
>> preferences and location, the number of fields on the form, and thus the
>> size of this dictionary, is variable, and not all fields are required.  I
>> seem to recall a setargs function in Python that would take any argument
>> passed into a class and set that argument as a member variable in a class.
>> A Google search turned up no such construct, so here I am asking you, is
>> there a way for me to create a class that accepts a dictionary of submitted
>> data and uses each key ad value to create a corresponding member variable ?
>> The current way I do this is with a very long argument list, and line by
>> line:
>> 
>
> You have already gotten one good solution from Kent.  But,
> depending on your needs, you might also focus on the request side
> rather than on when the class or instance is created.  That might
> enable you to add a bit of the security checking that Ken Fouey was
> concerned about.
>
> Here is an example of a class which looks a name up in a dictionary
> when the attribute is requested.  It does not create attributes for
> the keys in the dictionary:
>
> class Bunch(object):
> def __init__(self, vardict=None):
> if vardict is None:
> self.vardict = vardict
> else:
> self.vardict = vardict
> def __getattr__(self, name):
> if name in self.vardict:
> return self.vardict[name]
> else:
> raise AttributeError, 'Bunch has no attribute: %s' % name
>   

Since this is tutor, I'll add my $.02...

Kent's version of Bunch is great, I use that sort of definition all the 
time in my code when I want to use "instance.thing = blah" to save 
typing and make my code more readable, but also want to use dict 
nomenclature for iteration over keys, etc, rather than "getattr( 
instance, key )," again, primarily for neatness of code.

However, this version from Dave has the advantage of segregating your 
"special data" from the regular class attributes.  If you wanted to, for 
example, keep a count of the number of form keys that were passed into 
the form, you couldn't store that in an instance attribute without it 
getting mixed up with all your form variables.

I'm not saying either is better than the other overall, they each have 
their place and I use both, but in this *specific* case, I think you'd 
want to use Dave's version.

Just my opinion, I could be wrong.  :-)

e.

> def test():
> d = {'aaa': 111, 'bbb': 222, }
> b = Bunch(d)
> print b.aaa
> print b.ccc
>
> test()
>
> Dave
>
>
>   

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


Re: [Tutor] Which GUI?

2007-07-31 Thread scott
Hi,

I have been doing some research on the GUI's and noticed it is a very 
difficult choice to make which one to write with.  It seems to be even 
more difficult than choosing which programming language to use.

If you don't mind I have a few questions to ask about selecting the 
right GUI to program with.

1: I know many people suggest learning more than one programming 
language when writing programs and use each programming language like a 
tool on a toolbox.  Should one also learn more than one GUI and select 
the correct one for each individual program?

2: How have you come to select your favourite GUI(s)?

3: Is there a GUI that is better for developing for Linux?

4: I have read that WxPython is difficult to install on Linux from a 
couple different sources.  I have personally never found that a problem. 
  Is this true?  Is WxPython a poor choice for a Linux developer?

Thank you for any help you can give me :)

-- 
Your friend,
Scott

Sent to you from a Linux computer using Ubuntu Version 7.04 (Feisty Fawn)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which GUI?

2007-07-31 Thread Ian Witham
Hi Scott,

I'm no expert (yet) but I have come across this fun online tool which helps
you choose a GUI toolkit for Python:

Choose Your GUI Toolkit 

I had no problem installing WxPython from the Ubuntu repos, and I have heard
that is is one of the top choices for cross-platform GUI development.
If you are only interested in developing for Linux then there is PyGTK. I
haven't used this though so I couldn't comment on its ease of use.

Ian

On 8/1/07, scott <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I have been doing some research on the GUI's and noticed it is a
> very
> difficult choice to make which one to write with.  It seems to be even
> more difficult than choosing which programming language to use.
>
> If you don't mind I have a few questions to ask about selecting
> the
> right GUI to program with.
>
> 1: I know many people suggest learning more than one programming
> language when writing programs and use each programming language like a
> tool on a toolbox.  Should one also learn more than one GUI and select
> the correct one for each individual program?
>
> 2: How have you come to select your favourite GUI(s)?
>
> 3: Is there a GUI that is better for developing for Linux?
>
> 4: I have read that WxPython is difficult to install on Linux from a
> couple different sources.  I have personally never found that a problem.
>   Is this true?  Is WxPython a poor choice for a Linux developer?
>
> Thank you for any help you can give me :)
>
> --
> Your friend,
> Scott
>
> Sent to you from a Linux computer using Ubuntu Version 7.04 (Feisty Fawn)
> ___
> 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] Which GUI?

2007-07-31 Thread Terry Carroll
On Wed, 1 Aug 2007, Ian Witham wrote:

> I'm no expert (yet) but I have come across this fun online tool which helps
> you choose a GUI toolkit for Python:
> 
> Choose Your GUI Toolkit 

I think it's rigged.  I answered it honestly, and it suggested wxPython, 
which is no surprise; I came to the same conclusion myself based on my 
needs and preferences.  

But no matter how I adjusted the parameters, it always suggested wxPython.

I just input a 1 on every factor, except "Popularity, installed base", 
where I put in 100.  Surely that should suggest Tkinter, which comes 
pre-installed with Python,right?

Nope.  "wxPython is the GUI for you!"

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


[Tutor] C program

2007-07-31 Thread Alondt Yauk
Hello people of the world.

This question is not a Python related question, but I thought I might just 
throw it in here and see if there are any C old-timers who could help out.

I have a string with space and I would like to remove the space before 
comparing but I don't know how to go about doing it. Here is the code

int result = strcmp("yes","yes ");//trailing space on the second yes
this should give me '0' meaning they are both the same, but with the space, it 
gives me '-1'

Please note: those arguments can be supplied by the user, and when they do, I 
want to strip off the trailing space.

Any helps is highly appreciated.

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


Re: [Tutor] C program

2007-07-31 Thread Tiger12506
Hello people of the world.

This question is not a Python related question, but I thought I might just 
throw it in here and see if there are any C old-timers who could help out.

I have a string with space and I would like to remove the space before 
comparing but I don't know how to go about doing it. Here is the code

int result = strcmp("yes","yes ");//trailing space on the second yes
this should give me '0' meaning they are both the same, but with the space, 
it gives me '-1'

Please note: those arguments can be supplied by the user, and when they do, 
I want to strip off the trailing space.

Any helps is highly appreciated.
-

I doubt if this is the best way, but you can do this.


/*--*/
#include 
#include 

char s[20] = "yes";
char s2[20] = "yes  ";

void rstrip(char *c) {
  while (*c++!=0);
  c--;
  while (*--c==' ') *c = 0;
}

int main() {
  rstrip(s2);
  int result = strcmp(s, s2);
  printf("%d\n",result);
  system("pause");
}
/*--*/


JS

PS - This *is* a python list. Usually the tutors don't like people who post 
off-topic stuff. (And C is pretty off-topic) 

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


Re: [Tutor] passing form data into a class

2007-07-31 Thread John Fouhy
On 01/08/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> John, I spent the last two hours trying to understand what you wrote, and I
> feel I'm missing something:
>
>  >>> a_dict = {'one':1, 'two':2, 'three':3}
>  >>> class A(object):
>  def __init__(self, **kwargs):
>  for var in kwargs:
>  setattr(self, var, locals()[var])

Well, that's one risk of using something like locals() -- it can be
fragile with respect to transformations that wouldn't otherwise make
much difference.

Try this:

 a_dict = {'one':11, 'two':22}
class A(object):
  def __init__(self, one=1, two=2):
print locals()
A(**a_dict)

And this:

class B(object):
  def __init__(self, **kwargs):
print locals()
B(**a_dict)

Do you see the difference?  With B, you could write:

class B(object):
  def __init__(self, **kwargs):
for var in kwargs:
  setattr(self, var, kwargs[var])

Although, as Kent points out, you can more simply write
'self.__dict__.update(kwargs)'.

> >>> class A(object):
>  def __init__(self, **kwargs):
>  for var in locals():
>  setattr(self, var, locals()[var])
>  >>> a_inst = A(**a_dict)
>  RuntimeError: dictionary changed size during iteration

And that illustrates another risk of using locals() (and a risk of not
testing my code before posting it).  What's happening is that python
is inserting 'var' into locals() immediately after starting the loop,
which is breaking iteration.  You could fix this by saying:

  for var in locals().keys():
# etc

But a few other people have weighed in with suggestions that should be
less fragile, so you may want to go with their ideas instead.

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


Re: [Tutor] Which GUI?

2007-07-31 Thread Luke Paireepinart
Terry Carroll wrote:
> On Wed, 1 Aug 2007, Ian Witham wrote:
>
>   
>> I'm no expert (yet) but I have come across this fun online tool which helps
>> you choose a GUI toolkit for Python:
>>
>> Choose Your GUI Toolkit 
>> 
>
> I think it's rigged.  I answered it honestly, and it suggested wxPython, 
> which is no surprise; I came to the same conclusion myself based on my 
> needs and preferences.  
>
> But no matter how I adjusted the parameters, it always suggested wxPython.
>
> I just input a 1 on every factor, except "Popularity, installed base", 
> where I put in 100.  Surely that should suggest Tkinter, which comes 
> pre-installed with Python,right?
>
> Nope.  "wxPython is the GUI for you!"
>   
A quick view of the source code of the page finds the relative weights 
in the javascript:

var scorePyGUI = (easelearning * 50) + (maturity * 10) + (pop * 30) + (paint * 
10) + (windows * 10) + (linux * 100) + (mac * 100) + (pda * 10);

var scoreTkinter = (easelearning * 30) + (maturity * 100) + (pop * 100) + 
(paint * 50) + (windows * 50) + (linux * 80) + (mac * 70) + (pda * 0);

var scoreEasygui = (easelearning * 100) + (maturity * 10) + (pop * 10) + (paint 
* 10) + (windows * 50) + (linux * 70) + (mac * 70) + (pda * 0);

var scorewxPython = (easelearning * 33) + (maturity * 100) + (pop * 120) + 
(paint * 100) + (windows * 100) + (linux * 100) + (mac * 90) + (pda * 10);

var scorePythonCard = (easelearning * 90) + (maturity * 30) + (pop * 40) + 
(paint * 10) + (windows * 100) + (linux * 50) + (mac * 50) + (pda * 0);

var scorepyQt = (easelearning * 20) + (maturity * 100) + (pop * 60) + (paint * 
50) + (windows * 70) + (linux * 100) + (mac * 20) + (pda * 70);

var scorepyGtk = (easelearning * 20) + (maturity * 100) + (pop * 60) + (paint * 
90) + (windows * 40) + (linux * 80) + (mac * 25) + (pda * 0);

var scoreJython = (easelearning * 25) + (maturity * 75) + (pop * 50) + (paint * 
10) + (windows * 80) + (linux * 80) + (mac * 80) + (pda * 100);

var scoreAnygui = (easelearning * 40) + (maturity * 20) + (pop * 10) + (paint * 
10) + (windows * 90) + (linux * 90) + (mac * 50) + (pda * 10);

var scorefxPy = (easelearning * 20) + (maturity * 50 ) + (pop * 20) + (paint * 
0) + (windows * 75) + (linux * 90) + (mac * 20) + (pda * 0);

var scorepyFLTK = (easelearning * 50) + (maturity * 35) + (pop * 10) + (paint * 
0) + (windows * 75) + (linux * 90) + (mac * 20) + (pda * 0);


Seems like wxPython's scores are a little biased, maybe.
Especially that 120 for popularity, when no other GUI toolkit got more 
than 100 for any other category.
Interpret this as you will :)
> ___
> 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] Which GUI?

2007-07-31 Thread Alan Gauld
"scott" <[EMAIL PROTECTED]> wrote

> 1: I know many people suggest learning more than one programming
> language when writing programs and use each programming language 
> like a
> tool on a toolbox.  Should one also learn more than one GUI and 
> select
> the correct one for each individual program?

I would say not. There is little advantage of one over the other.
Unfortunately you will often be required to learn new GUIs because the
choice for any given project may already have been made, either
because the project is already underway, or its the 'house style'
or because some key component only works with a given GUI.

> 2: How have you come to select your favourite GUI(s)?

History and experience. The ones that are most 'intuitive' to use
and use least lines of code or have good GUI builders - something
lacking in most open-source GUIs at present...

> 3: Is there a GUI that is better for developing for Linux?
>
> 4: I have read that WxPython is difficult to install on Linux from a
> couple different sources.

I've never found any problem, but I've only done it twice...

>  Is this true?  Is WxPython a poor choice for a Linux developer?

I don't think so, Linux seems to be the main development OS
from what little I've seen.

Alan G. 


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


Re: [Tutor] C program

2007-07-31 Thread Alan Gauld

"Tiger12506" <[EMAIL PROTECTED]> wrote

> I have a string with space and I would like to remove the space 
> before
> comparing but I don't know how to go about doing it. Here is the 
> code

Look at strrchr()...

> void rstrip(char *c) {
>  while (*c++!=0);
>  c--;
>  while (*--c==' ') *c = 0;
> }

Or roll your own... :-)
This is probably easiest if you only want to catch spaces...

Alan G. 


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