Filters And Argument Unpacking
"Williams, James P2" <[email protected]> Sat, 12 Dec 2015 23:24:17 +0000
| Newsgroups | gmane.comp.python.cheetah |
|---|---|
| Message-ID | <[email protected]> |
--===============4537476871874774763==
Content-Language: en-US
Content-Type: multipart/alternative;
boundary="_000_B309D25CD7636A4F86413231DA18C3C720882FBFHDXDSP35uslmcoc_"
--_000_B309D25CD7636A4F86413231DA18C3C720882FBFHDXDSP35uslmcoc_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
I wrote a custom filter that accepts some additional keyword arguments.
class MyFilter(Cheetah.Filters.Filter):
def filter(self,val,goo=3DFalse,moo=3D42,**kw):
if goo or moo:
[...snip...]
else:
return super(MyFilter,self).filter(val,**kw)
...
template=3DCheetah.Template.Template(file=3Df,searchList=3D[...],filter=
=3DMyFilter)
...
In my template, I'm trying to call the filter, but unpacking a dictionary t=
o provide the new keyword arguments. This example assumes $koo is in the s=
earch list.
#set $args=3D{
"goo": True,
"moo": 52
}
This is ${koo,**$args}-koo.
I get a SyntaxError when the template is filled, apparently complaining abo=
ut the filter call. The template compiles to the following, more or less, =
the caret matching the one in the traceback.
_v =3D VFFSL(SL,"koo",True)
if _v is not None: write(_filter(_v,**VFFSL(SL,"args",True),rawExpr=3D..=
.))
^
I think the problem is that dict unpacking has to be the last argument, but=
the rawExpr follows it. I can't find this syntax rule in the docs, but it=
seems to hold in an interactive session.
Is there a way to do this? I know I can add an optional dictionary to the =
filter, and not use unpacking.
Thanks.
Jim
--_000_B309D25CD7636A4F86413231DA18C3C720882FBFHDXDSP35uslmcoc_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Courier New";
color:#002060;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"EN-US" link=3D"#0563C1" vlink=3D"#954F72">
<div class=3D"WordSection1">
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060">I wrote a custom filter that accepts some additional keyword =
arguments.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> class MyFilter(Cheetah.Filters.Filter):<o:p></o:=
p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> def filter(self,val,goo=
=3DFalse,moo=3D42,**kw):<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> =
if goo or moo:<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> =
[...snip...]<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> =
else:<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> =
return super(MyFilter,self).filter(val,**kw)<o:p></=
o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> ...<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> template=3DCheetah.Template.Template(file=3Df,se=
archList=3D[...],filter=3DMyFilter)<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> ...<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060">In my template, I’m trying to call the filter, but unpa=
cking a dictionary to provide the new keyword arguments. This example=
assumes $koo is in the search list.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> #set $args=3D{<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> “goo”: True,=
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> “moo”: 52<o:=
p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> }<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> This is ${koo,**$args}-koo.<o:p></o:p></span></p=
>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060">I get a SyntaxError when the template is filled, apparently c=
omplaining about the filter call. The template compiles to the follow=
ing, more or less, the caret matching the one in the
traceback.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> _v =3D VFFSL(SL,”koo”,True)<o:p></o:=
p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> if _v is not None: write(_filter(_v,**VFFSL(SL,&=
#8221;args”,True),rawExpr=3D...))<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"> &=
nbsp; &nbs=
p; &=
nbsp; &nbs=
p; &=
nbsp; ^<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060">I think the problem is that dict unpacking has to be the last=
argument, but the rawExpr follows it. I can’t find this syntax=
rule in the docs, but it seems to hold in an interactive
session.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060">Is there a way to do this? I know I can add an optional=
dictionary to the filter, and not use unpacking.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060">Thanks.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:"Courier New";c=
olor:#002060">Jim<o:p></o:p></span></p>
</div>
</body>
</html>
--_000_B309D25CD7636A4F86413231DA18C3C720882FBFHDXDSP35uslmcoc_--
--===============4537476871874774763==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
--===============4537476871874774763==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Cheetahtemplate-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
--===============4537476871874774763==--