简化/自动化此If语句中的分支

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简化/自动化此If语句中的分支相关的知识,希望对你有一定的参考价值。

我之前已经发布了这个,虽然我还没有完全解决这个问题,但最近收到了一些非常好的发型。我现在有一些可行的代码,我虽然这个网站上有人可能能够修复,或告诉我如何修复大声笑。

好吧,基本上是关于我要完成的目标,因为我多次被告知我正在做的事情是无用的,或者我没有提供足够的信息,我会尽力在这里。我有一个程序可以在PDF文件上创建发票并打印出来。 python脚本来自所有必要客户/服务信息等的CSV文件。

现在,到目前为止,我的代码中的问题是我拥有的服务数量,以及在Python Reportlabs中,您必须选择在页面上绘制字符串的位置。一个命令示例是:

cursor.drawstring(xLocation,yLocation,String)

通常的做法是将字符串1到字符串6,在它上面的字符串下面逐个绘制。在接下来的三个示例中,第一个是如果所有服务都被收费,函数将如何输出。如果没有向客户收取某些服务,则第二个(中间)是输出。最后(RightMost)示例是代码我希望它输出。

Service 1         Service 1        Service 1
Service 2                          Service 3
Service 3         Service 3        Service 4
Service 4         Service 4        Service 6
Service 5                          
Service 6         Service 6

不幸的是,基于布尔逻辑,以及有6种可能的服务这一事实,如果您愿意,每种都可以充电或不充电,空或满,那么有64种可能的方法来编写同样的功能以实现完美的输出。

如果我们为每个服务创建一个布尔值,0为空,1为满,那么我们必须测试000000-111111中的每个可能性。这是64.编码是完全合理的。

但是,我需要的服务超过六项,所需的功能数量呈指数级增长。我没有编写每个函数,而是编写了一个程序,它将获取0和1集的文件,即:

0000000000
0000000001
0000000010
etc..

并测试每一行中的每一位。然后,此函数将根据行中的位为文件中的每一行写出一个函数。现在,它产生不正确的输出。我先告诉你我的剧本:

def testFIle():
# This Function will be used to enumerate through the same file you 
used to create the switch dictionary,
# but will define a function for each line.

# filepath = '/home/smiley/Desktop/sampletenbools'
filepath = '/home/smiley/Desktop/10boolsALL'
with open(filepath) as fp:
    num = 1
    for cnt, line in enumerate(fp):
        var = line
        b1 = str(var)[0]
        b2 = str(var)[1]
        b3 = str(var)[2]
        b4 = str(var)[3]
        b5 = str(var)[4]
        b6 = str(var)[5]
        b7 = str(var)[6]
        b8 = str(var)[7]
        b9 = str(var)[8]
        b10 = str(var)[9]

        # YdrawLocationSVC1=490
        # YdrawLocationSVC2=475
        # YdrawLocationSVC3=460
        # YdrawLocationSVC4=445
        # YdrawLocationSVC5=430
        # YdrawLocationSVC6=415
        # YdrawLocationSVC7=400
        # YdrawLocationSVC8=375
        # YdrawLocationSVC9=360
        # YdrawLocationSVC10=345

        if b1 == "0":
            YdrawLocationSVC1=1111
            YdrawLocationSVC2=1
        if b1 == "1":
            YdrawLocationSVC1 = 1

        if b2 == "0":
            YdrawLocationSVC2=1111
            YdrawLocationSVC3=2
        if b2 == "1":
            YdrawLocationSVC2 = 2

        if b3 == "0":
            YdrawLocationSVC3=1111
            YdrawLocationSVC4=3
        if b3 == "1":
            YdrawLocationSVC3 = 3

        if b4 == "0":
            YdrawLocationSVC4=1111
            YdrawLocationSVC5=4
        if b4 == "1":
            YdrawLocationSVC4 = 4

        if b5 == "0":
            YdrawLocationSVC5=1111
            YdrawLocationSVC6=5
        if b5 == "1":
            YdrawLocationSVC5 = 5

        if b6 == "0":
            YdrawLocationSVC6=1111
            YdrawLocationSVC7=6
        if b6 == "1":
            YdrawLocationSVC6 = 6

        if b7 == "0":
            YdrawLocationSVC7=1111
            YdrawLocationSVC8=7
        if b7 == "1":
            YdrawLocationSVC7 = 7

        if b8 == "0":
            YdrawLocationSVC8=1111
            YdrawLocationSVC9=8
        if b8 == "1":
            YdrawLocationSVC8 = 9

        if b9 == "0":
            YdrawLocationSVC9=1111
            YdrawLocationSVC10=9
        if b9 == "1":
            YdrawLocationSVC9 = 9

        if b10 == "0":
            YdrawLocationSVC10=1111
        if b10 == "1":
            YdrawLocationSVC10 = 10


        print "# Bitrep ="+str(var).strip("
")
        print "def Print"+str(num)+"():"
        print"	c.setFont('Deja', 12, leading=None)"
        print "	# SERVICE NAME"+
        "
	c.drawString(100, "+str(YdrawLocationSVC1)+", stringn1)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC2)+", stringn2)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC3)+", stringn3)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC4)+", stringn4)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC5)+", stringn5)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC6)+", stringn6)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC7)+", stringn7)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC8)+", stringn8)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC9)+", stringn9)"+
        "
	c.drawString(100, "+str(YdrawLocationSVC10)+", stringn10)"




        # print("{} : {}".format(str(line).strip("
