晚上好!我是一名律师,我经常要计算被判刑的人多久才能获得福利,比如假释。
它的工作原理如下:
首先,我需要得到一些主要变量,比如那个人开始服刑的那一天(他被捕的那一天)。这将是第一项福利的基准日期。假设有人在2014年11月12日被捕。
base_date = datetime.date(day=12, month=11, year=2014)
我必须做的第二件事是知道每项罪行的判决是什么(有时这个人被判犯有不止一项罪行,对于每项罪行,都有不同的计算方法。假设这个人被判犯有两项罪行:
为了做到这一点,我使用了这种方法,这就是我认为我的代码失败的地方。我认为这些变量应该是datetime方法,或者类似的方法。
pena1_年,pena1_月,pena1_日=7,4,0
pena2_年,pena2_月,pena2_日=4,10,20
现在,由于第一次犯罪更加严重,他需要服刑五分之二加上第二次刑期的六分之一才能获得第一次利益。两者的结果都应添加到基准日,以便知道他何时能够获得第一项福利。我把一切都变成了一天。
pena1_total = int(((pena1_years * 365) + (pena1_months * 30) + (pena1_days)) / 5 * 2)
datedifference1 = datetime.datetime.now() - datetime.timedelta(days=pena1_total)
pena1_total = relativedelta(datetime.datetime.now(), datedifference1)
print("Pena 1: " + str(pena1_total.years) + "a, " + str(pena1_total.months) + "m, " + str(pena1_total.days) + "d.")
(打印返回:Pena 1:2a、11m、4d。)
pena2_total = int(((pena2_years * 365) + (pena2_months * 30) + (pena2_days)) / 6)
datedifference2 = datetime.datetime.now() - datetime.timedelta(days=pena2_total)
pena2_total = relativedelta(datetime.datetime.now(), datedifference2)
print("Pena 2: " + str(pena2_total.years) + "a, " + str(pena2_total.months) + "m, " + str(pena2_total.days) + "d.")
打印返回这个:佩纳2:0a,9m,21d。)
在计算了每项犯罪的分数后,我需要将两者相加,然后将其加到基准日期。在这种情况下,这名男子第一次犯罪需要服刑2年11个月零5天,第二次犯罪需要服刑9个月零23天,总共服刑3年8个月零29天,才能获得第一笔福利。但是,我需要遵循以下规则(法律规定必须这样计算):
我就是这样做的,但我不会得到2018年8月9日:
total = pena1_total + pena2_total
print("Total: " + str(total.years) + "a, " + str(total.months) + "m, " + str(total.days) + "d.")
(打印返回:总计:3a、8m、25d。)
data_semiaberto = data_base + relativedelta(days=+total.days, months=+total.months, years=+total.years)
print("Data para o semiaberto: " + str(data_semiaberto))
(打印返回此:数据第o半音:2018-08-06)
那么,第一次领取福利金的日期将是他假释的基准日期。他将需要在第一次犯罪的剩余刑期中服刑2/5,在第二次犯罪的剩余刑期中服刑1/6。为此,我需要从每项罪行的总刑期中减去他迄今为止所服的刑期。然而,我被困在这里。
以下是完整的代码:
import datetime
from dateutil.relativedelta import relativedelta
data_base = datetime.date(day=12, month=11, year=2014)
pena1_years, pena1_months, pena1_days = 7, 4, 0
pena2_years, pena2_months, pena2_days = 4, 10, 20
def calculadora():
# Fraction of the first sentence
pena1_total = int(((pena1_years * 365) + (pena1_months * 30) + (pena1_days)) / 5 * 2)
datedifference1 = datetime.datetime.now() - datetime.timedelta(days=pena1_total)
pena1_total = relativedelta(datetime.datetime.now(), datedifference1)
print("Pena 1: " + str(pena1_total.years) + "a, " + str(pena1_total.months) + "m, " + str(pena1_total.days) + "d.")
# Fraction of the second sentence
pena2_total = int(((pena2_years * 365) + (pena2_months * 30) + (pena2_days)) / 6)
datedifference2 = datetime.datetime.now() - datetime.timedelta(days=pena2_total)
pena2_total = relativedelta(datetime.datetime.now(), datedifference2)
print("Pena 2: " + str(pena2_total.years) + "a, " + str(pena2_total.months) + "m, " + str(pena2_total.days) + "d.")
# Fraction total
total = pena1_total + pena2_total
print("Total: " + str(total.years) + "a, " + str(total.months) + "m, " + str(total.days) + "d.")
# Date of the first benefit.
data_semiaberto = data_base + relativedelta(days=+total.days, months=+total.months, years=+total.years)
print("Data para o semiaberto: " + str(data_semiaberto))
calculadora()
有人能帮我吗?这会节省我很多工作时间。
通过阅读Rickey和Sathish的答案,我想出了一个解决方案。它返回了正确的日期。
仍然需要完成代码来自动化任何情况。
我不确定是否可以在这里发布GitHub。下面是代码。
import datetime, math
from dateutil.relativedelta import relativedelta
# Método dias para anos, meses e dias.
def days_to_ymd(days):
global days_to_ymd_result
years = days // 365
months = (days % 365) // 30
days = (days % 365) % 30
days_to_ymd_result = relativedelta(years=+years, months=+months, days=+days)
return days_to_ymd_result
# Método anos, meses e dias para dias.
def ymd_to_days(years, months, days):
global ymd_to_days_result
ymd_to_days_result = (years * 365) + (months * 30) + days
return ymd_to_days_result
# Método frações de dias.
def frac(days, frac):
global frac_result
frac_result = math.ceil(days * frac)
return frac_result
# Data base.
data_base = datetime.date(day=12, month=11, year=2014)
print("Data base: " + data_base.strftime("%d/%m/%Y"))
# Informações sobre as penas.
pena1 = relativedelta(years=+7, months=+4, days=+0)
pena1_totaldays = ymd_to_days(years=pena1.years, months=pena1.months, days=pena1.days)
pena2 = relativedelta(years=+4, months=+10, days=+20)
pena2_totaldays = ymd_to_days(years=pena2.years, months=pena2.months, days=pena2.days)
pena_total = pena1 + pena2
pena_total_days = ymd_to_days(years=pena_total.years, months=pena_total.months, days=pena_total.days)
print("Pena 1: " + str(pena1.years) + "a, " + str(pena1.months) + "m, " + str(pena1.days) + "d (" + str(pena1_totaldays) + " dias).")
print("Pena 2: " + str(pena2.years) + "a, " + str(pena2.months) + "m, " + str(pena2.days) + "d (" + str(pena2_totaldays) + " dias).")
print("Pena total: " + str(pena_total.years) + "a, " + str(pena_total.months) + "m, " + str(pena_total.days) + "d (" + str(pena_total_days) + " dias).")
# Frações de pena.
pena1_frac = frac(days=pena1_totaldays, frac=2/5)
pena2_frac = frac(days=pena2_totaldays, frac=1/6)
frac_total_days = pena1_frac + pena2_frac - 72
frac_total = days_to_ymd(frac_total_days)
print("2/5 de " + str(pena1.years) + "a, " + str(pena1.months) + "m, " + str(pena1.days) + "d (" + str(pena1_totaldays) + " dias): " + str(days_to_ymd(pena1_frac).years) + "a, " + str(days_to_ymd(pena1_frac).months) + "m, " + str(days_to_ymd(pena1_frac).days) + "d (" + str(pena1_frac) + " dias).")
print("1/6 de " + str(pena2.years) + "a, " + str(pena2.months) + "m, " + str(pena2.days) + "d (" + str(pena2_totaldays) + " dias): " + str(days_to_ymd(pena2_frac).years) + "a, " + str(days_to_ymd(pena2_frac).months) + "m, " + str(days_to_ymd(pena2_frac).days) + "d (" + str(pena2_frac) + " dias).")
print("Total a ser cumprido: " + str(frac_total.years) + "a, " + str(frac_total.months) + "m, " + str(frac_total.days) + "d (" + str(frac_total_days) + " dias).")
# Semiaberto.
data_semiaberto = data_base + (relativedelta(years=+frac_total.years, months=+frac_total.months, days=+(frac_total.days - 1))) # -1 = Inclui o dia do começo (art. 10, CP).
print("Data prevista para o regime semiaberto: " + str(data_semiaberto.strftime("%d/%m/%Y")))
# Pena restante.
pena1_restante_days = pena1_totaldays - pena1_frac
pena2_restante_days = pena2_totaldays - pena2_frac
pena_restante_total_days = pena1_restante_days + pena2_restante_days
pena1_restante = days_to_ymd(pena1_restante_days)
pena2_restante = days_to_ymd(pena2_restante_days)
pena_restante = days_to_ymd(pena_restante_total_days)
print("Pena 1 restante: " + str(pena1_restante.years) + "a, " + str(pena1_restante.months) + "m, " + str(pena1_restante.days) + "d (" + str(pena1_restante_days) + " dias).")
print("Pena 2 restante: " + str(pena2_restante.years) + "a, " + str(pena2_restante.months) + "m, " + str(pena2_restante.days) + "d (" + str(pena2_restante_days) + " dias).")
print("Pena total restante: " + str(pena_restante.years) + "a, " + str(pena_restante.months) + "m, " + str(pena_restante.days) + "d (" + str(pena_restante_total_days) + " dias).")
# Frações da pena.
pena1_frac = frac(days=pena1_restante_days, frac=2/5)
pena2_frac = frac(days=pena2_restante_days, frac=1/6)
frac_total_days = (pena1_frac + pena2_frac)
frac_total = days_to_ymd(frac_total_days)
print("2/5 de " + str(pena1_restante.years) + "a, " + str(pena1_restante.months) + "m, " + str(pena1_restante.days) + "d (" + str(pena1_restante_days) + " dias): " + str(days_to_ymd(pena1_frac).years) + "a, " + str(days_to_ymd(pena1_frac).months) + "m, " + str(days_to_ymd(pena1_frac).days) + "d (" + str(pena1_frac) + " dias).")
print("1/6 de " + str(pena2_restante.years) + "a, " + str(pena2_restante.months) + "m, " + str(pena2_restante.days) + "d (" + str(pena2_restante_days) + " dias): " + str(days_to_ymd(pena2_frac).years) + "a, " + str(days_to_ymd(pena2_frac).months) + "m, " + str(days_to_ymd(pena2_frac).days) + "d (" + str(pena2_frac) + " dias).")
print("Total a ser cumprido: " + str(frac_total.years) + "a, " + str(frac_total.months) + "m, " + str(frac_total.days) + "d (" + str(frac_total_days) + " dias).")
# Aberto.
data_aberto = data_semiaberto + (relativedelta(years=+frac_total.years, months=+frac_total.months, days=+(frac_total.days - 1))) # -1 = Inclui o dia do começo (art. 10, CP).
print("Data prevista para o regime aberto: " + str(data_aberto.strftime("%d/%m/%Y")))
既然我是来学习的,我想我应该尝试一下。这个代码仍然需要一些工作,但我认为这是一个好的开始。如果你抛弃它,我的感情不会受到伤害。如果你想使用它,我们可以根据需要添加功能。
import datetime as dt
from dateutil.relativedelta import relativedelta as rdelta
class Sentence:
release_date = None
def __init__(self, year=1970, month=1, day=1):
"""Create a new instance with the date of the arrest,
default is set to the unix epoch...hehehe"""
self.year = year
self.month = month
self.day = day
self.base_date = dt.date(self.year, self.month, self.day)
self.arrest_date = dt.date(self.year, self.month, self.day)
def basedate(self):
"""Returns the basedate/arrest date"""
return self.arrest_date
def penalty(self, years=0, months=0, days=0):
"""Each time the penalty method is call it accumulates
the length of the sentence. Returns release date."""
self.years = years
self.months = months
self.days = days
if Sentence.release_date is None:
Sentence.release_date = (self.base_date +
rdelta(years=self.years,
months=self.months,
days=self.days))
else:
Sentence.release_date = (Sentence.release_date +
rdelta(years=self.years,
months=self.months,
days=self.days))
return Sentence.release_date - rdelta(days=1)
def remaining(self):
"""Returns a tuple of years, months, days remaining of the
sentence"""
self.y_remaining = rdelta(Sentence.release_date, dt.date.today()).years
self.m_remaining = rdelta(Sentence.release_date,
dt.date.today()).months
self.d_remaining = rdelta(Sentence.release_date, dt.date.today()).days
return self.y_remaining, self.m_remaining, self.d_remaining
s1 = Sentence(2014, 11, 12)
print(f'Arreset Date: {"":>31}{s1.basedate()}')
print(f'Release Date after 1st sentence: {"":>12}{s1.penalty(7, 4, 0)}')
print(f'Release Date after 2nd sentence: {"":>12}{s1.penalty(4, 10, 20)}')
# print(f'Release Date after 2nd sentence: {"":>12}{s1.penalty(1, 2, 5)}')
print(f'Time remaining on sentence as of {dt.date.today()}: '
f'{s1.remaining()[0]} years, {s1.remaining()[1]} months, '
f'{s1.remaining()[2]} days')
# Output
# Arreset Date: 2014-11-12
# Release Date after 1st sentence: 2022-03-11
# Release Date after 2nd sentence: 2027-01-31
# Time remaining on sentence as of 2020-04-24: 6 years, 9 months, 8 days
检查下面的代码,它首先计算天数,然后减去每个福利的天数,并打印下一个福利使用日期的日期。
考虑每个月的闰年和天数,最好计算总天数并建立它的逻辑。
输出将打印每个福利的详细信息,并最终显示一起度过的总天数。
import datetime
from dateutil.relativedelta import relativedelta
data_base = datetime.date(day=12, month=11, year=2014)
pena1_years, pena1_months, pena1_days = 7, 4, 0
pena2_years, pena2_months, pena2_days = 4, 10, 20
total_days_spent = 0
def calculadora():
# Calculate the end date and total number of days for the first crime
pena1_end_date = data_base + relativedelta(years=+pena1_years, months=+pena1_months, days=+pena1_days)
pena1_reamining_days = (pena1_end_date - data_base).days
# Calculate the end date and total number of days for the second crime
pena2_end_date = data_base + relativedelta(years=+pena2_years, months=+pena2_months, days=+pena2_days)
pena2_reamining_days = (pena2_end_date - data_base).days
benefit_count=1
while pena1_reamining_days > 0 or pena2_reamining_days > 0:
#count the minumum number of days for both the crimes
pena1_minimum_days = round(pena1_reamining_days / 5 * 2)
if pena1_minimum_days == 0 and pena1_reamining_days > 0:
pena1_minimum_days = pena1_reamining_days
pena1_reamining_days -= pena1_minimum_days
print(f"Minimum no of days to spent in the first crime for the {benefit_count} benefit is {pena1_minimum_days} ")
pena2_minimum_days = round(pena2_reamining_days / 6)
if pena2_minimum_days == 0 and pena2_reamining_days > 0:
pena2_minimum_days = pena2_reamining_days
pena2_reamining_days -= pena2_minimum_days
print(f"Minimum no of days to spent in the second crime for the {benefit_count} benefit is {pena2_minimum_days} ")
total = pena1_minimum_days + pena2_minimum_days
global total_days_spent
total_days_spent += total
print(f"Total no of days to spent for the {benefit_count} benefit {total} ")
date_next_benefit = data_base + relativedelta(days=+total)
print(f"{benefit_count} benefit day to avail after {date_next_benefit}")
benefit_count+=1
calculadora()
print(f"Total number of days spent for both the crime is {total_days_spent}")
我正在尝试匹配从服务器下载的文件的md5sum。只有当总和匹配时,处理才会继续。 上面的代码并没有每次为某些文件正确提供md5sum。 当我去控制台检查md5sum时 下载文件的vimdiff未提供任何差异。。下载后的文件是正确的。 我无法在上述代码中看到问题。 我正在尝试更改缓冲区大小。但没有运气,所以我猜这不是因为缓冲区大小等。 问候Dheeraj Joshi
问题内容: 我正在解析一些具有the秒时间戳记datetime的数据。我使用以下代码来解析该字符串并将其转换为datetime对象: Python文档声称这不应该成为接受的问题。但是,我在上面的时间戳中收到此错误 谢谢 问题答案: 做这个: 输出为:
本文向大家介绍python计算日期之间的放假日期,包括了python计算日期之间的放假日期的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了python计算日期之间的放假日期,供大家参考,具体内容如下 代码如下: 打印结果如下: 中国 (4, [datetime.date(2014, 4, 20), datetime.date(2014, 12, 25), datetime.date(
2.4.4 Python 中真假的表示与计算* 如前所述,较新版本的 Python 引入了内建类型 bool,并且定义了布尔值 True 和 False。而在此之前,Python 曾经利用 1 和 0 来作为布尔值。 事实上,如今的 Python 在表达真假方面更加灵活——任何内建类型的值都可以解释成布尔值。例如,数值(int、long、float)可以解释成布尔值:0 为 False,非 0 值
我需要阅读由AutoCAD导出为PDF的平面图,并使用PDFBox在上面放置一些带有文本的标记。除了文字宽度的计算之外,一切都很顺利,文字的宽度写在标记旁边。 我浏览了整个PDF规范,详细阅读了其中涉及图形和文本的部分,但没有任何效果。据我所知,字形坐标空间设置在用户坐标空间的1/1000处。因此,宽度需要放大1000倍,但仍然是实际宽度的一小部分。 这就是我为定位文本所做的: *0.043f可以