class CatchAllParagraph(ParagraphMixin): """ A catch-all paragraph: everything is fed to the extra_data. Every field is treated as formatted text. """ extra_data = attrib(default=Factory(dict)) @classmethod def from_dict(cls, data): # Stuff all data in the extra_data mapping as FormattedTextField assert isinstance(data, dict) known_data = {} for key, value in data.items(): key = key.replace('-', '_') known_data[key] = FormattedTextField.from_value(value) return cls(extra_data=known_data) def to_dict(self): data = {} for field_name, field_value in self.extra_data.items(): if field_value: if hasattr(field_value, 'dumps'): field_value = field_value.dumps() data[field_name] = field_value return data def is_all_unknown(self): return all(k == 'unknown' for k in self.to_dict()) def is_valid(self, strict=False): if strict: return False return not self.is_all_unknown()
class AudioBlockFormatMatrix(AudioBlockFormat): """ADM audioBlockFormat with typeDefinition == "Matrix" Attributes: outputChannelFormat (Optional[AudioChannelFormat]) matrix (list[MatrixCoefficient]) """ outputChannelFormat = attrib(default=None, validator=optional(instance_of(AudioChannelFormat))) matrix = attrib(default=Factory(list), validator=list_of(MatrixCoefficient)) outputChannelFormatIDRef = attrib(default=None) def lazy_lookup_references(self, adm): if self.outputChannelFormatIDRef is not None: self.outputChannelFormat = adm.lookup_element(self.outputChannelFormatIDRef) self.outputChannelFormatIDRef = None for coefficient in self.matrix: coefficient.lazy_lookup_references(adm) def validate(self): super(AudioBlockFormatMatrix, self).validate() for coefficient in self.matrix: coefficient.validate()
class LinearPotentialFlowResult: problem = attrib() sources = attrib(default=None, init=False, repr=False) potential = attrib(default=None, init=False, repr=False) fs_elevation = attrib(default=Factory(dict), init=False, repr=False) __str__ = LinearPotentialFlowProblem.__str__ def __getattr__(self, name): """Direct access to the attributes of the included problem.""" try: return getattr(self.problem, name) except AttributeError: raise AttributeError( f"{self.__class__} does not have a attribute named {name}.") @property def settings_dict(self): settings = asdict(self.problem) # Keep only the name of the body, not the full object. settings['body_name'] = self.body.name del settings['body'] # Keep only water_depth # TODO: Remove. settings['water_depth'] = self.free_surface - self.sea_bottom del settings['free_surface'] del settings['sea_bottom'] return settings
class Controller: mapping = attrib(type=dict) direction = attrib( type=pygame.Rect, default=Factory(lambda: pygame.Rect(0, 0, 0, 0)), ) fire = attrib(type=bool, default=False)
class ClickTestResult(object): """ Captured results after testing a click command. """ echoOutputType: ClassVar = List[Tuple[str, Mapping[str, Any]]] exitCode: Union[int, None, Internal] = Internal.UNSET echoOutput: echoOutputType = Factory(list) stdin: StringIO = Factory(StringIO) stdout: StringIO = Factory(StringIO) stderr: StringIO = Factory(StringIO) beginLoggingToCalls: Sequence[Tuple[Sequence[str], Mapping[str, str]]] = ()
class PlayerState(object): deck = attrib(default=Factory(list)) discard = attrib() num_turns = attrib(default=0) @discard.default def _discard_default(self): return [card_types["copper"]] * 7 + [card_types["estate"]] * 3 def get_cards(self, n, up_to=False): """Get n cards from the deck, shuffling if necessary. if up_to, return fewer cards if there are not enough available.""" if len(self.deck) < n: shuffle(self.discard) self.deck, self.discard = self.deck + self.discard, [] if up_to: n = min(len(self.deck), n) else: assert n <= len(self.deck), "ran out of cards" res, self.deck = self.deck[:n], self.deck[n:] return res def score(self): """Total victory points of all owned cards.""" cards = self.deck + self.discard return sum(card.victory for card in cards)
class Performance: drums: MidiOut bass: MidiOut metronome: Metronome = Factory(Metronome) last_note: int = 48 async def play_drum( self, note: int, pulses: int, volume: int = 127, decay: float = 0.5 ) -> None: await self.play(self.drums, 9, note, pulses, volume, decay) async def play_bass( self, note: int, pulses: int, volume: int = 127, decay: float = 0.5 ) -> None: await self.play(self.bass, 0, note, pulses, volume, decay) async def play( self, out: MidiOut, channel: int, note: int, pulses: int, volume: int, decay: float = 0.5, ) -> None: note_on_length = int(round(pulses * decay, 0)) rest_length = pulses - note_on_length out.send_message([NOTE_ON | channel, note, volume]) await self.wait(note_on_length) out.send_message([NOTE_OFF | channel, note, volume]) await self.wait(rest_length) async def wait(self, pulses: int) -> None: await self.metronome.wait(pulses)
class AttrElement(Element): """An xml sub-element whose text contents is converted to a constructor kwarg, either directly or via some conversion.""" arg_name = attrib() attr_name = attrib(default=Factory(lambda self: self.arg_name, takes_self=True)) type = attrib(default=StringType) required = attrib(default=False) default = attrib(default=None) parse_only = attrib(default=False) def get_handlers(self): arg_name = self.arg_name convert = self.type.loads if convert is None: def f(kwargs, element): if arg_name in kwargs: raise ValueError("multiple {attr_name} elements found".format(attr_name=self.attr_name)) kwargs[arg_name] = text(element) else: def f(kwargs, element): if arg_name in kwargs: raise ValueError("multiple {attr_name} elements found".format(attr_name=self.attr_name)) kwargs[arg_name] = convert(text(element)) return [("element", qname, f) for qname in qnames(self.adm_name)] def to_xml(self, element, obj): if self.parse_only: return attr = getattr(obj, self.attr_name) if attr != self.default: new_el = element.makeelement(QName(default_ns, self.adm_name)) new_el.text = self.type.dumps_func(attr) element.append(new_el)
class ListElement(Element): """An xml sub-element whose text contents is added to a list in the constructor kwargs, either directly or via some conversion.""" arg_name = attrib() attr_name = attrib(default=Factory(lambda self: self.arg_name, takes_self=True)) type = attrib(default=StringType) required = attrib(default=False) parse_only = attrib(default=False) def get_handlers(self): arg_name = self.arg_name convert = self.type.loads if convert is None: def f(kwargs, element): kwargs.setdefault(arg_name, []).append(text(element)) else: def f(kwargs, element): kwargs.setdefault(arg_name, []).append(convert(text(element))) return [("element", qname, f) for qname in qnames(self.adm_name)] def to_xml(self, element, obj): if self.parse_only: return for data in getattr(obj, self.attr_name): new_el = element.makeelement(QName(default_ns, self.adm_name)) new_el.text = self.type.dumps_func(data) element.append(new_el)
class Payload(object): mtr_hash = attrib() metadata = attrib() nonce = attrib(default=False) timestamp = attrib(default=Factory(lambda: time())) version = attrib(default=PROTOCOL_VERSION) @staticmethod def build(tree, nonce, identity_info=None): metadata = Metadata( params=LocalParams.get_default().public_export(), identity_info=identity_info) if tree.root_hash is not None: mtr_hash = bytes2ascii(tree.root_hash) else: mtr_hash = None return Payload(metadata=metadata, mtr_hash=mtr_hash, nonce=bytes2ascii(nonce)) @staticmethod def from_dict(exported): raw_metadata = exported["metadata"] raw_payload = dict(exported) raw_payload['metadata'] = Metadata(**raw_metadata) return Payload(**raw_payload) def export(self): return asdict(self)
class patternModelTraining(object): params = attrib(default=Factory(PatternTaskParams)) net = attrib() dataloader = attrib() criterion = attrib() optimizer = attrib() @net.default def default_net(self): # See dataloader documentation net = EncapsulatedNTM(self.params.sequence_width, self.params.sequence_width + 1, self.params.controller_size, self.params.controller_layers, self.params.num_heads, self.params.memory_n, self.params.memory_m) return net @dataloader.default def default_dataloader(self): return dataloader(self.params.num_batches, self.params.batch_size, self.params.sequence_width, self.params.sequence_min_len, self.params.sequence_max_len) @criterion.default def default_criterion(self): return nn.BCELoss() @optimizer.default def default_optimizer(self): return optim.RMSprop(self.net.parameters(), momentum=self.params.rmsprop_momentum, alpha=self.params.rmsprop_alpha, lr=self.params.rmsprop_lr)
class TaskModelTraining(object): params = attrib(default=Factory(TaskParams)) net, dataloader, criterion, optimizer = attrib(), attrib(), attrib( ), attrib() @staticmethod def loss_fn(net, X, Y, criterion): state = net.create_new_state() output = None for i in range(X.size(0)): is_last = i == X.size(0) - 1 output, state = net(X[i], state, do_fc=is_last) assert not output is None loss = criterion(output, Y) assert not hasnan(loss) return loss @staticmethod def loss_fn(net, X, Y, criterion): state = net.create_new_state() _, (lstm_h, lstm_c) = net.lstm(X, state) output = net.linear(lstm_h) loss = criterion(output, Y) assert not hasnan(loss) return loss @staticmethod def forward_fn(net, X): state = net.create_new_state() _, (lstm_h, lstm_c) = net.lstm(X, state) output = net.linear(lstm_h) return output def dataloader_fn(self): """ Creates a news dataloader generator, for when the old one is exhausted """ return dataloader(**get_valid_fct_args(dataloader, self.params)) @net.default def default_net(self): net = getattr(models, self.params.model_type) net = net(**get_valid_fct_args(net.__init__, self.params)) return net @dataloader.default def default_dataloader(self): return dataloader(**get_valid_fct_args(dataloader, self.params)) @criterion.default def default_criterion(self): return nn.MSELoss() @optimizer.default def default_optimizer(self): return optim.RMSprop(self.net.parameters(), momentum=self.params.rmsprop_momentum, alpha=self.params.rmsprop_alpha, lr=self.params.rmsprop_lr)
class LeakedCookie: config: RunConfig = attrib() _result: List[Dict[str, str]] = attrib(default=Factory(dict)) @classmethod async def from_config(cls, config: RunConfig) -> 'LeakedCookie': """ reads config variable and creates class :param config: RunConfig config for starting :return: LeakedCookie """ return cls(config=config) async def run(self) -> None: """ starts async jobs from the main script file :return: None """ # create pretty printer printer = PrettyPrint(self.config.no_color) # makes request to receive cookies cookies = await Requests.make_request_to_target(URL(self.config.url), printer) # create cookie parser cookie_parser = CookiesParser(cookies) # print if needs found cookies from the target if self.config.print_cookies and cookies: printer.print_cookies(cookies) # parsing cookies self.result = await cookie_parser.parse_cookies() # if not quiet print result to stdout if not self.config.quiet: printer.print_result(self.result) # create write to file class file_handler = ReadWriteDocuments(self.config.output) # saving results to a file file_handler.save_result_to_file(self.result) @property def result(self) -> List[Dict[str, str]]: """ list of found unsecure cookies :return: List[Dict[str, str]] """ return self._result @result.setter def result(self, result: List[Dict[str, str]]) -> None: """ setter for result :param result: Dict[str, str] :return: None """ self._result = result
class EmptyLineTracker: """Provides a stateful method that returns the number of potential extra empty lines needed before and after the currently processed line. Note: this tracker works on lines that haven't been split yet. """ previous_line: Optional[Line] = attrib(default=None) previous_after: int = attrib(default=0) previous_defs: List[int] = attrib(default=Factory(list)) def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]: """Returns the number of extra empty lines before and after the `current_line`. This is for separating `def`, `async def` and `class` with extra empty lines (two on module-level), as well as providing an extra empty line after flow control keywords to make them more prominent. """ before, after = self._maybe_empty_lines(current_line) self.previous_after = after self.previous_line = current_line return before, after def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]: before = 0 depth = current_line.depth while self.previous_defs and self.previous_defs[-1] >= depth: self.previous_defs.pop() before = (1 if depth else 2) - self.previous_after is_decorator = current_line.is_decorator if is_decorator or current_line.is_def or current_line.is_class: if not is_decorator: self.previous_defs.append(depth) if self.previous_line is None: # Don't insert empty lines before the first line in the file. return 0, 0 if self.previous_line and self.previous_line.is_decorator: # Don't insert empty lines between decorators. return 0, 0 newlines = 2 if current_line.depth: newlines -= 1 newlines -= self.previous_after return newlines, 0 if current_line.is_flow_control: return before, 1 if (self.previous_line and self.previous_line.is_import and not current_line.is_import and depth == self.previous_line.depth): return (before or 1), 0 if (self.previous_line and self.previous_line.is_yield and (not current_line.is_yield or depth != self.previous_line.depth)): return (before or 1), 0 return before, 0
class Message(object): category = attrib() src = attrib(validator=assert_not_empty) dst = attrib() text = attrib() note = attrib() when = attrib() data = attrib(default=Factory(dict))
class ProviderSettings: """Provider settings.""" name: str description: str provider: Optional[str] = None args: Dict[Optional[str], Any] = Factory(dict) primary: bool = False
class Position(object): """ A Ranger position. """ positionID: str name: str members: Set[Ranger] = Factory(set)
class BracketTracker: depth: int = attrib(default=0) bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = attrib(default=Factory(dict)) delimiters: Dict[LeafID, Priority] = attrib(default=Factory(dict)) previous: Optional[Leaf] = attrib(default=None) def mark(self, leaf: Leaf) -> None: if leaf.type == token.COMMENT: return if leaf.type in CLOSING_BRACKETS: self.depth -= 1 opening_bracket = self.bracket_match.pop((self.depth, leaf.type)) leaf.opening_bracket = opening_bracket # type: ignore leaf.bracket_depth = self.depth # type: ignore if self.depth == 0: delim = is_delimiter(leaf) if delim: self.delimiters[id(leaf)] = delim elif self.previous is not None: if leaf.type == token.STRING and self.previous.type == token.STRING: self.delimiters[id(self.previous)] = STRING_PRIORITY elif (leaf.type == token.NAME and leaf.value == 'for' and leaf.parent and leaf.parent.type in {syms.comp_for, syms.old_comp_for}): self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY elif (leaf.type == token.NAME and leaf.value == 'if' and leaf.parent and leaf.parent.type in {syms.comp_if, syms.old_comp_if}): self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY if leaf.type in OPENING_BRACKETS: self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf self.depth += 1 self.previous = leaf def any_open_brackets(self) -> bool: """Returns True if there is an yet unmatched open bracket on the line.""" return bool(self.bracket_match) def max_priority(self, exclude: Iterable[LeafID] = ()) -> int: """Returns the highest priority of a delimiter found on the line. Values are consistent with what `is_delimiter()` returns. """ return max(v for k, v in self.delimiters.items() if k not in exclude)
class ProviderSettings: """Provider settings.""" name: str description: str provider: Optional[str] = None args: ProviderArgs = Factory(ProviderArgs) # type: ignore primary: bool = False
class RvtSession(object): session_id = attrib() start = attrib(default=Factory(str)) end = attrib(default=Factory(str)) duration = attrib(default=Factory(set)) build = attrib(default=Factory(str)) hosts = attrib(default=Factory(str)) central = attrib(default=Factory(str)) syncs = attrib(default=Factory(list)) links = attrib(default=Factory(list))
class CopyTaskModelTraining(object): params = attrib(default=Factory(CopyTaskParams)) net = attrib() dataloader = attrib() criterion = attrib() optimizer = attrib() @net.default def default_net(self): # Choose between Neural Turing Machine or Vanilla LSTM ENCAPSULATION = { 'NTM-LSTM': EncapsulatedNTM, 'NTM-FFW': EncapsulatedNTM, 'LSTM': VanillaLSTM } encapsulation = ENCAPSULATION[self.params.controller_type] head_activation_type = {'relu': F.relu, 'softplus': F.softplus} # Arguments for Classical model # We have 1 additional input for the delimiter which is passed on a # separate "control" channel classic_args = (self.params.sequence_width + 1, self.params.sequence_width, self.params.controller_size, self.params.controller_layers) # Arguments for Neural Turing Machine Model ntm_args = classic_args + ( self.params.num_heads, self.params.memory_n, self.params.memory_m, self.params.controller_type, head_activation_type[self.params.head_activation_type]) # Choice of Args ARGUMENTS = {EncapsulatedNTM: ntm_args, VanillaLSTM: classic_args} net = encapsulation(*ARGUMENTS[encapsulation]) return net @dataloader.default def default_dataloader(self): return dataloader(self.params.num_batches, self.params.batch_size, self.params.sequence_width, self.params.sequence_min_len, self.params.sequence_max_len) @criterion.default def default_criterion(self): return nn.BCELoss() @optimizer.default def default_optimizer(self): return optim.RMSprop(self.net.parameters(), momentum=self.params.rmsprop_momentum, alpha=self.params.rmsprop_alpha, lr=self.params.rmsprop_lr)
class Intercept: """Allows text to be gathered from the user, bypassing normal command processing.""" func = attrib() args = attrib(default=Factory(tuple)) kwargs = attrib(default=Factory(dict)) multiline = attrib(default=Factory(bool)) lines = attrib(default=Factory(list)) end = attrib(default=Factory(lambda: '.')) abort = attrib(default=Factory(lambda: '@abort')) connection = attrib(default=Factory(lambda: None)) def feed(self, line): """Add text to self.text.""" if line == self.abort: self.connection.notify('Aborted.') self.connection.intercept = None self.lines.append(line) if not self.multiline or line == self.end: if self.multiline: # We don't want the end character in the buffer. self.lines.pop() self.connection.intercept = None self.func('\n'.join(self.lines), *self.args, **self.kwargs)
class Line: """A generic line.""" text = attrib() _gag = attrib(default=Factory(lambda: False), init=False) _sub = attrib(default=Factory(lambda: False), init=False) def __attrs_post_init__(self): try: self.text = str(self.text.decode(errors='ignore')) except AttributeError: pass # Not needed. self._gag = False self._sub = None def gag(self): """Gag the received text.""" self._gag = True def ungag(self): """Ungag the text.""" self._gag = False def gagged(self): """Return whether the text is gagged.""" return self._gag def substitute(self, text): """Substitute the actual text with the provided text.""" self._sub = text def unsubstitute(self): """Remove any substitution.""" self._sub = None def substituted(self): """Return whether the text is substituted or not.""" return self._sub is not None def get_text(self): """Get the text from the line.""" if not self.gagged(): if self.substituted(): return self._sub else: return self.text
class MockRouter(object): """ Mock router. """ runsSeen = attrib(default=Factory(list)) def run(self, **kwargs): self.runsSeen.append(kwargs)
class Hotel(object): name = attrib(converter=str.strip) address = attrib(converter=str.strip) stars = attrib(converter=str.strip) contact = attrib(converter=str.strip) phone = attrib(converter=str.strip) uri = attrib(converter=str.strip) errors = attrib(default=Factory(list)) @classmethod def from_row(cls, row): name, address, stars, contact, phone, uri = row return cls(name, address, stars, contact, phone, uri) def is_valid(self): return len(self.errors) == 0 def as_dict(self): data = OrderedDict([ ('name', self.name), ('address', self.address), ('stars', self.stars), ('contact', self.contact), ('phone', self.phone), ('uri', self.uri) ]) if self.errors: data['errors'] = self.errors return data @name.validator def validate_name(self, attribute, value): try: value.encode('utf-8') except UnicodeEncodeError: self.errors.append('Name is not UTF-8 valid.') @uri.validator def validate_uri(self, attribute, value): if not re.match(uri_regex, value): self.errors.append( 'URI value: "{}"" is not a valid url.'.format(value)) @stars.validator def validate_stars(self, attribute, value): try: self.stars = int(value) except (ValueError, TypeError): self.errors.append( 'Stars value: "{}"" is not a valid integer.'.format(value)) return if not 0 <= self.stars <= 5: self.errors.append( 'Stars value: "{}"" should be between 0 and 5.'.format(value))
class RestAPI(ManagedModel): resource_type = 'rest_api' swagger_doc = attrib() api_gateway_stage = attrib() lambda_function = attrib() authorizers = attrib(default=Factory(list)) def dependencies(self): return [self.lambda_function] + self.authorizers
class Contact(object): id = attr.ib(Factory(_contact_autoincrement), init=False) name = attr.ib('') pref_name = attr.ib('') emails = attr.ib(Factory(list)) primary_email = attr.ib(Factory(_default_email, takes_self=True)) company = attr.ib('') location = attr.ib('') add_date = attr.ib(Factory(datetime.datetime.now)) # The next two parts are part of the Django-esque Manager pattern, # mentioned in the ContactManager docstring objects = ContactManager() def save(self): self.objects.save(self)
class StreamerTrack(Track): url = attrib(default=Factory(lambda: None)) def __attrs_post_init__(self): self.title = self.url def get_stream(self): return URLStream(self.title.encode())
class SoundLoader(object): server = attrib() world = attrib() pack = attrib(default=Factory(str)) zdata = attrib(default=Factory(lambda: None), repr=False, init=False) pdata = attrib(default=Factory(dict), repr=False, init=False) cache = attrib(default=Factory(dict), repr=False, init=False) def __attrs_post_init__(self): if self.pack != "": self.zdata = zipfile.ZipFile(self.pack) self.pdata = { name: self.zdata.read(name) for name in self.zdata.namelist() } def get_position(self): position = self.source.position return position def set_position(self, position): self.source.position = position position = property(get_position, set_position) def play_stationary(self, key, m): s = self.load_sound(key) s.source.position = m.x, m.y, m.z s.play() def load_sound(self, key): self.source = libaudioverse.SourceNode(self.server, self.world) if key not in self.cache: b = libaudioverse.Buffer(self.server) if self.pdata == "": b.load_from_file(key) #else: #b.decode_from_array(self.pdata[key]) self.cache[key] = b b = self.cache[key] n = libaudioverse.BufferNode(self.server) n.buffer.value = b return Sound(n, self.source)
class RestAPI(ManagedModel): resource_type = 'rest_api' swagger_doc = attrib() # type: DV[Dict[str, Any]] api_gateway_stage = attrib() # type: str lambda_function = attrib() # type: LambdaFunction authorizers = attrib(default=Factory(list)) # type: List[LambdaFunction] def dependencies(self): # type: () -> List[Model] return cast(List[Model], [self.lambda_function] + self.authorizers)