def iadd(t1, offset):
fractional, whole = math.modf(offset)
t1.twsec += whole
t1.tfsec += fractional
normalize(t1)
return t1
python类modf()的实例源码
def iadd(t1, offset):
fractional, whole = math.modf(offset)
t1.twsec += whole
t1.tfsec += fractional
normalize(t1)
return t1
def normalize(tstamp):
# Get fractional adjustment from whole seconds
fadj, tstamp.twsec = math.modf(tstamp.twsec)
# Adjust fractional seconds and get whole seconds adjustment
tstamp.tfsec, wadj = math.modf(tstamp.tfsec + fadj)
# If fractional seconds are negative, borrow a second from the whole
# seconds to make it positive, normalizing to [0,1)
if (tstamp.tfsec < 0.0):
tstamp.tfsec += 1.0;
wadj -= 1.0;
tstamp.twsec += wadj;
def iadd(t1, offset):
fractional, whole = math.modf(offset)
t1.twsec += whole
t1.tfsec += fractional
normalize(t1)
return t1
def microtime(get_as_float = False) :
if get_as_float:
return time.time()
else:
return '%.8f %d' % math.modf(time.time())
def day_time(connection, value=None):
if value is not None:
if not connection.admin:
return S_NO_RIGHTS
value = float(value)
if value < 0.0:
raise ValueError()
connection.protocol.current_time = value
connection.protocol.update_day_color()
f, i = modf(connection.protocol.current_time)
return S_TIME_OF_DAY.format(hours=int(i), minutes=int(f * 60))
def suspend (timeout):
a, b = math.modf (timeout)
for i in range (int (b)):
socketpool.noop ()
time.sleep (1)
time.sleep (a)
def get_sampled_frames(start, end, sampling):
return [math.modf(start + x * sampling) for x in range(int((end - start) / sampling) + 1)]
def float2text(number_float):
cent, euro = modf(number_float)
cent = round(cent * 100)
zahlwort = num2text(int(euro)) + ' Euro'
if (cent > 0):
zahlwort += ' und ' + num2text(int(cent)) + ' Cent'
zahlwort = zahlwort[0].upper() + zahlwort[1:]
return zahlwort
def fractionalDayToTime(frac):
ofrac = frac
(frac, h) = math.modf(frac*24)
(frac, m) = math.modf(frac*60)
(frac, s) = math.modf(frac*60)
return datetime.time(h, m, s, int(frac*1.0e6))
## Convert time in seconds-since-start-of-day into datetime.time
def setHue ( self, newhue ):
""" Modifies hue value of Colz object """
if isinstance( newhue, int ):
newhue /= 360.0
if newhue > 1.0:
newhue, whole = math.modf(newhue) # Keep decimal part
self.h = newhue
self.hsl[0] = newhue
self.hsla[0] = newhue
self.updateFromHsl()
def hueLerp ( h1, h2, amt ):
""" Generic linear interpolation """
if isinstance( h1, int ):
h1 = h1 / 360.0
if isinstance( h2, int ):
h2 = h2 / 360.0
hue = h1 + Colz.shortHueDist( h1, h2) * amt
if hue > 1.0:
hue, whole = math.modf(hue) # Keep decimal part
if hue < 0.0:
hue, whole = math.modf(hue) # Keep decimal part
hue = 1.0 + hue
return hue
def next_generation(self):
next_population = []
wheel = []
for permutation in self.population:
value = self.evaluation[id(permutation)]
fitness = value / self.avg_value
fitness_fraction, copies = math.modf(fitness)
if fitness_fraction:
wheel.append(permutation)
wheel.append(permutation)
if copies == 1:
next_population.append(permutation)
elif copies > 1:
next_population += [permutation] * int(copies)
# XXX: fractional copies should be placed with a proportinal probability (FMD2)
next_population += random.sample(wheel, len(self.population) - len(next_population))
next_population_size = len(next_population)
# crossover
crossover_count = int(0.5 * self.crossover_p * next_population_size)
crossover_indices = random.sample(range(next_population_size), 2 * crossover_count)
for i in range(crossover_count):
index_a, index_b = crossover_indices[2*i], crossover_indices[2*i+1]
next_population[index_a], next_population[index_b] = self.crossover(next_population[index_a], next_population[index_b])
# mutation
for mutation, p in self.mutations:
for index in random.sample(range(next_population_size), int(p * next_population_size)):
next_population[index] = mutation(next_population[index])
self.population = next_population
self.generation_count += 1
self.evaluate()
def addBubbleToTop(bubbleArray, bubble):
posx = bubble.rect.centerx
leftSidex = posx - BUBBLERADIUS
columnDivision = math.modf(float(leftSidex) / float(BUBBLEWIDTH))
column = int(columnDivision[1])
if columnDivision[0] < 0.5:
bubbleArray[0][column] = copy.copy(bubble)
else:
column += 1
bubbleArray[0][column] = copy.copy(bubble)
row = 0
return row, column
def TimeDelta_or_None(s):
try:
h, m, s = s.split(':')
h, m, s = int(h), int(m), float(s)
td = timedelta(hours=abs(h), minutes=m, seconds=int(s),
microseconds=int(math.modf(s)[0] * 1000000))
if h < 0:
return -td
else:
return td
except ValueError:
# unpacking or int/float conversion failed
return None
def Time_or_None(s):
try:
h, m, s = s.split(':')
h, m, s = int(h), int(m), float(s)
return time(hour=h, minute=m, second=int(s),
microsecond=int(math.modf(s)[0] * 1000000))
except ValueError:
return None
def latlng2dm(latlng):
"""
Degrees Minutes representation of LatLng
:param latlng: LatLng
:return: unicode
>>> latlng2dm(LatLng(45.5, 30.5))
u'N4530.0E03030.0'
"""
def dm(v, pattern):
f, degrees = math.modf(abs(v))
cents, minutes = math.modf(f * 60)
cents = round(cents * 10)
if cents >= 10:
cents = 0
minutes += 1
return pattern.format(
int(degrees),
int(minutes),
int(cents)
)
return '{0}{1}{2}{3}'.format(
'N' if latlng.latitude >= 0 else 'S',
dm(latlng.latitude, '{0:0>2d}{1:0>2d}.{2}'),
'E' if latlng.longitude > 0 else 'W',
dm(latlng.longitude, '{0:0>3d}{1:0>2d}.{2}'),
)
def microtime(get_as_float=False):
if get_as_float:
return time.time()
else:
return '%.8f %d' % math.modf(time.time())
def microtime(get_as_float=False):
if get_as_float:
return time.time()
else:
return '%.8f %d' % math.modf(time.time())
def minutes_to_str(m):
frac,m = math.modf(m)
hours = m//60
minutes = m % 60
seconds = frac * 60
return "%02d:%02d.%02.f" % (hours,minutes,seconds)
# Read the stock history file as a list of lists