如何在不破坏代码的情况下添加 if 语句?

发布于 2025-01-11 00:07:03 字数 3569 浏览 7 评论 0原文

我正在使用 python zope 打印 html 代码,并且回溯并不是很有帮助。我想添加一个 if 语句来检查是否 "test(highlight_today == 'yes' and DateTime(container.util.getToday(format="%m-%d-%Y")) == DateTime(dmy), '颜色:黑色;;字体粗细:900;;','颜色:灰色;;')" 存在。如果确实添加“tabindex=0”。然而我遇到了几个问题。

第一次尝试是将其变成一个变量。

try1 = test(highlight_today == 'yes' and DateTime(container.util.getToday(format="%m-%d-%Y")) == DateTime(dmy), 'color:black;; font-weight:900;;', 'color:grey;;')

但即使在尝试执行 if 语句之前,我也会遇到回溯错误。我也尝试将其串起来,但有类似的结果。

 __traceback_info__: container.Admin.Holiday.printCalendar(current_mo=container.securityUtil.sanitize(path('request/month | nothing')) or container.util.getToday(format='%m'), current_yr=container.securityUtil.sanitize(path('request/year | nothing')) or container.util.getToday(format='%Y'), past_date='no', highlight_today='yes', prev_next='yes', min_height='3em')
  Module PythonExpr, line 1, in <expression>

如果我尝试在文档字符串后进行识别以提高可读性,我也会得到此回溯。我一直在尝试在线查找有关 zope 字符串与 html 连接的资源,但没有成功。任何意见或建议都有帮助。该表有两个部分。据我所知,第一部分没有问题。

print '''
    <table class="scroll_data" style="background-color:white">
        <tr><td class="bold" colspan="7" style="text-align:center">%s<span class="px-2" tabindex="0" aria-label="%s %s schedule">%s %s</span>%s</td></tr>
        <tr>
            <td width="14%%">Sun</td>
            <td width="14%%">Mon</td>
            <td width="14%%">Tue</td>
            <td width="14%%">Wed</td>
            <td width="14%%">Thu</td>
            <td width="14%%">Fri</td>
            <td width="14%%">Sat</td>
        </tr>
    ''' %(test(prev_next=='yes','<input type="button" title="View previous month calendar" id="cal_prev" value="Prev" data-role="none" data-inline="true"/>',''),
    month, current_yr, month, current_yr, test(prev_next=='yes',
    '<input type="button" title="View next month calendar" id="cal_next" value="Next" data-role="none" data-inline="true"/>',''))

第二部分

dow = 0
dow_continue = 'false'
current_date = 1
while current_date <= int(date_range):

    current_dt=''
    if int(current_date) < 10:
        current_dt = '0' + str(int(current_date))
        # if current date 1 < 10 dt = 0+1
        # dt = 01
    else:
        current_dt = str(current_date)
        # else dt = 10 or 11 etc.


    if dow == 0:
        print '''   <tr>'''

    if dow_start == dow or dow_continue == 'true':
        dow_continue = 'true'

        dmy = '%s-%s-%s' %(current_month, current_dt, current_yr)

        # This print statement was a one liner, it breaks when I indent it, either I am indenting it wrong or zope doesn't allow me?
        print '''       <td class="%s" id="%s" style="text-align: right; vertical-align: top; %s height:%s;">
        <span style="color:black">%d</span></td>''' 
        %(test(past_date == 'yes', test(DateTime(container.util.getToday(format="%m-%d-%Y")) > DateTime(dmy), 'even', 'odd'), 'odd'), 
        dmy, 
        test(highlight_today == 'yes' and DateTime(container.util.getToday(format="%m-%d-%Y")) == DateTime(dmy), 'color:black;; font-weight:900;;', 'color:grey;;'),
        min_height, 
        current_date)

        current_date = current_date + 1

    else:

        print '''       <td>&nbsp;</td>'''

    if dow == 6:
        print '''   </tr>'''
        dow = -1

    dow = dow + 1

I am working with python zope printing html code and the traceback isn't very helpful. I want to add an if statement to check if "test(highlight_today == 'yes' and DateTime(container.util.getToday(format="%m-%d-%Y")) == DateTime(dmy), 'color:black;; font-weight:900;;', 'color:grey;;')" exists. If it does add "tabindex=0". However I ran into a couple of issues.

First try was making it into a variable.

try1 = test(highlight_today == 'yes' and DateTime(container.util.getToday(format="%m-%d-%Y")) == DateTime(dmy), 'color:black;; font-weight:900;;', 'color:grey;;')

but I get traceback error even before I try to do an if statement. I also try to String it but have similar outcome.

 __traceback_info__: container.Admin.Holiday.printCalendar(current_mo=container.securityUtil.sanitize(path('request/month | nothing')) or container.util.getToday(format='%m'), current_yr=container.securityUtil.sanitize(path('request/year | nothing')) or container.util.getToday(format='%Y'), past_date='no', highlight_today='yes', prev_next='yes', min_height='3em')
  Module PythonExpr, line 1, in <expression>

I also get this traceback if I try to ident after the docstring for readability. I been trying to find resources online about zope string concatenation with html but haven't been successful. Any advice or suggestions helps. There are two parts to the table. From what I can tell this first part had no issues.

print '''
    <table class="scroll_data" style="background-color:white">
        <tr><td class="bold" colspan="7" style="text-align:center">%s<span class="px-2" tabindex="0" aria-label="%s %s schedule">%s %s</span>%s</td></tr>
        <tr>
            <td width="14%%">Sun</td>
            <td width="14%%">Mon</td>
            <td width="14%%">Tue</td>
            <td width="14%%">Wed</td>
            <td width="14%%">Thu</td>
            <td width="14%%">Fri</td>
            <td width="14%%">Sat</td>
        </tr>
    ''' %(test(prev_next=='yes','<input type="button" title="View previous month calendar" id="cal_prev" value="Prev" data-role="none" data-inline="true"/>',''),
    month, current_yr, month, current_yr, test(prev_next=='yes',
    '<input type="button" title="View next month calendar" id="cal_next" value="Next" data-role="none" data-inline="true"/>',''))

Second part

dow = 0
dow_continue = 'false'
current_date = 1
while current_date <= int(date_range):

    current_dt=''
    if int(current_date) < 10:
        current_dt = '0' + str(int(current_date))
        # if current date 1 < 10 dt = 0+1
        # dt = 01
    else:
        current_dt = str(current_date)
        # else dt = 10 or 11 etc.


    if dow == 0:
        print '''   <tr>'''

    if dow_start == dow or dow_continue == 'true':
        dow_continue = 'true'

        dmy = '%s-%s-%s' %(current_month, current_dt, current_yr)

        # This print statement was a one liner, it breaks when I indent it, either I am indenting it wrong or zope doesn't allow me?
        print '''       <td class="%s" id="%s" style="text-align: right; vertical-align: top; %s height:%s;">
        <span style="color:black">%d</span></td>''' 
        %(test(past_date == 'yes', test(DateTime(container.util.getToday(format="%m-%d-%Y")) > DateTime(dmy), 'even', 'odd'), 'odd'), 
        dmy, 
        test(highlight_today == 'yes' and DateTime(container.util.getToday(format="%m-%d-%Y")) == DateTime(dmy), 'color:black;; font-weight:900;;', 'color:grey;;'),
        min_height, 
        current_date)

        current_date = current_date + 1

    else:

        print '''       <td> </td>'''

    if dow == 6:
        print '''   </tr>'''
        dow = -1

    dow = dow + 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文