Re: simple python question
results.append({'URL': '/feed/%s/show/%s' % (item.rss, item.rssfeed),}) On Thu, Aug 21, 2008 at 23:29, Bobby Roberts <[EMAIL PROTECTED]> wrote: > > hi. > > I'm trying to figure out how to dynamically replace things in a string > as follows: > > >results.append({'URL': '/feed/%s/show/%s' (%item.rss, > %item.rssfeed),}) > > > I think i'm close but it's not the right syntax. can anyone out there > help? > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple python question
> What I'm doing is to load a model from a template. > I have in a template something like this: > {% block menu %} > {% block rss %} > > I want to load "menu" and "rss", but I think that is too much work for > now, I have no idea how to implement it and I probably won't do it. > Thank you very much for your help. Have you looked at template tags? Check this tutorial: http://www.djangoproject.com/documentation/templates_python/ -Sten --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple python question
Thank you! What I'm doing is to load a model from a template. I have in a template something like this: {% block menu %} {% block rss %} I want to load "menu" and "rss", but I think that is too much work for now, I have no idea how to implement it and I probably won't do it. Thank you very much for your help. On 11 abr, 00:07, "Todd O'Bryan" <[EMAIL PROTECTED]> wrote: > On Tue, 2007-04-10 at 18:03 -0500, Jeremy Dunck wrote: > > On 4/10/07, Todd O'Bryan <[EMAIL PROTECTED]> wrote: > > ... > > > The key is that he wanted to use the string name of the class, not the > > > class itself. Assuming that Foo is available (i.e., is local to the code > > > you're running or has been imported), this should work: > > > > o = locals()['Foo']() > > > Oh. In that case, you also want to make sure the string is trust-worthy. > > > I hope you're not creating a class instance from a request parameter. :) > > What he said! (I'm inclined to be so trusting...) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple python question
On Tue, 2007-04-10 at 18:03 -0500, Jeremy Dunck wrote: > On 4/10/07, Todd O'Bryan <[EMAIL PROTECTED]> wrote: > ... > > The key is that he wanted to use the string name of the class, not the > > class itself. Assuming that Foo is available (i.e., is local to the code > > you're running or has been imported), this should work: > > > > o = locals()['Foo']() > > Oh. In that case, you also want to make sure the string is trust-worthy. > > I hope you're not creating a class instance from a request parameter. :) What he said! (I'm inclined to be so trusting...) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple python question
This should work: class Foo: def hello(self): return "Hello World" object = Foo() print object.hello() More information: http://www.diveintopython.org/object_oriented_framework/index.html Ciao, - Matt On Apr 11, 12:22 am, "Grupo Django" <[EMAIL PROTECTED]> wrote: > I know this is not the right place for asking about python, but it's a > simple question. > I need to load an object given in a string. Example: > > #I have a class called foo > class foo: > def Hello(): > return "Hello World" > > object = 'foo' > > print object.Hello() > > Something like this. > Is it possible in python? I think it is but I don't know how to do it. > > Thank you very much. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple python question
On 4/10/07, Todd O'Bryan <[EMAIL PROTECTED]> wrote: ... > The key is that he wanted to use the string name of the class, not the > class itself. Assuming that Foo is available (i.e., is local to the code > you're running or has been imported), this should work: > > o = locals()['Foo']() Oh. In that case, you also want to make sure the string is trust-worthy. I hope you're not creating a class instance from a request parameter. :) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple python question
On Tue, 2007-04-10 at 17:50 -0500, Jeremy Dunck wrote: > On 4/10/07, Grupo Django <[EMAIL PROTECTED]> wrote: > > > > I know this is not the right place for asking about python, but it's a > > simple question. > > I need to load an object given in a string. Example: > > > > #I have a class called foo > > class foo: > > def Hello(): > > return "Hello World" > > > > object = 'foo' > > > > print object.Hello() > > You probably don't really want to use the variable name "object", > since that's also the name of Python's base class. > > But just to follow your example, these lines (almost) do what you want: > > object = foo() > print object.Hello() > > Now I'll rewrite it using better Python conventions and correct a small bug: > > class Foo(object): > def hello(self): > return "Hello World" > > o = Foo() > print o.hello() > The key is that he wanted to use the string name of the class, not the class itself. Assuming that Foo is available (i.e., is local to the code you're running or has been imported), this should work: o = locals()['Foo']() The locals() function returns a dictionary with all currently defined names as keys mapped to their current values. Since 'Foo' is mapped to a class, the code above gets the class and then makes an instance by calling it like a function. Todd --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Simple python question
On 4/10/07, Grupo Django <[EMAIL PROTECTED]> wrote: > > I know this is not the right place for asking about python, but it's a > simple question. > I need to load an object given in a string. Example: > > #I have a class called foo > class foo: > def Hello(): > return "Hello World" > > object = 'foo' > > print object.Hello() You probably don't really want to use the variable name "object", since that's also the name of Python's base class. But just to follow your example, these lines (almost) do what you want: object = foo() print object.Hello() Now I'll rewrite it using better Python conventions and correct a small bug: class Foo(object): def hello(self): return "Hello World" o = Foo() print o.hello() Note: * upper-case class name * consistent indention (4 spaces) * lower-case method name. * method name takes explicit "self" argument, similar to implicit "this" in other languages * avoid using built-in names in variables To see a list of built-ins you should avoid stomping on, try this at the python prompt: dir(__builtins__) To see more about python coding conventions, see PEP 8: http://www.python.org/dev/peps/pep-0008/ To get a nice overview of Python, see this: http://docs.python.org/tut/ If you're new to programming, this may be more helpful: http://www.greenteapress.com/thinkpython/ If you're experienced with programming but new to Python, try this: http://diveintopython.org To get more help with Python (not Django) see python-help: http://mail.python.org/mailman/listinfo/python-help Welcome to Python. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---