2013-03-25 5 views
0

Flask 내에서 Jinja2 템플릿을 사용하는 경우에만이 작업을 수행 할 수 있습니다 (단, 특정 작업과 관련된 작업은 아니지만 더 일반적인 작업을 수행하기위한 작업 일뿐입니다).) :Python이 동적 속성으로 사용할 매개 변수 목록을 전달합니다.

class MultipleMacroFor(object): 
    def __init__(self, macro): 
     self.macro = macro 

    def renderable(self, macro_var): 
     return get_template_attribute(self.macro, macro_var) 

class ResponseItemMacros(MultipleMacroFor): 
    def __init__(self): 
     super(ResponseItemMacros, self).__init__(macro="macros/response_items.html") 

    @property 
    def general_macro(self): 
     return self.renderable('general_macro') 

RI = ResponseItemMacros() 

사용 사례의 예 :

class MultipleMacroFor(object): 
    def __init__(self, macro, which_macros): 
      self.macro = macro 
      self.which_macros = which_macros 

    # take which_macros and have them each be returnable as in the function above but 
    # respond to the more general case, not specifically requiring construction 
    # @property 
    # def (passed in var which_macro)(self): 
    # return self.renderable(passed in var which_macro) 

RI = MultipleMacroFor(macro="macros/response_items.html", macros=['general', 'etc', 'etal']) 
,536 :이로 전환 할

RI.general_macro # returns the renderable 'general_macro' from 'macros/response_items.html' that can be used with in a Jinja2 template 

는 경우에 사용합니다

RI.general_macro #as above but without having to construct the intermediate ResponseItemsMacros class 

및 전달리스트가 동적으로 목록에 통과하고 수동으로 첫 번째 예제로 구성하지 기반으로 구축되어, 속성으로 호출 할 수 있습니다. 첫 번째 예제에서는 두 클래스의 인스턴스를 수동으로 생성해야합니다. 두 번째는 원하는 클래스를 사용하여 호출 될 속성으로 인스턴스화 할 수있는 클래스를 하나만 사용하는 것으로 렌더링 가능한 함수를 통해 관련 명명 된 매크로를 생성합니다.

정확하게이 방법을 모르겠습니다. 모든 의견을 감사드립니다.

+1

본인의 질문이나 예제와 질문의 관계를 이해할 수 없습니다. 당신은 무엇을 원하는지 좀 더 명확하게 expllain 할 수 있습니까, 아마도'MultipleMacroFor'를 사용하는 코드를 제공할까요? – Bakuriu

+0

공정함 충분 함, 설명 추가 – blueblank

답변

0

인스턴스 당 속성을 가질 수 없으며 최소한 적어도 get_attribute을 사용하면 안됩니다. 가장 간단한 해결책은 getattr을 사용하는 것입니다.

class MultipleMacroFor(object): 
    def __init__(self, macro, names): 
     self._macro = macro 
     self._names = names 

    def get_renderable(self, macro_var): 
     return get_template_attribute(self.macro, macro_var) 

    def __getattr__(self, name): 
     if name in self._names: 
      return self.get_renderable(name) 
     raise AttributeError(
      "object %s has no attribute '%s'" % (
       type(self).__name__, name) 
      ) 

좀 더 복잡한 방법은 동적으로 하위 클래스를 작성하여 인스턴스화하는 것입니다.

class MultipleMacroFor(object): 
    def __init__(self, macro, names): 
     self._macro = macro 
     self._names = names 


    def get_renderable(self, macro_var): 
     return get_template_attribute(self.macro, macro_var) 

def MultipleMacroForFactory(macro, names): 
    attrs = dict() 
    for name in names: 
     attrs[name] = property(
      lambda self, name=name: self.get_renderable(name) 
      ) 

    clsname = "MultipleMacroFor%s" % "".join(
     name.capitalize() for name in names 
     ) 

    return type(clsname, (MultipleMacroFor,), attrs)(macro, names) 
+0

두 번째는 atm을 찾는 것보다 더 복잡해 보이지만 첫 번째 것은 내가 찾고 있던 것이지만 처음에 물었을 때 어떻게 움직이는 지 이해하지 못했기 때문에 시도해 보겠습니다. – blueblank

+0

나는 왜 당신이'__getattr__'을 언급하고'__getitem__'을 가진 클래스를 작성하는지 이해하지 못합니다. 그 상황에서 – Bakuriu

+0

__getattr__이 (가) 효과가있었습니다. – blueblank

관련 문제