本文整理汇总了Python中fractions.Fraction方法的典型用法代码示例。如果您正苦于以下问题:Python fractions.Fraction方法的具体用法?Python fractions.Fraction怎么用?Python fractions.Fraction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fractions
的用法示例。
在下文中一共展示了fractions.Fraction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: edit_teaching_equivalent_inst
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def edit_teaching_equivalent_inst(request, equivalent_id):
"""
Instructors form to edit a teaching equivalent
"""
instructor = get_object_or_404(Person, userid=request.user.username)
equivalent = get_object_or_404(TeachingEquivalent, pk=equivalent_id, instructor=instructor, status='UNCO')
if request.method == 'POST':
form = TeachingEquivForm(request.POST, instance=equivalent)
if form.is_valid():
equivalent = form.save(commit=False)
equivalent.credits_numerator = form.cleaned_data['credits_numerator']
equivalent.credits_denominator = form.cleaned_data['credits_denominator']
equivalent.save()
messages.add_message(request, messages.SUCCESS, "Teaching Equivalent successfully edited")
return HttpResponseRedirect(reverse('planning.views.view_teaching_equivalent_inst', kwargs={'equivalent_id': equivalent.id}))
else:
credits_value = Fraction("%d/%d" % (equivalent.credits_numerator, equivalent.credits_denominator)).__str__()
form = TeachingEquivForm(instance=equivalent, initial={'credits': credits_value})
return render(request, 'planning/edit_teaching_equiv_inst.html', {'form': form, 'equivalent': equivalent})
示例2: edit_teaching_equivalent_admin
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def edit_teaching_equivalent_admin(request, userid, equivalent_id):
"""
Edit a teaching equivalent for an instructor
"""
instructor = _get_instructor_for_units(request, userid)
equivalent = get_object_or_404(TeachingEquivalent, pk=equivalent_id, instructor=instructor)
if request.method == 'POST':
form = TeachingEquivForm(request.POST, instance=equivalent)
if form.is_valid():
equivalent = form.save(commit=False)
equivalent.credits_numerator = form.cleaned_data['credits_numerator']
equivalent.credits_denominator = form.cleaned_data['credits_denominator']
equivalent.save()
messages.add_message(request, messages.SUCCESS, "Teaching Equivalent successfully edited")
return HttpResponseRedirect(reverse('planning.views.view_teaching_equivalent_admin', kwargs={'userid': userid, 'equivalent_id': equivalent.id}))
else:
credits_value = Fraction("%d/%d" % (equivalent.credits_numerator, equivalent.credits_denominator)).__str__()
form = TeachingEquivForm(instance=equivalent, initial={'credits': credits_value})
return render(request, 'planning/edit_teaching_equiv_admin.html', {'form': form, 'equivalent': equivalent, 'instructor': instructor})
示例3: edit_course_offering_credits
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def edit_course_offering_credits(request, userid, course_slug):
"""
Edit the amount of credits a course offering is worth
"""
instructor = _get_instructor_for_units(request, userid)
member = get_object_or_404(Member, role='INST', person=instructor, offering__slug=course_slug)
if request.method == 'POST':
form = CourseOfferingCreditForm(request.POST)
if form.is_valid():
member.set_teaching_credit(form.cleaned_data['credits'])
member.save()
messages.add_message(request, messages.SUCCESS, "Course credits successfully updated for %s" % member.offering.name())
return HttpResponseRedirect(reverse('planning.views.view_teaching_credits_admin', kwargs={'userid': userid}))
else:
form = CourseOfferingCreditForm(initial={'credits': Fraction(member.teaching_credit()).__str__()})
return render(request, 'planning/edit_offering_credits.html', {'form': form, 'instructor': instructor, 'member': member})
示例4: fraction_display
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def fraction_display(val):
val = fractions.Fraction(val)
n = val.numerator
d = val.denominator
if n != 0:
whole = abs(n)/d*(n/abs(n)) # in case val is negative
else:
whole = 0
res = str(whole)
# only have a negative fraction if whole is 0
if val<0 and whole==0:
remainder = val + whole
else:
remainder = abs(val - whole)
if remainder != 0:
if whole == 0:
res = str(remainder)
else:
res += ' ' + str(remainder)
return res
示例5: test_object_array
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_object_array(self):
from fractions import Fraction
arr = np.array([Fraction(1, 2), Fraction(-1, 2)])
actual = np.pad(arr, (2, 3), mode='linear_ramp', end_values=0)
# deliberately chosen to have a non-power-of-2 denominator such that
# rounding to floats causes a failure.
expected = np.array([
Fraction( 0, 12),
Fraction( 3, 12),
Fraction( 6, 12),
Fraction(-6, 12),
Fraction(-4, 12),
Fraction(-2, 12),
Fraction(-0, 12),
])
assert_equal(actual, expected)
示例6: test_is_scalar_builtin_scalars
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_is_scalar_builtin_scalars(self):
assert is_scalar(None)
assert is_scalar(True)
assert is_scalar(False)
assert is_scalar(Number())
assert is_scalar(Fraction())
assert is_scalar(0.)
assert is_scalar(np.nan)
assert is_scalar('foobar')
assert is_scalar(b'foobar')
assert is_scalar(u('efoobar'))
assert is_scalar(datetime(2014, 1, 1))
assert is_scalar(date(2014, 1, 1))
assert is_scalar(time(12, 0))
assert is_scalar(timedelta(hours=1))
assert is_scalar(pd.NaT)
示例7: test_colors
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_colors(self):
limit = Limit(
ratio=Fraction(3, 4), knee=Fraction(1, 4), gain=Fraction(1, 2))
self.assertTrue(limit)
cl = color_list_test.COLORS1[:]
limit.limit_colors(cl, ListMath)
c = 106 + 1 / 4
expected = [(c, 0, 0), (0, c, 0), (0, 0, c), (c, c, c)]
self.assertEqual(len(expected), len(cl))
asserts = 0
for exp, act in zip(expected, cl):
for e, a in zip(exp, act):
self.assertAlmostEqual(e, a)
asserts += 1
self.assertEqual(asserts, 12)
示例8: test_get
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_get(self):
p = Palette([Red, Green, Blue], continuous=True)
result = [p(-1 + Fraction(i) / 2) for i in range(12)]
expected = [
(0, 170, 85),
(0, 85, 170),
(255, 0, 0),
(170, 85, 0),
(85, 170, 0),
(0, 255, 0),
(0, 170, 85),
(0, 85, 170),
(255, 0, 0),
(170, 85, 0),
(85, 170, 0),
(0, 255, 0)]
self.assertEqual(result, expected)
示例9: test_get_serpentine
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_get_serpentine(self):
p = Palette([Red, Green, Blue], serpentine=True, continuous=True)
result = [p(-1 + Fraction(i) / 2) for i in range(12)]
expected = [
(85, 170, 0),
(170, 85, 0),
(255, 0, 0),
(170, 85, 0),
(85, 170, 0),
(0, 255, 0),
(0, 170, 85),
(0, 85, 170),
(0, 0, 255),
(0, 85, 170),
(0, 170, 85),
(0, 255, 0)
]
self.assertEqual(result, expected)
示例10: test_double
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_double(self):
p = Palette([Red, Green], continuous=True)
self.assertEqual(p[0], Red)
self.assertEqual(p[1], Green)
result = [p(-1 + Fraction(i) / 3) for i in range(12)]
expected = [
(127.5, 127.5, 0),
(85, 170, 0),
(42.5, 212.5, 0),
(255, 0, 0),
(212.5, 42.5, 0),
(170, 85, 0),
(127.5, 127.5, 0),
(85, 170, 0),
(42.5, 212.5, 0),
(255, 0, 0),
(212.5, 42.5, 0),
(170, 85, 0)
]
self.assertEqual(result, expected)
示例11: test_double_serpentine
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_double_serpentine(self):
p = Palette([Red, Green], serpentine=True, continuous=True)
result = [p(-1 + Fraction(i) / 3) for i in range(12)]
expected = [
(127.5, 127.5, 0.0),
(170.0, 85.0, 0.0),
(212.5, 42.5, 0.0),
(255.0, 0.0, 0.0),
(212.5, 42.5, 0.0),
(170.0, 85.0, 0.0),
(127.5, 127.5, 0.0),
(85.0, 170.0, 0.0),
(42.5, 212.5, 0.0),
(0.0, 255.0, 0.0),
(42.5, 212.5, 0.0),
(85.0, 170.0, 0.0)
]
self.assertEqual(result, expected)
示例12: test_continuous
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_continuous(self):
colors = [Red, Green, Blue, White]
p = Palette(colors, continuous=True)
result = [p(-1 + Fraction(i) / 3) for i in range(16)]
expected = [
(63.75, 63.75, 255.0),
(127.5, 127.5, 255.0),
(191.25, 191.25, 255.0),
(255.0, 0.0, 0.0),
(191.25, 63.75, 0.0),
(127.5, 127.5, 0.0),
(63.75, 191.25, 0.0),
(0.0, 255.0, 0.0),
(0.0, 191.25, 63.75),
(0.0, 127.5, 127.5),
(0.0, 63.75, 191.25),
(0.0, 0.0, 255.0),
(63.75, 63.75, 255.0),
(127.5, 127.5, 255.0),
(191.25, 191.25, 255.0),
(255.0, 0.0, 0.0),
]
self.assertEqual(expected, result)
示例13: test_continuous_serpentine
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def test_continuous_serpentine(self):
colors = [Red, Green, Blue, White]
p = Palette(colors, serpentine=True, continuous=True)
result = [p(-1 + Fraction(i) / 3) for i in range(15)]
expected = [
(63.75, 191.25, 0.0),
(127.5, 127.5, 0.0),
(191.25, 63.75, 0.0),
(255.0, 0.0, 0.0),
(191.25, 63.75, 0.0),
(127.5, 127.5, 0.0),
(63.75, 191.25, 0.0),
(0.0, 255.0, 0.0),
(0.0, 191.25, 63.75),
(0.0, 127.5, 127.5),
(0.0, 63.75, 191.25),
(0.0, 0.0, 255.0),
(63.75, 63.75, 255.0),
(127.5, 127.5, 255.0),
(191.25, 191.25, 255.0)
]
self.assertEqual(expected, result)
示例14: getStreamImage
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def getStreamImage(isDay):
# Capture an image stream to memory based on daymode
with picamera.PiCamera() as camera:
camera.resolution = (testWidth, testHeight)
with picamera.array.PiRGBArray(camera) as stream:
if isDay:
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
time.sleep(motionCamSleep) # sleep so camera can get AWB
else:
# use variable framerate_range for Low Light motion image stream
camera.framerate_range = (Fraction(1, 6), Fraction(30, 1))
time.sleep(2) # Give camera time to measure AWB
camera.iso = nightMaxISO
camera.capture(stream, format='rgb', use_video_port=useVideoPort)
camera.close()
return stream.array
#-----------------------------------------------------------------------------------------------
示例15: match_string
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import Fraction [as 别名]
def match_string(string):
"""Match a string against the expected format for a :class:`Fraction`
(``[-]numerator/denominator``) and return the numerator and denominator
as a tuple.
Args:
string -- a string representation of a rational number
Return: a tuple (numerator, denominator)
Raise ValueError: if the format of the string is invalid
"""
format_re = re.compile(r'(?P<numerator>-?\d+)/(?P<denominator>\d+)')
match = format_re.match(string)
if match is None:
raise ValueError('Invalid format for a rational: %s' % string)
gd = match.groupdict()
return (int(gd['numerator']), int(gd['denominator']))