def decorate(f): bases = ( ModelMixin, T(f.__name__ + '_props', signal_fields(custom_fields)), ) def invoke_model(self, sim_step): try: return f(self, sim_step) except TypeError: return f(sim_step) return type(f.__name__, bases, {'__call__': invoke_model})
class Predicate(T('Predicate', 'f')): def __call__(self, *args, **kwargs): return self.f(*args, **kwargs) def __and__(self, pred): def _and_(*args, **kwargs): return self.f(*args, **kwargs) and pred(*args, **kwargs) return Predicate(_and_) def __or__(self, pred): def _or_(*args, **kwargs): return self.f(*args, **kwargs) or pred(*args, **kwargs) return Predicate(_or_) def __invert__(self): return neg(self.f)
def decorate(f): nt_name = f.__name__ + '_props' nt_fields = ' '.join(('var_', custom_fields)) bases = ( ModelMixin, FeatureMixin, T(nt_name, nt_fields) ) def invoke_model(self, sim_step): try: return f(self, self.var_, sim_step) except TypeError: return f(self.var_, sim_step) ret_type = type(f.__name__ + '_t', bases, {}) setattr(ret_type, '__call__', invoke_model) def create(*args, **kwargs): def get(var): return ret_type(var, *args, **kwargs) return get return create
WAVEFORM_ENCODE_TYPE_HCA = 2 WAVEFORM_ENCODE_TYPE_VAG = 7 WAVEFORM_ENCODE_TYPE_ATRAC3 = 8 WAVEFORM_ENCODE_TYPE_BCWAV = 9 WAVEFORM_ENCODE_TYPE_NINTENDO_DSP = 13 wave_type_ftable = { WAVEFORM_ENCODE_TYPE_ADX: ".adx", WAVEFORM_ENCODE_TYPE_HCA: ".hca", WAVEFORM_ENCODE_TYPE_VAG: ".at3", WAVEFORM_ENCODE_TYPE_ATRAC3: ".vag", WAVEFORM_ENCODE_TYPE_BCWAV: ".bcwav", WAVEFORM_ENCODE_TYPE_NINTENDO_DSP: ".dsp" } track_t = T("track_t", ("cue_id", "name", "memory_wav_id", "external_wav_id", "enc_type", "is_stream")) class TrackList(object): def __init__(self, utf): cue_handle = io.BytesIO(utf.rows[0]["CueTable"]) nam_handle = io.BytesIO(utf.rows[0]["CueNameTable"]) wav_handle = io.BytesIO(utf.rows[0]["WaveformTable"]) syn_handle = io.BytesIO(utf.rows[0]["SynthTable"]) cues = UTFTable(cue_handle) nams = UTFTable(nam_handle) wavs = UTFTable(wav_handle) syns = UTFTable(syn_handle) self.tracks = []
unit='[0/1]', start=0, min=0, max=1, step=1, ) bearing_condition = T('BearingConditon', 'left right')( left=models.Property( human_name="Condition of left door bearing", name='bearing_condition_left', unit='%', start=100, min=70, max=100, step=1, ), right=models.Property( human_name="Condition of right door bearing", name='bearing_condition_right', unit='%', start=100, min=70, max=100, step=1, ), ) current_in_red_zone = predicate(partial(within_top_margin, 0, 5, 0.05)) current_in_green_zone = ~current_in_red_zone def door_position_input(people_waiting, bearing_condition):
import numpy from .util import (single_sample) from scipy.integrate import odeint from collections import namedtuple as T def signal_fields(custom_fields=None): _signal_fields = ('name', 'human_name', 'start') custom_fields = custom_fields or '' return _signal_fields + tuple(custom_fields.split()) System = T('System', 'processes properties dependencies') SimulationStep = T('SimulationStep', 'x0 inputs duration') Property = T('Property', signal_fields('unit min max step')) class ModelMixin(): def __and__(self, feature_or_features): """ @feature_or_features is an iterable or a single element """ try: # @feature_or_features is a single callable feature = feature_or_features return feature(self) except TypeError: pass features = iter(feature_or_features) try: head = next(features) return head(self) & features
class Struct(struct.Struct): """ struct with an output filter (usually a namedtuple) """ def __init__(self, fmt, out_type): super().__init__(fmt) self.out_type = out_type def unpack(self, buf): return self.out_type(*super().unpack(buf)) utf_header_t = Struct( ">IHHIIIHHI", T("utf_header_t", ("table_size", "u1", "row_offset", "string_table_offset", "data_offset", "table_name_offset", "number_of_fields", "row_size", "number_of_rows"))) class UTFTable(object): def __init__(self, file): buf = R(file) magic = buf.uint32_t() if magic != 0x40555446: raise ValueError("bad magic") self.header = buf.struct(utf_header_t) self.name = buf.string0(at=self.header.string_table_offset + 8 + self.header.table_name_offset) buf.seek(0x20)
from collections import namedtuple as T import logging from dateutil.relativedelta import relativedelta logger = logging.getLogger(__name__) periods = ("weekly", "monthly", "yearly") BillingPeriod = T('BillingPeriod', periods)(*periods) one_day = relativedelta(days=1) def next_bill_period(active_from, period, last_period_end): logger.debug("Calculating next bill: active from %s", active_from) return ( last_period_end + one_day, last_period_end + one_day + period_to_delta(period) - one_day ) def period_to_delta(period): if period == BillingPeriod.weekly: return relativedelta(days=7) elif period == BillingPeriod.monthly: return relativedelta(months=1) elif period == BillingPeriod.yearly: return relativedelta(years=1) else:
COLUMN_TYPE_DATA: "8s", COLUMN_TYPE_STRING: "4s", COLUMN_TYPE_FLOAT: "f", COLUMN_TYPE_8BYTE: "Q", COLUMN_TYPE_4BYTE2: "i", COLUMN_TYPE_4BYTE: "I", COLUMN_TYPE_2BYTE2: "h", COLUMN_TYPE_2BYTE: "H", COLUMN_TYPE_1BYTE2: "b", COLUMN_TYPE_1BYTE: "B" } utf_header_t = Struct( ">IHHIIIHHI", T("utf_header_t", ("table_size", "u1", "row_offset", "string_table_offset", "data_offset", "table_name_offset", "number_of_fields", "row_size", "number_of_rows"))) class UTFTable(object): def __init__(self, file): buf = R(file) magic = buf.uint32_t() if magic != 0x40555446: raise ValueError("bad magic") self.header = buf.struct(utf_header_t) self.name = buf.string0(at=self.header.string_table_offset + 8 + self.header.table_name_offset) buf.seek(0x20)