def a2s_rules(server_addr, timeout=2, challenge=0, binary=False): """Get rules from server :param server_addr: (ip, port) for the server :type server_addr: tuple :param timeout: (optional) timeout in seconds :type timeout: float :param challenge: (optional) challenge number :type challenge: int :param binary: (optional) return rules as raw bytes :type binary: bool :raises: :class:`RuntimeError`, :class:`socket.timeout` :returns: a list of rules :rtype: :class:`dict` """ ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ss.connect(server_addr) ss.settimeout(timeout) # request challenge number if challenge in (-1, 0): ss.send(_pack('<lci', -1, b'V', challenge)) try: _, header, challenge = _unpack_from('<lcl', ss.recv(512)) except: ss.close() raise if header != b'A': raise RuntimeError("Unexpected challenge response") # request player info ss.send(_pack('<lci', -1, b'V', challenge)) try: data = StructReader(_handle_a2s_response(ss)) finally: ss.close() header, num_rules = data.unpack('<4xcH') if header != b'E': raise RuntimeError("Invalid response header - %s" % repr(header)) rules = {} while len(rules) != num_rules: name = data.read_cstring(binary=binary) value = data.read_cstring(binary=binary) if not binary: if _re_match(r'^\-?[0-9]+$', value): value = int(value) elif _re_match(r'^\-?[0-9]+\.[0-9]+$', value): value = float(value) rules[name] = value return rules
def a2s_rules(server_addr, timeout=2, challenge=0): """Get rules from server :param server_addr: (ip, port) for the server :type server_addr: tuple :param timeout: (optional) timeout in seconds :type timeout: float :param challenge: (optional) challenge number :type challenge: int :raises: :class:`RuntimeError`, :class:`socket.timeout` :returns: a list of players :rtype: :class:`list` """ ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ss.connect(server_addr) ss.settimeout(timeout) # request challenge number if challenge in (-1, 0): ss.send(_pack('<lci', -1, b'V', challenge)) try: _, header, challenge = _unpack_from('<lcl', ss.recv(512)) except: ss.close() raise if header != b'A': raise RuntimeError("Unexpected challenge response") # request player info ss.send(_pack('<lci', -1, b'V', challenge)) try: data = StructReader(_handle_a2s_response(ss)) finally: ss.close() header, num_rules = data.unpack('<4xcH') if header != b'E': raise RuntimeError("Invalid reponse header - %s" % repr(header)) rules = {} while len(rules) != num_rules: name = data.read_cstring() value = data.read_cstring() if _re_match(r'^\-?[0-9]+$', value): value = int(value) elif _re_match(r'^\-?[0-9]+\.[0-9]+$', value): value = float(value) rules[name] = value return rules
def isprime_regex(n): """isprime_regex(n) -> True|False Astonishingly, you can test whether a number is prime using a regex. It goes without saying that this is not efficient, and should be treated as a novelty rather than a serious implementation. It is O(N^2) in time and O(N) in memory: in other words, slow and expensive. """ _validate_int(n) return not _re_match(r'^1?$|^(11+?)\1+$', '1' * n)
def isprime_regex(n): """isprime_regex(n) -> True|False Astonishingly, you can test whether a number is prime using a regex. It goes without saying that this is not efficient, and should be treated as a novelty rather than a serious implementation. It is O(N^2) in time and O(N) in memory: in other words, slow and expensive. """ _validate_int(n) return not _re_match(r'^1?$|^(11+?)\1+$', '1'*n)
def isprime_regex(n): """Slow primality test using a regular expression. >>> Awful.isprime_regex(11) True >>> Awful.isprime_regex(15) False Unsurprisingly, this is not efficient, and should be treated as a novelty rather than a serious implementation. It is O(N^2) in time and O(N) in memory: in other words, slow and expensive. """ _validate_int(n) return not _re_match(r'^1?$|^(11+?)\1+$', '1'*n)
def _is_aims_volume(obj): return _re_match(r".*soma.aims.Volume.*", str(type(obj))) or _re_match( r".*soma.aims.AimsData.*", str(type(obj)))
def ensure_the_value_file_doesnt_look_scary(): if _re_match(r'\S+\Z', value_path): return _whine_about_how_value_file_looks_scary(listener, value_path) raise stop()
def _parse_the_one_line(line, path, hence, listener): md = _re_match(r'editable_readme_path: (\S.+)$', line) if md: return md[1] _whine_about_how_line_doesnt_look_right(listener, line, path, hence)