"), "Print" + str(num)))
        num += 1
        # print "
"


testFIle()

目前,只要只缺少一个服务,此代码就会生成正确的输出:

# Bitrep =1111111101
def Print1022():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 6, stringn6)
c.drawString(100, 7, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 1111, stringn9)
c.drawString(100, 10, stringn10)
# Bitrep =1111111110
def Print1023():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 6, stringn6)
c.drawString(100, 7, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 9, stringn9)
c.drawString(100, 1111, stringn10)
# Bitrep =1111111111
def Print1024():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 6, stringn6)
c.drawString(100, 7, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 9, stringn9)
c.drawString(100, 10, stringn10)

但是,我的代码中的逻辑打破了多个缺少的服务,如下所示:

# Bitrep =1111100101
def Print998():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 1111, stringn6)
c.drawString(100, 1111, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 1111, stringn9)
c.drawString(100, 10, stringn10)
# Bitrep =1111100110
def Print999():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 1111, stringn6)
c.drawString(100, 1111, stringn7)
c.drawString(100, 9, stringn8)
c.drawString(100, 9, stringn9)
c.drawString(100, 1111, stringn10)

如果任何人都可以解决这个问题,以便输出看起来像文件中所有行的下一位,我将upvote你的****大声笑:

def Print999():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 3, stringn3)
c.drawString(100, 4, stringn4)
c.drawString(100, 5, stringn5)
c.drawString(100, 1111, stringn6)
c.drawString(100, 1111, stringn7)
c.drawString(100, 6, stringn8)
c.drawString(100, 7, stringn9)
c.drawString(100, 1111, stringn10)

def Print764():
c.setFont('Deja', 12, leading=None)
# SERVICE NAME
c.drawString(100, 1, stringn1)
c.drawString(100, 2, stringn2)
c.drawString(100, 1111, stringn3)
c.drawString(100, 3, stringn4)
c.drawString(100, 4, stringn5)
c.drawString(100, 1111, stringn6)
c.drawString(100, 1111, stringn7)
c.drawString(100, 5, stringn8)
c.drawString(100, 6, stringn9)
c.drawString(100, 1111, stringn10)

和往常一样,如果你把它发到我的帖子的末尾,我会留下深刻的印象。感谢任何为此付出最轻微努力的人,感谢所有帮助我做到这一点的人。

答案

不想放弃你的努力,但人们一直在告诉你,你做错了是有原因的:)

Reportlab允许您使用编程工具创建动态PDF文档,这意味着在内容更改时布局会发生变化。当您期望各种不同的内容时,尝试对每行的位置进行硬编码将很快变得笨拙/不可能并且很容易破解。

让自己轻松一点,然后使用循环。最简单的例子是,这里有一些基本页面列表的代码......

from reportlab.lib.units import cm 

# get the data selection first
my_services_list = [1,3,6]  # or my_dict, e.g. services {'1': 'message1', etc}

# set the cursor starting position
pos_X, pos_Y = 5, 15

for service in my_services_list:
    c.drawString(pos_X*cm, pos_Y*cm, service)
    # move the cursor down a line - remember that zero is the page bottom
    pos_Y -= 1

就是这样,没有空行。 .drawString适用于更简单的页面。如果您需要更全面的方法,请阅读有关框架和Flowables的文档。如果需要,您可以将文本流入文本框,就像使用桌面出版软件一样。

以上是关于简化/自动化此If语句中的分支的主要内容,如果未能解决你的问题,请参考以下文章

Go语言系列-03-流程控制语句

如何简化此 if 语句?

布尔逻辑:如何简化此 IF 语句?

IT兄弟连 Java语法教程 流程控制语句 分支结构语句2

简化条件表达式

python之switch语句,优化多个if语句