简体   繁体   English

Genshi和Python生成器(产量)

[英]Genshi and Python Generators (yield)

How do I create/call a python generator in Genshi? 如何在Genshi中创建/调用python生成器? Is that even possible? 这甚至可能吗?

For example, (and no i'm not looking for an alternate solution to this problem, of which there are many, including enumerate on the for each, etc): 例如,(并且我没有寻找这个问题的替代解决方案,其中有很多,包括枚举的每个,等等):

 <?python
  """ a bunch of other code ... """
  def bg_color_gen():
    """ Alternate background color every call """
    while 1:
      yield "#FFFFFF"
      yield "#EBEBEB"
  ?>

And then calling this function: 然后调用此函数:

  <fo:block background-color="${bg_color_gen()}">First entry</fo:block>
  <fo:block background-color="${bg_color_gen()}">Second entry</fo:block>
  <fo:block background-color="${bg_color_gen()}">Third entry</fo:block>

This has nothing to do with my < fo:block >, which you could replace with < div >. 这与我的<fo:block>无关,你可以用<div>替换它。 It is not an FO question but a Genshi question. 这不是一个FO问题,而是一个Genshi问题。 I'm guessing Genshi doesn't recognize the 'yield' and runs 'while 1' ad-infinitum? 我猜Genshi不承认'收益'并且在'无限1'的情况下运行?

Also, I do realize I could use a global to keep track of a counter, and then call 此外,我确实意识到我可以使用全局来跟踪计数器,然后调用

 counter++
 if counter%yieldCount==0: return "#FFFFFFF"
 elif counter%yieldCount==1: return "#EBEBEB"

But this is not a generator and gets ugly very quickly! 但这不是一个发电机而且很快变丑!

Clarification: Another way to ask this question: how would you code 澄清:提出这个问题的另一种方式:你将如何编码

def fib():
    a,b = 0,1
    while True:
        yield a
        b = a+b
        yield b
        a = a+b

Which would then be called in the sentence "The first number is $fib(), the second is $fib(), the third is $fib(), and so on." 然后在句子中调用“第一个数字是$ fib(),第二个是$ fib(),第三个是$ fib(),依此类推。”

================================================ ================================================

Updated full solution based on accepted answer: 根据已接受的答案更新完整解决方案

<?python
def fib_generator():
    a,b = 0,1
    while True:
        yield a
        b = a+b
        yield b
        a = a+b
fib = fib_generator()
?>


The first number is ${next(fib)}, 
the second is ${next(fib)},
the third is ${next(fib)}, and so on.

Without knowing the structure of your content, I would suggest the following: 在不知道您的内容结构的情况下,我建议如下:

<fo:block py:for="i, entry in entries"
          background-color="${'#FFFFFF' if i % 2 else '#EBEBEB'}">
  ${entry}
</fo:block>

However if you truly want to use a generator then you could just evaluate using Python's native next() 但是,如果你真的想使用一个生成器,那么你可以只使用Python的本机next()进行评估

<py:with vars="color=bg_color_gen();">
  <fo:block background-color="${next(color)}">
</py:with>

You would want to declare the generator first and then call next on it to get a yield ed color. 你会想首先声明发电机,然后调用next就可以得到一个yield版颜色。

In this case you are passing three different instances of the generator created by calling bg_color_gen() ie) 在这种情况下,您传递通过调用bg_color_gen()创建的生成器的三个不同实例ie)

# this creates a generator
>>> bg_color_gen()
<generator object bg_color_gen at 0x02B21A30>
>>> bgcg = bg_color_gen()
# this gets values
>>> next(bgcg)
'#FFFFFF'
>>> next(bgcg)
'#EBEBEB'
>>> next(bgcg)
'#FFFFFF'
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM