【Salesforce】資料庫操作簡介
阿新 • • 發佈:2020-11-28
此文轉載自:https://blog.csdn.net/qq_45906219/article/details/110196150
不管妹子有還是沒有, 都絲毫不影響我們繼續程式設計 !
加油, 程式設計人.
喜歡就點一個收藏吧。 或者一個小小的贊。 謝謝。
效果圖:
實現原理:
- 建立一個類, 用來計算天數, 類中封裝好了你的類, 只需要修改一下天數就可以了。
- 玫瑰花是我從GitHub上造來的, 稍後我會給出修改後的程式碼。
快捷之處:
本篇部落格程式碼已經收錄到我的GitHub上, 連結計算特殊日子天數, 如果只是尋求程式碼, 點入自取。
思路介紹:
計算天數:
- 類如下:
import time
class GetInitialDate(object):
# The first date
# 請更換你的紀念日, 如果數量很多, 可以生成列表,
# 遍歷傳值,不懂的可以私聊我
# 其實這個天數也可以直接從物件中傳值過來,但是必須得是 ****-**-** 這樣的格式
Initial_date = "xxxx-xx-xx"
def __init__(self):
self.date_item = self.get_items
self.now_date = self._get_now_date()
self. Initial_date_sum = self.get_sum_date(self.Initial_date)
self.now_date_sum = self.get_sum_date(self.now_date)
def __str__(self):
"""稍微修飾一下,"""
return f"我來啦".center(40,
'-') + f"\n不錯喲, 你們已經認識這麼長時間了, 足足有 {self.now_date_sum - self.Initial_date_sum + 2} 天呢\n希望你們在接下里的時間, 能夠更加細膩的陪伴,感謝有你!\n" + "我一直在!".center(
40, '-')
@property
def get_items(self):
"""遍歷或者字典天數
as: {1: 31, 2: 59, 3: 90, 4: 120, 5: 151, 6: 181, 7: 212, 8: 243, 9: 273, 10: 304, 11: 334, 12: 365}
如果是閏年 後續需要加一天"""
date_items = {}
s = 0
for i in range(1, 13):
if i in [1, 3, 5, 7, 8, 10, 12]:
s += 31
elif i == 2:
s += 28
else:
s += 30
date_items[i] = s
return date_items
def get_sum_date(self, date):
""" start sum the date """
year, month, date = map(int, date.split("-"))
is_leap = True if self._is_leap(year) else False
if not is_leap:
The_date = self.date_item[month] + date
else:
The_date = self.date_item[month] + 1 + date
return The_date
def _get_now_date(self):
"""Return the localtime as: "****-**-**" date """
return time.strftime("%Y-%m-%d", time.localtime())
def _is_leap(self, year):
"year -> 1 if leap year, else 0."
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
一般我們在寫程式碼計算日期的時候, 都涉及到一個平閏年的關係, 所以在這裡也不例外,
- def is_leap 用來判斷是否是閏年,公式百度上都可以找到。
- def get_now_date 用來計算現在的日期, 並用規定的形式返回
- def get_items 用來生成一個總日期的字典, 這裡先按照平年計算, 如果後面判斷得到是閏年, 只要順次加1 就可以了。
- def get_sum_date 用來計算給定日期的總天數, 返回一個天數數值
- def str 魔術方法, 返回物件的輸出
不難看出, 其實就是計算2個日期相差就可以了。。
列印成花:
- 這個繪製玫瑰花的程式碼,很久之前就收錄了, 想過改善這個版本, 後面時間耽誤了,就直接套上了。。
程式碼如下:
-
我們只需要生成一個 GetIntitalDate物件, 去獲得這個物件返回的特定字串, 然後分割列印就可以了。
-
ggg 這個函式,就是繪製曲線,
-
start 包括線條繪製, 顏色填充, 還有字型的編寫,都濃縮排去了。
import turtle as t
class Rose:
mystr = GetInitialDate()
def __init__(self):
one, two, three, four = str(self.mystr).split("\n")
self.str_item = {1: one, 2: two, 3: three, 4: four}
self.start()
pass
def ggg(self, n, r, d=1): # 曲線繪製
for i in range(n):
t.left(d)
t.circle(r, abs(d))
def start(self):
"""draw the rose """
s = 0.2 # size
t.setup(450 * 5 * s, 750 * 5 * s)
t.pencolor("black")
t.fillcolor("red")
t.speed(-1)
t.penup()
t.goto(0, 900 * s)
t.pendown()
# 繪製花朵形狀
t.begin_fill()
t.circle(200 * s, 30)
self.ggg(60, 50 * s)
t.circle(200 * s, 30)
self.ggg(4, 100 * s)
t.circle(200 * s, 50)
self.ggg(50, 50 * s)
t.circle(350 * s, 65)
self.ggg(40, 70 * s)
t.circle(150 * s, 50)
self.ggg(20, 50 * s, -1)
t.circle(400 * s, 60)
self.ggg(18, 50 * s)
t.fd(250 * s)
t.right(150)
t.circle(-500 * s, 12)
t.left(140)
t.circle(500 * s, 110)
t.left(27)
t.circle(650 * s, 100)
t.left(130)
t.circle(-300 * s, 20)
t.right(123)
t.circle(220 * s, 57)
t.end_fill()
# 繪製花枝
t.left(120)
t.fd(280 * s)
t.left(115)
t.circle(300 * s, 33)
t.left(180)
t.circle(-300 * s, 33)
self.ggg(70, 225 * s, -1)
t.circle(350 * s, 104)
t.left(90)
t.circle(200 * s, 105)
t.circle(-500 * s, 63)
t.penup()
t.goto(150 * s, -10 * s)
t.pendown()
t.left(160)
self.ggg(20, 2400 * s)
self.ggg(220, 250 * s, -1)
# 繪製一個綠色葉子
t.fillcolor("green")
t.penup()
t.goto(670 * s, -180 * s)
t.pendown()
t.right(140)
t.begin_fill()
t.circle(300 * s, 120)
t.left(60)
t.circle(300 * s, 120)
t.end_fill()
t.penup()
t.goto(180 * s, -550 * s)
t.pendown()
t.right(85)
t.circle(600 * s, 40)
# 繪製第二個綠色葉子
t.penup()
t.goto(-150 * s, -1000 * s)
t.pendown()
t.begin_fill()
t.right(120)
t.circle(300 * s, 115)
t.left(75)
t.circle(300 * s, 100)
t.end_fill()
t.penup()
t.goto(430 * s, -1070 * s)
t.pendown()
t.right(30)
t.circle(-600 * s, 35)
# 文字部分
t.pensize(4)
t.pencolor("purple")
t.penup()
t.goto(-800 * s, -200 * s)
t.pendown()
t.write("我們不需要過多解釋", align="left", font=("arial", 10, "normal"))
t.penup()
t.goto(-800 * s, -300 * s)
t.pendown()
t.write("只需要一如既往", align="left", font=("arial", 10, "normal"))
t.penup()
t.goto(-750 * s, -400 * s)
t.pendown()
t.write("奔跑,奔跑,繼續奔跑!!", align="left", font=("arial", 10, "normal"))
# -- 繪製上方文字
for i in range(1, 5):
t.penup()
t.goto(-750 * s, 1500 * s)
if i == 1:
pass
else:
t.goto(-750 * s, (1500 - 100 * i) * s)
t.pendown()
t.write(f"{self.str_item[i]}", align="left", font=("arial", 10, "normal"))
t.hideturtle()
t.done()
a = Rose()
小情侶的特定日會不會數錯天數而煩惱? 用python編一個小程式,實現日期的計算,然後加點靈魂, 繪製成花。
喜歡就點一個收藏吧。 或者一個小的贊。 謝謝。