id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
7,900
chrippa/python-librtmp
librtmp/rtmp.py
RTMP.handle_packet
def handle_packet(self, packet): """Lets librtmp look at a packet and send a response if needed.""" if not isinstance(packet, RTMPPacket): raise ValueError("A RTMPPacket argument is required") return librtmp.RTMP_ClientPacket(self.rtmp, packet.packet)
python
def handle_packet(self, packet): if not isinstance(packet, RTMPPacket): raise ValueError("A RTMPPacket argument is required") return librtmp.RTMP_ClientPacket(self.rtmp, packet.packet)
[ "def", "handle_packet", "(", "self", ",", "packet", ")", ":", "if", "not", "isinstance", "(", "packet", ",", "RTMPPacket", ")", ":", "raise", "ValueError", "(", "\"A RTMPPacket argument is required\"", ")", "return", "librtmp", ".", "RTMP_ClientPacket", "(", "se...
Lets librtmp look at a packet and send a response if needed.
[ "Lets", "librtmp", "look", "at", "a", "packet", "and", "send", "a", "response", "if", "needed", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L288-L295
7,901
chrippa/python-librtmp
librtmp/rtmp.py
RTMP.process_packets
def process_packets(self, transaction_id=None, invoked_method=None, timeout=None): """Wait for packets and process them as needed. :param transaction_id: int, Wait until the result of this transaction ID is recieved. :param invoked_method: int, Wait until this method is invoked by the server. :param timeout: int, The time to wait for a result from the server. Note: This is the timeout used by this method only, the connection timeout is still used when reading packets. Raises :exc:`RTMPError` on error. Raises :exc:`RTMPTimeoutError` on timeout. Usage:: >>> @conn.invoke_handler ... def add(x, y): ... return x + y >>> @conn.process_packets() """ start = time() while self.connected and transaction_id not in self._invoke_results: if timeout and (time() - start) >= timeout: raise RTMPTimeoutError("Timeout") packet = self.read_packet() if packet.type == PACKET_TYPE_INVOKE: try: decoded = decode_amf(packet.body) except AMFError: continue try: method, transaction_id_, obj = decoded[:3] args = decoded[3:] except ValueError: continue if method == "_result": if len(args) > 0: result = args[0] else: result = None self._invoke_results[transaction_id_] = result else: handler = self._invoke_handlers.get(method) if handler: res = handler(*args) if res is not None: self.call("_result", res, transaction_id=transaction_id_) if method == invoked_method: self._invoke_args[invoked_method] = args break if transaction_id_ == 1.0: self._connect_result = packet else: self.handle_packet(packet) else: self.handle_packet(packet) if transaction_id: result = self._invoke_results.pop(transaction_id, None) return result if invoked_method: args = self._invoke_args.pop(invoked_method, None) return args
python
def process_packets(self, transaction_id=None, invoked_method=None, timeout=None): start = time() while self.connected and transaction_id not in self._invoke_results: if timeout and (time() - start) >= timeout: raise RTMPTimeoutError("Timeout") packet = self.read_packet() if packet.type == PACKET_TYPE_INVOKE: try: decoded = decode_amf(packet.body) except AMFError: continue try: method, transaction_id_, obj = decoded[:3] args = decoded[3:] except ValueError: continue if method == "_result": if len(args) > 0: result = args[0] else: result = None self._invoke_results[transaction_id_] = result else: handler = self._invoke_handlers.get(method) if handler: res = handler(*args) if res is not None: self.call("_result", res, transaction_id=transaction_id_) if method == invoked_method: self._invoke_args[invoked_method] = args break if transaction_id_ == 1.0: self._connect_result = packet else: self.handle_packet(packet) else: self.handle_packet(packet) if transaction_id: result = self._invoke_results.pop(transaction_id, None) return result if invoked_method: args = self._invoke_args.pop(invoked_method, None) return args
[ "def", "process_packets", "(", "self", ",", "transaction_id", "=", "None", ",", "invoked_method", "=", "None", ",", "timeout", "=", "None", ")", ":", "start", "=", "time", "(", ")", "while", "self", ".", "connected", "and", "transaction_id", "not", "in", ...
Wait for packets and process them as needed. :param transaction_id: int, Wait until the result of this transaction ID is recieved. :param invoked_method: int, Wait until this method is invoked by the server. :param timeout: int, The time to wait for a result from the server. Note: This is the timeout used by this method only, the connection timeout is still used when reading packets. Raises :exc:`RTMPError` on error. Raises :exc:`RTMPTimeoutError` on timeout. Usage:: >>> @conn.invoke_handler ... def add(x, y): ... return x + y >>> @conn.process_packets()
[ "Wait", "for", "packets", "and", "process", "them", "as", "needed", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L297-L377
7,902
chrippa/python-librtmp
librtmp/rtmp.py
RTMP.call
def call(self, method, *args, **params): """Calls a method on the server.""" transaction_id = params.get("transaction_id") if not transaction_id: self.transaction_id += 1 transaction_id = self.transaction_id obj = params.get("obj") args = [method, transaction_id, obj] + list(args) args_encoded = map(lambda x: encode_amf(x), args) body = b"".join(args_encoded) format = params.get("format", PACKET_SIZE_MEDIUM) channel = params.get("channel", 0x03) packet = RTMPPacket(type=PACKET_TYPE_INVOKE, format=format, channel=channel, body=body) self.send_packet(packet) return RTMPCall(self, transaction_id)
python
def call(self, method, *args, **params): transaction_id = params.get("transaction_id") if not transaction_id: self.transaction_id += 1 transaction_id = self.transaction_id obj = params.get("obj") args = [method, transaction_id, obj] + list(args) args_encoded = map(lambda x: encode_amf(x), args) body = b"".join(args_encoded) format = params.get("format", PACKET_SIZE_MEDIUM) channel = params.get("channel", 0x03) packet = RTMPPacket(type=PACKET_TYPE_INVOKE, format=format, channel=channel, body=body) self.send_packet(packet) return RTMPCall(self, transaction_id)
[ "def", "call", "(", "self", ",", "method", ",", "*", "args", ",", "*", "*", "params", ")", ":", "transaction_id", "=", "params", ".", "get", "(", "\"transaction_id\"", ")", "if", "not", "transaction_id", ":", "self", ".", "transaction_id", "+=", "1", "...
Calls a method on the server.
[ "Calls", "a", "method", "on", "the", "server", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L379-L403
7,903
chrippa/python-librtmp
librtmp/rtmp.py
RTMP.remote_method
def remote_method(self, method, block=False, **params): """Creates a Python function that will attempt to call a remote method when used. :param method: str, Method name on the server to call :param block: bool, Wheter to wait for result or not Usage:: >>> send_usher_token = conn.remote_method("NetStream.Authenticate.UsherToken", block=True) >>> send_usher_token("some token") 'Token Accepted' """ def func(*args): call = self.call(method, *args, **params) if block: return call.result() return call func.__name__ = method return func
python
def remote_method(self, method, block=False, **params): def func(*args): call = self.call(method, *args, **params) if block: return call.result() return call func.__name__ = method return func
[ "def", "remote_method", "(", "self", ",", "method", ",", "block", "=", "False", ",", "*", "*", "params", ")", ":", "def", "func", "(", "*", "args", ")", ":", "call", "=", "self", ".", "call", "(", "method", ",", "*", "args", ",", "*", "*", "par...
Creates a Python function that will attempt to call a remote method when used. :param method: str, Method name on the server to call :param block: bool, Wheter to wait for result or not Usage:: >>> send_usher_token = conn.remote_method("NetStream.Authenticate.UsherToken", block=True) >>> send_usher_token("some token") 'Token Accepted'
[ "Creates", "a", "Python", "function", "that", "will", "attempt", "to", "call", "a", "remote", "method", "when", "used", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L405-L429
7,904
chrippa/python-librtmp
librtmp/rtmp.py
RTMPCall.result
def result(self, timeout=None): """Retrieves the result of the call. :param timeout: The time to wait for a result from the server. Raises :exc:`RTMPTimeoutError` on timeout. """ if self.done: return self._result result = self.conn.process_packets(transaction_id=self.transaction_id, timeout=timeout) self._result = result self.done = True return result
python
def result(self, timeout=None): if self.done: return self._result result = self.conn.process_packets(transaction_id=self.transaction_id, timeout=timeout) self._result = result self.done = True return result
[ "def", "result", "(", "self", ",", "timeout", "=", "None", ")", ":", "if", "self", ".", "done", ":", "return", "self", ".", "_result", "result", "=", "self", ".", "conn", ".", "process_packets", "(", "transaction_id", "=", "self", ".", "transaction_id", ...
Retrieves the result of the call. :param timeout: The time to wait for a result from the server. Raises :exc:`RTMPTimeoutError` on timeout.
[ "Retrieves", "the", "result", "of", "the", "call", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L473-L489
7,905
chrippa/python-librtmp
librtmp/utils.py
add_signal_handler
def add_signal_handler(): """Adds a signal handler to handle KeyboardInterrupt.""" import signal def handler(sig, frame): if sig == signal.SIGINT: librtmp.RTMP_UserInterrupt() raise KeyboardInterrupt signal.signal(signal.SIGINT, handler)
python
def add_signal_handler(): import signal def handler(sig, frame): if sig == signal.SIGINT: librtmp.RTMP_UserInterrupt() raise KeyboardInterrupt signal.signal(signal.SIGINT, handler)
[ "def", "add_signal_handler", "(", ")", ":", "import", "signal", "def", "handler", "(", "sig", ",", "frame", ")", ":", "if", "sig", "==", "signal", ".", "SIGINT", ":", "librtmp", ".", "RTMP_UserInterrupt", "(", ")", "raise", "KeyboardInterrupt", "signal", "...
Adds a signal handler to handle KeyboardInterrupt.
[ "Adds", "a", "signal", "handler", "to", "handle", "KeyboardInterrupt", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/utils.py#L12-L21
7,906
taxpon/pymesh
pymesh/base.py
BaseMesh.rotate_x
def rotate_x(self, deg): """Rotate mesh around x-axis :param float deg: Rotation angle (degree) :return: """ rad = math.radians(deg) mat = numpy.array([ [1, 0, 0, 0], [0, math.cos(rad), math.sin(rad), 0], [0, -math.sin(rad), math.cos(rad), 0], [0, 0, 0, 1] ]) self.vectors = self.vectors.dot(mat) return self
python
def rotate_x(self, deg): rad = math.radians(deg) mat = numpy.array([ [1, 0, 0, 0], [0, math.cos(rad), math.sin(rad), 0], [0, -math.sin(rad), math.cos(rad), 0], [0, 0, 0, 1] ]) self.vectors = self.vectors.dot(mat) return self
[ "def", "rotate_x", "(", "self", ",", "deg", ")", ":", "rad", "=", "math", ".", "radians", "(", "deg", ")", "mat", "=", "numpy", ".", "array", "(", "[", "[", "1", ",", "0", ",", "0", ",", "0", "]", ",", "[", "0", ",", "math", ".", "cos", "...
Rotate mesh around x-axis :param float deg: Rotation angle (degree) :return:
[ "Rotate", "mesh", "around", "x", "-", "axis" ]
a90b3b2ed1408d793f3b5208dd8087b08fb7c92e
https://github.com/taxpon/pymesh/blob/a90b3b2ed1408d793f3b5208dd8087b08fb7c92e/pymesh/base.py#L47-L61
7,907
taxpon/pymesh
pymesh/base.py
BaseMesh.translate_x
def translate_x(self, d): """Translate mesh for x-direction :param float d: Amount to translate """ mat = numpy.array([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [d, 0, 0, 1] ]) self.vectors = self.vectors.dot(mat) return self
python
def translate_x(self, d): mat = numpy.array([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [d, 0, 0, 1] ]) self.vectors = self.vectors.dot(mat) return self
[ "def", "translate_x", "(", "self", ",", "d", ")", ":", "mat", "=", "numpy", ".", "array", "(", "[", "[", "1", ",", "0", ",", "0", ",", "0", "]", ",", "[", "0", ",", "1", ",", "0", ",", "0", "]", ",", "[", "0", ",", "0", ",", "1", ",",...
Translate mesh for x-direction :param float d: Amount to translate
[ "Translate", "mesh", "for", "x", "-", "direction" ]
a90b3b2ed1408d793f3b5208dd8087b08fb7c92e
https://github.com/taxpon/pymesh/blob/a90b3b2ed1408d793f3b5208dd8087b08fb7c92e/pymesh/base.py#L93-L105
7,908
taxpon/pymesh
pymesh/base.py
BaseMesh.translate_y
def translate_y(self, d): """Translate mesh for y-direction :param float d: Amount to translate """ mat = numpy.array([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, d, 0, 1] ]) self.vectors = self.vectors.dot(mat) return self
python
def translate_y(self, d): mat = numpy.array([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, d, 0, 1] ]) self.vectors = self.vectors.dot(mat) return self
[ "def", "translate_y", "(", "self", ",", "d", ")", ":", "mat", "=", "numpy", ".", "array", "(", "[", "[", "1", ",", "0", ",", "0", ",", "0", "]", ",", "[", "0", ",", "1", ",", "0", ",", "0", "]", ",", "[", "0", ",", "0", ",", "1", ",",...
Translate mesh for y-direction :param float d: Amount to translate
[ "Translate", "mesh", "for", "y", "-", "direction" ]
a90b3b2ed1408d793f3b5208dd8087b08fb7c92e
https://github.com/taxpon/pymesh/blob/a90b3b2ed1408d793f3b5208dd8087b08fb7c92e/pymesh/base.py#L107-L119
7,909
taxpon/pymesh
pymesh/base.py
BaseMesh.translate_z
def translate_z(self, d): """Translate mesh for z-direction :param float d: Amount to translate """ mat = numpy.array([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, d, 1] ]) self.vectors = self.vectors.dot(mat) return self
python
def translate_z(self, d): mat = numpy.array([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, d, 1] ]) self.vectors = self.vectors.dot(mat) return self
[ "def", "translate_z", "(", "self", ",", "d", ")", ":", "mat", "=", "numpy", ".", "array", "(", "[", "[", "1", ",", "0", ",", "0", ",", "0", "]", ",", "[", "0", ",", "1", ",", "0", ",", "0", "]", ",", "[", "0", ",", "0", ",", "1", ",",...
Translate mesh for z-direction :param float d: Amount to translate
[ "Translate", "mesh", "for", "z", "-", "direction" ]
a90b3b2ed1408d793f3b5208dd8087b08fb7c92e
https://github.com/taxpon/pymesh/blob/a90b3b2ed1408d793f3b5208dd8087b08fb7c92e/pymesh/base.py#L121-L133
7,910
taxpon/pymesh
pymesh/stl.py
Stl.__load
def __load(fh, mode=MODE_AUTO): """Load Mesh from STL file :param FileIO fh: The file handle to open :param int mode: The mode to open, default is :py:data:`AUTOMATIC`. :return: """ header = fh.read(Stl.HEADER_SIZE).lower() name = "" data = None if not header.strip(): return if mode in (Stl.MODE_AUTO, Stl.MODE_ASCII) and header.startswith('solid'): try: name = header.split('\n', 1)[0][:5].strip() data = Stl.__load_ascii(fh, header) mode = Stl.MODE_ASCII except: pass else: data = Stl.__load_binary(fh) mode = Stl.MODE_BINARY return name, data, mode
python
def __load(fh, mode=MODE_AUTO): header = fh.read(Stl.HEADER_SIZE).lower() name = "" data = None if not header.strip(): return if mode in (Stl.MODE_AUTO, Stl.MODE_ASCII) and header.startswith('solid'): try: name = header.split('\n', 1)[0][:5].strip() data = Stl.__load_ascii(fh, header) mode = Stl.MODE_ASCII except: pass else: data = Stl.__load_binary(fh) mode = Stl.MODE_BINARY return name, data, mode
[ "def", "__load", "(", "fh", ",", "mode", "=", "MODE_AUTO", ")", ":", "header", "=", "fh", ".", "read", "(", "Stl", ".", "HEADER_SIZE", ")", ".", "lower", "(", ")", "name", "=", "\"\"", "data", "=", "None", "if", "not", "header", ".", "strip", "("...
Load Mesh from STL file :param FileIO fh: The file handle to open :param int mode: The mode to open, default is :py:data:`AUTOMATIC`. :return:
[ "Load", "Mesh", "from", "STL", "file" ]
a90b3b2ed1408d793f3b5208dd8087b08fb7c92e
https://github.com/taxpon/pymesh/blob/a90b3b2ed1408d793f3b5208dd8087b08fb7c92e/pymesh/stl.py#L52-L78
7,911
neptune-ml/steppy
steppy/utils.py
initialize_logger
def initialize_logger(): """Initialize steppy logger. This logger is used throughout the steppy library to report computation progress. Example: Simple use of steppy logger: .. code-block:: python initialize_logger() logger = get_logger() logger.info('My message inside pipeline') result looks like this: .. code:: 2018-06-02 12:33:48 steppy >>> My message inside pipeline Returns: logging.Logger: logger object formatted in the steppy style """ logger = logging.getLogger('steppy') logger.setLevel(logging.INFO) message_format = logging.Formatter(fmt='%(asctime)s %(name)s >>> %(message)s', datefmt='%Y-%m-%d %H:%M:%S') # console handler console_handler = logging.StreamHandler(sys.stdout) console_handler.setLevel(logging.INFO) console_handler.setFormatter(fmt=message_format) # add the handlers to the logger logger.addHandler(console_handler) return logger
python
def initialize_logger(): logger = logging.getLogger('steppy') logger.setLevel(logging.INFO) message_format = logging.Formatter(fmt='%(asctime)s %(name)s >>> %(message)s', datefmt='%Y-%m-%d %H:%M:%S') # console handler console_handler = logging.StreamHandler(sys.stdout) console_handler.setLevel(logging.INFO) console_handler.setFormatter(fmt=message_format) # add the handlers to the logger logger.addHandler(console_handler) return logger
[ "def", "initialize_logger", "(", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "'steppy'", ")", "logger", ".", "setLevel", "(", "logging", ".", "INFO", ")", "message_format", "=", "logging", ".", "Formatter", "(", "fmt", "=", "'%(asctime)s %(nam...
Initialize steppy logger. This logger is used throughout the steppy library to report computation progress. Example: Simple use of steppy logger: .. code-block:: python initialize_logger() logger = get_logger() logger.info('My message inside pipeline') result looks like this: .. code:: 2018-06-02 12:33:48 steppy >>> My message inside pipeline Returns: logging.Logger: logger object formatted in the steppy style
[ "Initialize", "steppy", "logger", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/utils.py#L8-L45
7,912
neptune-ml/steppy
steppy/utils.py
display_upstream_structure
def display_upstream_structure(structure_dict): """Displays pipeline structure in the jupyter notebook. Args: structure_dict (dict): dict returned by :func:`~steppy.base.Step.upstream_structure`. """ graph = _create_graph(structure_dict) plt = Image(graph.create_png()) display(plt)
python
def display_upstream_structure(structure_dict): graph = _create_graph(structure_dict) plt = Image(graph.create_png()) display(plt)
[ "def", "display_upstream_structure", "(", "structure_dict", ")", ":", "graph", "=", "_create_graph", "(", "structure_dict", ")", "plt", "=", "Image", "(", "graph", ".", "create_png", "(", ")", ")", "display", "(", "plt", ")" ]
Displays pipeline structure in the jupyter notebook. Args: structure_dict (dict): dict returned by :func:`~steppy.base.Step.upstream_structure`.
[ "Displays", "pipeline", "structure", "in", "the", "jupyter", "notebook", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/utils.py#L71-L80
7,913
neptune-ml/steppy
steppy/utils.py
persist_as_png
def persist_as_png(structure_dict, filepath): """Saves pipeline diagram to disk as png file. Args: structure_dict (dict): dict returned by :func:`~steppy.base.Step.upstream_structure` filepath (str): filepath to which the png with pipeline visualization should be persisted """ graph = _create_graph(structure_dict) graph.write(filepath, format='png')
python
def persist_as_png(structure_dict, filepath): graph = _create_graph(structure_dict) graph.write(filepath, format='png')
[ "def", "persist_as_png", "(", "structure_dict", ",", "filepath", ")", ":", "graph", "=", "_create_graph", "(", "structure_dict", ")", "graph", ".", "write", "(", "filepath", ",", "format", "=", "'png'", ")" ]
Saves pipeline diagram to disk as png file. Args: structure_dict (dict): dict returned by :func:`~steppy.base.Step.upstream_structure` filepath (str): filepath to which the png with pipeline visualization should be persisted
[ "Saves", "pipeline", "diagram", "to", "disk", "as", "png", "file", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/utils.py#L83-L92
7,914
neptune-ml/steppy
steppy/utils.py
_create_graph
def _create_graph(structure_dict): """Creates pydot graph from the pipeline structure dict. Args: structure_dict (dict): dict returned by step.upstream_structure Returns: graph (pydot.Dot): object representing upstream pipeline structure (with regard to the current Step). """ graph = pydot.Dot() for node in structure_dict['nodes']: graph.add_node(pydot.Node(node)) for node1, node2 in structure_dict['edges']: graph.add_edge(pydot.Edge(node1, node2)) return graph
python
def _create_graph(structure_dict): graph = pydot.Dot() for node in structure_dict['nodes']: graph.add_node(pydot.Node(node)) for node1, node2 in structure_dict['edges']: graph.add_edge(pydot.Edge(node1, node2)) return graph
[ "def", "_create_graph", "(", "structure_dict", ")", ":", "graph", "=", "pydot", ".", "Dot", "(", ")", "for", "node", "in", "structure_dict", "[", "'nodes'", "]", ":", "graph", ".", "add_node", "(", "pydot", ".", "Node", "(", "node", ")", ")", "for", ...
Creates pydot graph from the pipeline structure dict. Args: structure_dict (dict): dict returned by step.upstream_structure Returns: graph (pydot.Dot): object representing upstream pipeline structure (with regard to the current Step).
[ "Creates", "pydot", "graph", "from", "the", "pipeline", "structure", "dict", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/utils.py#L95-L109
7,915
neptune-ml/steppy
steppy/adapter.py
Adapter.adapt
def adapt(self, all_ouputs: AllOutputs) -> DataPacket: """Adapt inputs for the transformer included in the step. Args: all_ouputs: Dict of outputs from parent steps. The keys should match the names of these steps and the values should be their respective outputs. Returns: Dictionary with the same keys as `adapting_recipes` and values constructed according to the respective recipes. """ adapted = {} for name, recipe in self.adapting_recipes.items(): adapted[name] = self._construct(all_ouputs, recipe) return adapted
python
def adapt(self, all_ouputs: AllOutputs) -> DataPacket: adapted = {} for name, recipe in self.adapting_recipes.items(): adapted[name] = self._construct(all_ouputs, recipe) return adapted
[ "def", "adapt", "(", "self", ",", "all_ouputs", ":", "AllOutputs", ")", "->", "DataPacket", ":", "adapted", "=", "{", "}", "for", "name", ",", "recipe", "in", "self", ".", "adapting_recipes", ".", "items", "(", ")", ":", "adapted", "[", "name", "]", ...
Adapt inputs for the transformer included in the step. Args: all_ouputs: Dict of outputs from parent steps. The keys should match the names of these steps and the values should be their respective outputs. Returns: Dictionary with the same keys as `adapting_recipes` and values constructed according to the respective recipes.
[ "Adapt", "inputs", "for", "the", "transformer", "included", "in", "the", "step", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/adapter.py#L106-L122
7,916
neptune-ml/steppy
steppy/base.py
Step.fit_transform
def fit_transform(self, data): """Fit the model and transform data or load already processed data. Loads cached or persisted output or adapts data for the current transformer and executes ``transformer.fit_transform``. Args: data (dict): data dictionary with keys as input names and values as dictionaries of key-value pairs that can be passed to the ``self.transformer.fit_transform`` method. Example: .. code-block:: python data = {'input_1': {'X': X, 'y': y}, 'input_2': {'X': X, 'y': y} } Returns: dict: Step output from the ``self.transformer.fit_transform`` method """ if data: assert isinstance(data, dict), 'Step {}, "data" argument in the "fit_transform()" method must be dict, ' \ 'got {} instead.'.format(self.name, type(data)) logger.info('Step {}, working in "{}" mode'.format(self.name, self._mode)) if self._mode == 'inference': ValueError('Step {}, you are in "{}" mode, where you cannot run "fit".' 'Please change mode to "train" to enable fitting.' 'Use: "step.set_mode_train()" then "step.fit_transform()"'.format(self.name, self._mode)) if self.output_is_cached and not self.force_fitting: logger.info('Step {} using cached output'.format(self.name)) step_output_data = self.output elif self.output_is_persisted and self.load_persisted_output and not self.force_fitting: logger.info('Step {} loading persisted output from {}'.format(self.name, self.experiment_directory_output_step)) step_output_data = self._load_output(self.experiment_directory_output_step) else: step_inputs = {} if self.input_data is not None: for input_data_part in self.input_data: step_inputs[input_data_part] = data[input_data_part] for input_step in self.input_steps: step_inputs[input_step.name] = input_step.fit_transform(data) if self.adapter: step_inputs = self._adapt(step_inputs) else: step_inputs = self._unpack(step_inputs) step_output_data = self._fit_transform_operation(step_inputs) logger.info('Step {}, fit and transform completed'.format(self.name)) return step_output_data
python
def fit_transform(self, data): if data: assert isinstance(data, dict), 'Step {}, "data" argument in the "fit_transform()" method must be dict, ' \ 'got {} instead.'.format(self.name, type(data)) logger.info('Step {}, working in "{}" mode'.format(self.name, self._mode)) if self._mode == 'inference': ValueError('Step {}, you are in "{}" mode, where you cannot run "fit".' 'Please change mode to "train" to enable fitting.' 'Use: "step.set_mode_train()" then "step.fit_transform()"'.format(self.name, self._mode)) if self.output_is_cached and not self.force_fitting: logger.info('Step {} using cached output'.format(self.name)) step_output_data = self.output elif self.output_is_persisted and self.load_persisted_output and not self.force_fitting: logger.info('Step {} loading persisted output from {}'.format(self.name, self.experiment_directory_output_step)) step_output_data = self._load_output(self.experiment_directory_output_step) else: step_inputs = {} if self.input_data is not None: for input_data_part in self.input_data: step_inputs[input_data_part] = data[input_data_part] for input_step in self.input_steps: step_inputs[input_step.name] = input_step.fit_transform(data) if self.adapter: step_inputs = self._adapt(step_inputs) else: step_inputs = self._unpack(step_inputs) step_output_data = self._fit_transform_operation(step_inputs) logger.info('Step {}, fit and transform completed'.format(self.name)) return step_output_data
[ "def", "fit_transform", "(", "self", ",", "data", ")", ":", "if", "data", ":", "assert", "isinstance", "(", "data", ",", "dict", ")", ",", "'Step {}, \"data\" argument in the \"fit_transform()\" method must be dict, '", "'got {} instead.'", ".", "format", "(", "self",...
Fit the model and transform data or load already processed data. Loads cached or persisted output or adapts data for the current transformer and executes ``transformer.fit_transform``. Args: data (dict): data dictionary with keys as input names and values as dictionaries of key-value pairs that can be passed to the ``self.transformer.fit_transform`` method. Example: .. code-block:: python data = {'input_1': {'X': X, 'y': y}, 'input_2': {'X': X, 'y': y} } Returns: dict: Step output from the ``self.transformer.fit_transform`` method
[ "Fit", "the", "model", "and", "transform", "data", "or", "load", "already", "processed", "data", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/base.py#L310-L364
7,917
neptune-ml/steppy
steppy/base.py
Step.clean_cache_step
def clean_cache_step(self): """Clean cache for current step. """ logger.info('Step {}, cleaning cache'.format(self.name)) self.output = None return self
python
def clean_cache_step(self): logger.info('Step {}, cleaning cache'.format(self.name)) self.output = None return self
[ "def", "clean_cache_step", "(", "self", ")", ":", "logger", ".", "info", "(", "'Step {}, cleaning cache'", ".", "format", "(", "self", ".", "name", ")", ")", "self", ".", "output", "=", "None", "return", "self" ]
Clean cache for current step.
[ "Clean", "cache", "for", "current", "step", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/base.py#L471-L476
7,918
neptune-ml/steppy
steppy/base.py
Step.clean_cache_upstream
def clean_cache_upstream(self): """Clean cache for all steps that are upstream to `self`. """ logger.info('Cleaning cache for the entire upstream pipeline') for step in self.all_upstream_steps.values(): logger.info('Step {}, cleaning cache'.format(step.name)) step.output = None return self
python
def clean_cache_upstream(self): logger.info('Cleaning cache for the entire upstream pipeline') for step in self.all_upstream_steps.values(): logger.info('Step {}, cleaning cache'.format(step.name)) step.output = None return self
[ "def", "clean_cache_upstream", "(", "self", ")", ":", "logger", ".", "info", "(", "'Cleaning cache for the entire upstream pipeline'", ")", "for", "step", "in", "self", ".", "all_upstream_steps", ".", "values", "(", ")", ":", "logger", ".", "info", "(", "'Step {...
Clean cache for all steps that are upstream to `self`.
[ "Clean", "cache", "for", "all", "steps", "that", "are", "upstream", "to", "self", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/base.py#L478-L485
7,919
neptune-ml/steppy
steppy/base.py
Step.get_step_by_name
def get_step_by_name(self, name): """Extracts step by name from the pipeline. Extracted Step is a fully functional pipeline as well. All upstream Steps are already defined. Args: name (str): name of the step to be fetched Returns: Step (obj): extracted step """ self._validate_step_name(name) name = str(name) try: return self.all_upstream_steps[name] except KeyError as e: msg = 'No Step with name "{}" found. ' \ 'You have following Steps: {}'.format(name, list(self.all_upstream_steps.keys())) raise StepError(msg) from e
python
def get_step_by_name(self, name): self._validate_step_name(name) name = str(name) try: return self.all_upstream_steps[name] except KeyError as e: msg = 'No Step with name "{}" found. ' \ 'You have following Steps: {}'.format(name, list(self.all_upstream_steps.keys())) raise StepError(msg) from e
[ "def", "get_step_by_name", "(", "self", ",", "name", ")", ":", "self", ".", "_validate_step_name", "(", "name", ")", "name", "=", "str", "(", "name", ")", "try", ":", "return", "self", ".", "all_upstream_steps", "[", "name", "]", "except", "KeyError", "a...
Extracts step by name from the pipeline. Extracted Step is a fully functional pipeline as well. All upstream Steps are already defined. Args: name (str): name of the step to be fetched Returns: Step (obj): extracted step
[ "Extracts", "step", "by", "name", "from", "the", "pipeline", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/base.py#L487-L505
7,920
neptune-ml/steppy
steppy/base.py
Step.persist_upstream_structure
def persist_upstream_structure(self): """Persist json file with the upstream steps structure, that is step names and their connections.""" persist_dir = os.path.join(self.experiment_directory, '{}_upstream_structure.json'.format(self.name)) logger.info('Step {}, saving upstream pipeline structure to {}'.format(self.name, persist_dir)) joblib.dump(self.upstream_structure, persist_dir)
python
def persist_upstream_structure(self): persist_dir = os.path.join(self.experiment_directory, '{}_upstream_structure.json'.format(self.name)) logger.info('Step {}, saving upstream pipeline structure to {}'.format(self.name, persist_dir)) joblib.dump(self.upstream_structure, persist_dir)
[ "def", "persist_upstream_structure", "(", "self", ")", ":", "persist_dir", "=", "os", ".", "path", ".", "join", "(", "self", ".", "experiment_directory", ",", "'{}_upstream_structure.json'", ".", "format", "(", "self", ".", "name", ")", ")", "logger", ".", "...
Persist json file with the upstream steps structure, that is step names and their connections.
[ "Persist", "json", "file", "with", "the", "upstream", "steps", "structure", "that", "is", "step", "names", "and", "their", "connections", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/base.py#L507-L511
7,921
neptune-ml/steppy
steppy/base.py
Step.persist_upstream_diagram
def persist_upstream_diagram(self, filepath): """Creates upstream steps diagram and persists it to disk as png file. Pydot graph is created and persisted to disk as png file under the filepath directory. Args: filepath (str): filepath to which the png with steps visualization should be persisted """ assert isinstance(filepath, str),\ 'Step {} error, filepath must be str. Got {} instead'.format(self.name, type(filepath)) persist_as_png(self.upstream_structure, filepath)
python
def persist_upstream_diagram(self, filepath): assert isinstance(filepath, str),\ 'Step {} error, filepath must be str. Got {} instead'.format(self.name, type(filepath)) persist_as_png(self.upstream_structure, filepath)
[ "def", "persist_upstream_diagram", "(", "self", ",", "filepath", ")", ":", "assert", "isinstance", "(", "filepath", ",", "str", ")", ",", "'Step {} error, filepath must be str. Got {} instead'", ".", "format", "(", "self", ".", "name", ",", "type", "(", "filepath"...
Creates upstream steps diagram and persists it to disk as png file. Pydot graph is created and persisted to disk as png file under the filepath directory. Args: filepath (str): filepath to which the png with steps visualization should be persisted
[ "Creates", "upstream", "steps", "diagram", "and", "persists", "it", "to", "disk", "as", "png", "file", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/base.py#L513-L524
7,922
neptune-ml/steppy
steppy/base.py
BaseTransformer.fit_transform
def fit_transform(self, *args, **kwargs): """Performs fit followed by transform. This method simply combines fit and transform. Args: args: positional arguments (can be anything) kwargs: keyword arguments (can be anything) Returns: dict: output """ self.fit(*args, **kwargs) return self.transform(*args, **kwargs)
python
def fit_transform(self, *args, **kwargs): self.fit(*args, **kwargs) return self.transform(*args, **kwargs)
[ "def", "fit_transform", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "self", ".", "fit", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "self", ".", "transform", "(", "*", "args", ",", "*", "*", "kwargs", ")" ]
Performs fit followed by transform. This method simply combines fit and transform. Args: args: positional arguments (can be anything) kwargs: keyword arguments (can be anything) Returns: dict: output
[ "Performs", "fit", "followed", "by", "transform", "." ]
856b95f1f5189e1d2ca122b891bc670adac9692b
https://github.com/neptune-ml/steppy/blob/856b95f1f5189e1d2ca122b891bc670adac9692b/steppy/base.py#L773-L786
7,923
lepture/otpauth
otpauth.py
generate_hotp
def generate_hotp(secret, counter=4): """Generate a HOTP code. :param secret: A secret token for the authentication. :param counter: HOTP is a counter based algorithm. """ # https://tools.ietf.org/html/rfc4226 msg = struct.pack('>Q', counter) digest = hmac.new(to_bytes(secret), msg, hashlib.sha1).digest() ob = digest[19] if PY2: ob = ord(ob) pos = ob & 15 base = struct.unpack('>I', digest[pos:pos + 4])[0] & 0x7fffffff token = base % 1000000 return token
python
def generate_hotp(secret, counter=4): # https://tools.ietf.org/html/rfc4226 msg = struct.pack('>Q', counter) digest = hmac.new(to_bytes(secret), msg, hashlib.sha1).digest() ob = digest[19] if PY2: ob = ord(ob) pos = ob & 15 base = struct.unpack('>I', digest[pos:pos + 4])[0] & 0x7fffffff token = base % 1000000 return token
[ "def", "generate_hotp", "(", "secret", ",", "counter", "=", "4", ")", ":", "# https://tools.ietf.org/html/rfc4226", "msg", "=", "struct", ".", "pack", "(", "'>Q'", ",", "counter", ")", "digest", "=", "hmac", ".", "new", "(", "to_bytes", "(", "secret", ")",...
Generate a HOTP code. :param secret: A secret token for the authentication. :param counter: HOTP is a counter based algorithm.
[ "Generate", "a", "HOTP", "code", "." ]
49914d83d36dbcd33c9e26f65002b21ce09a6303
https://github.com/lepture/otpauth/blob/49914d83d36dbcd33c9e26f65002b21ce09a6303/otpauth.py#L143-L160
7,924
lepture/otpauth
otpauth.py
generate_totp
def generate_totp(secret, period=30, timestamp=None): """Generate a TOTP code. A TOTP code is an extension of HOTP algorithm. :param secret: A secret token for the authentication. :param period: A period that a TOTP code is valid in seconds :param timestamp: Current time stamp. """ if timestamp is None: timestamp = time.time() counter = int(timestamp) // period return generate_hotp(secret, counter)
python
def generate_totp(secret, period=30, timestamp=None): if timestamp is None: timestamp = time.time() counter = int(timestamp) // period return generate_hotp(secret, counter)
[ "def", "generate_totp", "(", "secret", ",", "period", "=", "30", ",", "timestamp", "=", "None", ")", ":", "if", "timestamp", "is", "None", ":", "timestamp", "=", "time", ".", "time", "(", ")", "counter", "=", "int", "(", "timestamp", ")", "//", "peri...
Generate a TOTP code. A TOTP code is an extension of HOTP algorithm. :param secret: A secret token for the authentication. :param period: A period that a TOTP code is valid in seconds :param timestamp: Current time stamp.
[ "Generate", "a", "TOTP", "code", "." ]
49914d83d36dbcd33c9e26f65002b21ce09a6303
https://github.com/lepture/otpauth/blob/49914d83d36dbcd33c9e26f65002b21ce09a6303/otpauth.py#L163-L175
7,925
lepture/otpauth
otpauth.py
OtpAuth.valid_hotp
def valid_hotp(self, code, last=0, trials=100): """Valid a HOTP code. :param code: A number that is less than 6 characters. :param last: Guess HOTP code from last + 1 range. :param trials: Guest HOTP code end at last + trials + 1. """ if not valid_code(code): return False code = bytes(int(code)) for i in range(last + 1, last + trials + 1): if compare_digest(bytes(self.hotp(counter=i)), code): return i return False
python
def valid_hotp(self, code, last=0, trials=100): if not valid_code(code): return False code = bytes(int(code)) for i in range(last + 1, last + trials + 1): if compare_digest(bytes(self.hotp(counter=i)), code): return i return False
[ "def", "valid_hotp", "(", "self", ",", "code", ",", "last", "=", "0", ",", "trials", "=", "100", ")", ":", "if", "not", "valid_code", "(", "code", ")", ":", "return", "False", "code", "=", "bytes", "(", "int", "(", "code", ")", ")", "for", "i", ...
Valid a HOTP code. :param code: A number that is less than 6 characters. :param last: Guess HOTP code from last + 1 range. :param trials: Guest HOTP code end at last + trials + 1.
[ "Valid", "a", "HOTP", "code", "." ]
49914d83d36dbcd33c9e26f65002b21ce09a6303
https://github.com/lepture/otpauth/blob/49914d83d36dbcd33c9e26f65002b21ce09a6303/otpauth.py#L67-L81
7,926
lepture/otpauth
otpauth.py
OtpAuth.valid_totp
def valid_totp(self, code, period=30, timestamp=None): """Valid a TOTP code. :param code: A number that is less than 6 characters. :param period: A period that a TOTP code is valid in seconds :param timestamp: Validate TOTP at this given timestamp """ if not valid_code(code): return False return compare_digest( bytes(self.totp(period, timestamp)), bytes(int(code)) )
python
def valid_totp(self, code, period=30, timestamp=None): if not valid_code(code): return False return compare_digest( bytes(self.totp(period, timestamp)), bytes(int(code)) )
[ "def", "valid_totp", "(", "self", ",", "code", ",", "period", "=", "30", ",", "timestamp", "=", "None", ")", ":", "if", "not", "valid_code", "(", "code", ")", ":", "return", "False", "return", "compare_digest", "(", "bytes", "(", "self", ".", "totp", ...
Valid a TOTP code. :param code: A number that is less than 6 characters. :param period: A period that a TOTP code is valid in seconds :param timestamp: Validate TOTP at this given timestamp
[ "Valid", "a", "TOTP", "code", "." ]
49914d83d36dbcd33c9e26f65002b21ce09a6303
https://github.com/lepture/otpauth/blob/49914d83d36dbcd33c9e26f65002b21ce09a6303/otpauth.py#L83-L95
7,927
lepture/otpauth
otpauth.py
OtpAuth.to_uri
def to_uri(self, type, label, issuer, counter=None): """Generate the otpauth protocal string. :param type: Algorithm type, hotp or totp. :param label: Label of the identifier. :param issuer: The company, the organization or something else. :param counter: Counter of the HOTP algorithm. """ type = type.lower() if type not in ('hotp', 'totp'): raise ValueError('type must be hotp or totp') if type == 'hotp' and not counter: raise ValueError('HOTP type authentication need counter') # https://code.google.com/p/google-authenticator/wiki/KeyUriFormat url = ('otpauth://%(type)s/%(label)s?secret=%(secret)s' '&issuer=%(issuer)s') dct = dict( type=type, label=label, issuer=issuer, secret=self.encoded_secret, counter=counter ) ret = url % dct if type == 'hotp': ret = '%s&counter=%s' % (ret, counter) return ret
python
def to_uri(self, type, label, issuer, counter=None): type = type.lower() if type not in ('hotp', 'totp'): raise ValueError('type must be hotp or totp') if type == 'hotp' and not counter: raise ValueError('HOTP type authentication need counter') # https://code.google.com/p/google-authenticator/wiki/KeyUriFormat url = ('otpauth://%(type)s/%(label)s?secret=%(secret)s' '&issuer=%(issuer)s') dct = dict( type=type, label=label, issuer=issuer, secret=self.encoded_secret, counter=counter ) ret = url % dct if type == 'hotp': ret = '%s&counter=%s' % (ret, counter) return ret
[ "def", "to_uri", "(", "self", ",", "type", ",", "label", ",", "issuer", ",", "counter", "=", "None", ")", ":", "type", "=", "type", ".", "lower", "(", ")", "if", "type", "not", "in", "(", "'hotp'", ",", "'totp'", ")", ":", "raise", "ValueError", ...
Generate the otpauth protocal string. :param type: Algorithm type, hotp or totp. :param label: Label of the identifier. :param issuer: The company, the organization or something else. :param counter: Counter of the HOTP algorithm.
[ "Generate", "the", "otpauth", "protocal", "string", "." ]
49914d83d36dbcd33c9e26f65002b21ce09a6303
https://github.com/lepture/otpauth/blob/49914d83d36dbcd33c9e26f65002b21ce09a6303/otpauth.py#L105-L131
7,928
lepture/otpauth
otpauth.py
OtpAuth.to_google
def to_google(self, type, label, issuer, counter=None): """Generate the otpauth protocal string for Google Authenticator. .. deprecated:: 0.2.0 Use :func:`to_uri` instead. """ warnings.warn('deprecated, use to_uri instead', DeprecationWarning) return self.to_uri(type, label, issuer, counter)
python
def to_google(self, type, label, issuer, counter=None): warnings.warn('deprecated, use to_uri instead', DeprecationWarning) return self.to_uri(type, label, issuer, counter)
[ "def", "to_google", "(", "self", ",", "type", ",", "label", ",", "issuer", ",", "counter", "=", "None", ")", ":", "warnings", ".", "warn", "(", "'deprecated, use to_uri instead'", ",", "DeprecationWarning", ")", "return", "self", ".", "to_uri", "(", "type", ...
Generate the otpauth protocal string for Google Authenticator. .. deprecated:: 0.2.0 Use :func:`to_uri` instead.
[ "Generate", "the", "otpauth", "protocal", "string", "for", "Google", "Authenticator", "." ]
49914d83d36dbcd33c9e26f65002b21ce09a6303
https://github.com/lepture/otpauth/blob/49914d83d36dbcd33c9e26f65002b21ce09a6303/otpauth.py#L133-L140
7,929
carlospalol/money
money/exchange.py
ExchangeRates.install
def install(self, backend='money.exchange.SimpleBackend'): """Install an exchange rates backend using a python path string""" # RADAR: Python2 if isinstance(backend, money.six.string_types): path, name = backend.rsplit('.', 1) module = importlib.import_module(path) backend = getattr(module, name)() elif isinstance(backend, type): backend = backend() if not isinstance(backend, BackendBase): raise TypeError("backend '{}' is not a subclass of " "money.xrates.BackendBase".format(backend)) self._backend = backend
python
def install(self, backend='money.exchange.SimpleBackend'): # RADAR: Python2 if isinstance(backend, money.six.string_types): path, name = backend.rsplit('.', 1) module = importlib.import_module(path) backend = getattr(module, name)() elif isinstance(backend, type): backend = backend() if not isinstance(backend, BackendBase): raise TypeError("backend '{}' is not a subclass of " "money.xrates.BackendBase".format(backend)) self._backend = backend
[ "def", "install", "(", "self", ",", "backend", "=", "'money.exchange.SimpleBackend'", ")", ":", "# RADAR: Python2", "if", "isinstance", "(", "backend", ",", "money", ".", "six", ".", "string_types", ")", ":", "path", ",", "name", "=", "backend", ".", "rsplit...
Install an exchange rates backend using a python path string
[ "Install", "an", "exchange", "rates", "backend", "using", "a", "python", "path", "string" ]
1e51f651f93edd62c16eb3d7aa034fec03096046
https://github.com/carlospalol/money/blob/1e51f651f93edd62c16eb3d7aa034fec03096046/money/exchange.py#L81-L93
7,930
carlospalol/money
money/exchange.py
ExchangeRates.rate
def rate(self, currency): """Return quotation between the base and another currency""" if not self._backend: raise ExchangeBackendNotInstalled() return self._backend.rate(currency)
python
def rate(self, currency): if not self._backend: raise ExchangeBackendNotInstalled() return self._backend.rate(currency)
[ "def", "rate", "(", "self", ",", "currency", ")", ":", "if", "not", "self", ".", "_backend", ":", "raise", "ExchangeBackendNotInstalled", "(", ")", "return", "self", ".", "_backend", ".", "rate", "(", "currency", ")" ]
Return quotation between the base and another currency
[ "Return", "quotation", "between", "the", "base", "and", "another", "currency" ]
1e51f651f93edd62c16eb3d7aa034fec03096046
https://github.com/carlospalol/money/blob/1e51f651f93edd62c16eb3d7aa034fec03096046/money/exchange.py#L113-L117
7,931
anntzer/mplcursors
lib/mplcursors/_pick_info.py
_register_scatter
def _register_scatter(): """ Patch `PathCollection` and `scatter` to register their return values. This registration allows us to distinguish `PathCollection`s created by `Axes.scatter`, which should use point-like picking, from others, which should use path-like picking. The former is more common, so we store the latter instead; this also lets us guess the type better if this module is imported late. """ @functools.wraps(PathCollection.__init__) def __init__(self, *args, **kwargs): _nonscatter_pathcollections.add(self) return __init__.__wrapped__(self, *args, **kwargs) PathCollection.__init__ = __init__ @functools.wraps(Axes.scatter) def scatter(*args, **kwargs): paths = scatter.__wrapped__(*args, **kwargs) with suppress(KeyError): _nonscatter_pathcollections.remove(paths) return paths Axes.scatter = scatter
python
def _register_scatter(): @functools.wraps(PathCollection.__init__) def __init__(self, *args, **kwargs): _nonscatter_pathcollections.add(self) return __init__.__wrapped__(self, *args, **kwargs) PathCollection.__init__ = __init__ @functools.wraps(Axes.scatter) def scatter(*args, **kwargs): paths = scatter.__wrapped__(*args, **kwargs) with suppress(KeyError): _nonscatter_pathcollections.remove(paths) return paths Axes.scatter = scatter
[ "def", "_register_scatter", "(", ")", ":", "@", "functools", ".", "wraps", "(", "PathCollection", ".", "__init__", ")", "def", "__init__", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "_nonscatter_pathcollections", ".", "add", "(", "...
Patch `PathCollection` and `scatter` to register their return values. This registration allows us to distinguish `PathCollection`s created by `Axes.scatter`, which should use point-like picking, from others, which should use path-like picking. The former is more common, so we store the latter instead; this also lets us guess the type better if this module is imported late.
[ "Patch", "PathCollection", "and", "scatter", "to", "register", "their", "return", "values", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_pick_info.py#L37-L60
7,932
anntzer/mplcursors
lib/mplcursors/_pick_info.py
_call_with_selection
def _call_with_selection(func): """Decorator that passes a `Selection` built from the non-kwonly args.""" wrapped_kwonly_params = [ param for param in inspect.signature(func).parameters.values() if param.kind == param.KEYWORD_ONLY] sel_sig = inspect.signature(Selection) default_sel_sig = sel_sig.replace( parameters=[param.replace(default=None) if param.default is param.empty else param for param in sel_sig.parameters.values()]) @functools.wraps(func) def wrapper(*args, **kwargs): extra_kw = {param.name: kwargs.pop(param.name) for param in wrapped_kwonly_params if param.name in kwargs} ba = default_sel_sig.bind(*args, **kwargs) # apply_defaults ba.arguments = ChainMap( ba.arguments, {name: param.default for name, param in default_sel_sig.parameters.items() if param.default is not param.empty}) sel = Selection(*ba.args, **ba.kwargs) return func(sel, **extra_kw) wrapper.__signature__ = Signature( list(sel_sig.parameters.values()) + wrapped_kwonly_params) return wrapper
python
def _call_with_selection(func): wrapped_kwonly_params = [ param for param in inspect.signature(func).parameters.values() if param.kind == param.KEYWORD_ONLY] sel_sig = inspect.signature(Selection) default_sel_sig = sel_sig.replace( parameters=[param.replace(default=None) if param.default is param.empty else param for param in sel_sig.parameters.values()]) @functools.wraps(func) def wrapper(*args, **kwargs): extra_kw = {param.name: kwargs.pop(param.name) for param in wrapped_kwonly_params if param.name in kwargs} ba = default_sel_sig.bind(*args, **kwargs) # apply_defaults ba.arguments = ChainMap( ba.arguments, {name: param.default for name, param in default_sel_sig.parameters.items() if param.default is not param.empty}) sel = Selection(*ba.args, **ba.kwargs) return func(sel, **extra_kw) wrapper.__signature__ = Signature( list(sel_sig.parameters.values()) + wrapped_kwonly_params) return wrapper
[ "def", "_call_with_selection", "(", "func", ")", ":", "wrapped_kwonly_params", "=", "[", "param", "for", "param", "in", "inspect", ".", "signature", "(", "func", ")", ".", "parameters", ".", "values", "(", ")", "if", "param", ".", "kind", "==", "param", ...
Decorator that passes a `Selection` built from the non-kwonly args.
[ "Decorator", "that", "passes", "a", "Selection", "built", "from", "the", "non", "-", "kwonly", "args", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_pick_info.py#L481-L508
7,933
anntzer/mplcursors
lib/mplcursors/_pick_info.py
_set_valid_props
def _set_valid_props(artist, kwargs): """Set valid properties for the artist, dropping the others.""" artist.set(**{k: kwargs[k] for k in kwargs if hasattr(artist, "set_" + k)}) return artist
python
def _set_valid_props(artist, kwargs): artist.set(**{k: kwargs[k] for k in kwargs if hasattr(artist, "set_" + k)}) return artist
[ "def", "_set_valid_props", "(", "artist", ",", "kwargs", ")", ":", "artist", ".", "set", "(", "*", "*", "{", "k", ":", "kwargs", "[", "k", "]", "for", "k", "in", "kwargs", "if", "hasattr", "(", "artist", ",", "\"set_\"", "+", "k", ")", "}", ")", ...
Set valid properties for the artist, dropping the others.
[ "Set", "valid", "properties", "for", "the", "artist", "dropping", "the", "others", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_pick_info.py#L765-L768
7,934
carlospalol/money
money/money.py
Money.to
def to(self, currency): """Return equivalent money object in another currency""" if currency == self._currency: return self rate = xrates.quotation(self._currency, currency) if rate is None: raise ExchangeRateNotFound(xrates.backend_name, self._currency, currency) amount = self._amount * rate return self.__class__(amount, currency)
python
def to(self, currency): if currency == self._currency: return self rate = xrates.quotation(self._currency, currency) if rate is None: raise ExchangeRateNotFound(xrates.backend_name, self._currency, currency) amount = self._amount * rate return self.__class__(amount, currency)
[ "def", "to", "(", "self", ",", "currency", ")", ":", "if", "currency", "==", "self", ".", "_currency", ":", "return", "self", "rate", "=", "xrates", ".", "quotation", "(", "self", ".", "_currency", ",", "currency", ")", "if", "rate", "is", "None", ":...
Return equivalent money object in another currency
[ "Return", "equivalent", "money", "object", "in", "another", "currency" ]
1e51f651f93edd62c16eb3d7aa034fec03096046
https://github.com/carlospalol/money/blob/1e51f651f93edd62c16eb3d7aa034fec03096046/money/money.py#L245-L254
7,935
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
_get_rounded_intersection_area
def _get_rounded_intersection_area(bbox_1, bbox_2): """Compute the intersection area between two bboxes rounded to 8 digits.""" # The rounding allows sorting areas without floating point issues. bbox = bbox_1.intersection(bbox_1, bbox_2) return round(bbox.width * bbox.height, 8) if bbox else 0
python
def _get_rounded_intersection_area(bbox_1, bbox_2): # The rounding allows sorting areas without floating point issues. bbox = bbox_1.intersection(bbox_1, bbox_2) return round(bbox.width * bbox.height, 8) if bbox else 0
[ "def", "_get_rounded_intersection_area", "(", "bbox_1", ",", "bbox_2", ")", ":", "# The rounding allows sorting areas without floating point issues.", "bbox", "=", "bbox_1", ".", "intersection", "(", "bbox_1", ",", "bbox_2", ")", "return", "round", "(", "bbox", ".", "...
Compute the intersection area between two bboxes rounded to 8 digits.
[ "Compute", "the", "intersection", "area", "between", "two", "bboxes", "rounded", "to", "8", "digits", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L66-L70
7,936
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
cursor
def cursor(pickables=None, **kwargs): """ Create a `Cursor` for a list of artists, containers, and axes. Parameters ---------- pickables : Optional[List[Union[Artist, Container, Axes, Figure]]] All artists and containers in the list or on any of the axes or figures passed in the list are selectable by the constructed `Cursor`. Defaults to all artists and containers on any of the figures that :mod:`~matplotlib.pyplot` is tracking. Note that the latter will only work when relying on pyplot, not when figures are directly instantiated (e.g., when manually embedding Matplotlib in a GUI toolkit). **kwargs Keyword arguments are passed to the `Cursor` constructor. """ if pickables is None: # Do not import pyplot ourselves to avoid forcing the backend. plt = sys.modules.get("matplotlib.pyplot") pickables = [ plt.figure(num) for num in plt.get_fignums()] if plt else [] elif (isinstance(pickables, Container) or not isinstance(pickables, Iterable)): pickables = [pickables] def iter_unpack_figures(pickables): for entry in pickables: if isinstance(entry, Figure): yield from entry.axes else: yield entry def iter_unpack_axes(pickables): for entry in pickables: if isinstance(entry, Axes): yield from _iter_axes_subartists(entry) containers.extend(entry.containers) elif isinstance(entry, Container): containers.append(entry) else: yield entry containers = [] artists = list(iter_unpack_axes(iter_unpack_figures(pickables))) for container in containers: contained = list(filter(None, container.get_children())) for artist in contained: with suppress(ValueError): artists.remove(artist) if contained: artists.append(_pick_info.ContainerArtist(container)) return Cursor(artists, **kwargs)
python
def cursor(pickables=None, **kwargs): if pickables is None: # Do not import pyplot ourselves to avoid forcing the backend. plt = sys.modules.get("matplotlib.pyplot") pickables = [ plt.figure(num) for num in plt.get_fignums()] if plt else [] elif (isinstance(pickables, Container) or not isinstance(pickables, Iterable)): pickables = [pickables] def iter_unpack_figures(pickables): for entry in pickables: if isinstance(entry, Figure): yield from entry.axes else: yield entry def iter_unpack_axes(pickables): for entry in pickables: if isinstance(entry, Axes): yield from _iter_axes_subartists(entry) containers.extend(entry.containers) elif isinstance(entry, Container): containers.append(entry) else: yield entry containers = [] artists = list(iter_unpack_axes(iter_unpack_figures(pickables))) for container in containers: contained = list(filter(None, container.get_children())) for artist in contained: with suppress(ValueError): artists.remove(artist) if contained: artists.append(_pick_info.ContainerArtist(container)) return Cursor(artists, **kwargs)
[ "def", "cursor", "(", "pickables", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "pickables", "is", "None", ":", "# Do not import pyplot ourselves to avoid forcing the backend.", "plt", "=", "sys", ".", "modules", ".", "get", "(", "\"matplotlib.pyplot\"", ...
Create a `Cursor` for a list of artists, containers, and axes. Parameters ---------- pickables : Optional[List[Union[Artist, Container, Axes, Figure]]] All artists and containers in the list or on any of the axes or figures passed in the list are selectable by the constructed `Cursor`. Defaults to all artists and containers on any of the figures that :mod:`~matplotlib.pyplot` is tracking. Note that the latter will only work when relying on pyplot, not when figures are directly instantiated (e.g., when manually embedding Matplotlib in a GUI toolkit). **kwargs Keyword arguments are passed to the `Cursor` constructor.
[ "Create", "a", "Cursor", "for", "a", "list", "of", "artists", "containers", "and", "axes", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L546-L601
7,937
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
Cursor.selections
def selections(self): r"""The tuple of current `Selection`\s.""" for sel in self._selections: if sel.annotation.axes is None: raise RuntimeError("Annotation unexpectedly removed; " "use 'cursor.remove_selection' instead") return tuple(self._selections)
python
def selections(self): r"""The tuple of current `Selection`\s.""" for sel in self._selections: if sel.annotation.axes is None: raise RuntimeError("Annotation unexpectedly removed; " "use 'cursor.remove_selection' instead") return tuple(self._selections)
[ "def", "selections", "(", "self", ")", ":", "for", "sel", "in", "self", ".", "_selections", ":", "if", "sel", ".", "annotation", ".", "axes", "is", "None", ":", "raise", "RuntimeError", "(", "\"Annotation unexpectedly removed; \"", "\"use 'cursor.remove_selection'...
r"""The tuple of current `Selection`\s.
[ "r", "The", "tuple", "of", "current", "Selection", "\\", "s", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L259-L265
7,938
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
Cursor.add_selection
def add_selection(self, pi): """ Create an annotation for a `Selection` and register it. Returns a new `Selection`, that has been registered by the `Cursor`, with the added annotation set in the :attr:`annotation` field and, if applicable, the highlighting artist in the :attr:`extras` field. Emits the ``"add"`` event with the new `Selection` as argument. When the event is emitted, the position of the annotation is temporarily set to ``(nan, nan)``; if this position is not explicitly set by a callback, then a suitable position will be automatically computed. Likewise, if the text alignment is not explicitly set but the position is, then a suitable alignment will be automatically computed. """ # pi: "pick_info", i.e. an incomplete selection. # Pre-fetch the figure and axes, as callbacks may actually unset them. figure = pi.artist.figure axes = pi.artist.axes if axes.get_renderer_cache() is None: figure.canvas.draw() # Needed by draw_artist below anyways. renderer = pi.artist.axes.get_renderer_cache() ann = pi.artist.axes.annotate( _pick_info.get_ann_text(*pi), xy=pi.target, xytext=(np.nan, np.nan), ha=_MarkedStr("center"), va=_MarkedStr("center"), visible=self.visible, **self.annotation_kwargs) ann.draggable(use_blit=not self._multiple) extras = [] if self._highlight: hl = self.add_highlight(*pi) if hl: extras.append(hl) sel = pi._replace(annotation=ann, extras=extras) self._selections.append(sel) for cb in self._callbacks["add"]: cb(sel) # Check that `ann.axes` is still set, as callbacks may have removed the # annotation. if ann.axes and ann.xyann == (np.nan, np.nan): fig_bbox = figure.get_window_extent() ax_bbox = axes.get_window_extent() overlaps = [] for idx, annotation_position in enumerate( self.annotation_positions): ann.set(**annotation_position) # Work around matplotlib/matplotlib#7614: position update is # missing. ann.update_positions(renderer) bbox = ann.get_window_extent(renderer) overlaps.append( (_get_rounded_intersection_area(fig_bbox, bbox), _get_rounded_intersection_area(ax_bbox, bbox), # Avoid needlessly jumping around by breaking ties using # the last used position as default. idx == self._last_auto_position)) auto_position = max(range(len(overlaps)), key=overlaps.__getitem__) ann.set(**self.annotation_positions[auto_position]) self._last_auto_position = auto_position else: if isinstance(ann.get_ha(), _MarkedStr): ann.set_ha({-1: "right", 0: "center", 1: "left"}[ np.sign(np.nan_to_num(ann.xyann[0]))]) if isinstance(ann.get_va(), _MarkedStr): ann.set_va({-1: "top", 0: "center", 1: "bottom"}[ np.sign(np.nan_to_num(ann.xyann[1]))]) if (extras or len(self.selections) > 1 and not self._multiple or not figure.canvas.supports_blit): # Either: # - there may be more things to draw, or # - annotation removal will make a full redraw necessary, or # - blitting is not (yet) supported. figure.canvas.draw_idle() elif ann.axes: # Fast path, only needed if the annotation has not been immediately # removed. figure.draw_artist(ann) # Explicit argument needed on MacOSX backend. figure.canvas.blit(figure.bbox) # Removal comes after addition so that the fast blitting path works. if not self._multiple: for sel in self.selections[:-1]: self.remove_selection(sel) return sel
python
def add_selection(self, pi): # pi: "pick_info", i.e. an incomplete selection. # Pre-fetch the figure and axes, as callbacks may actually unset them. figure = pi.artist.figure axes = pi.artist.axes if axes.get_renderer_cache() is None: figure.canvas.draw() # Needed by draw_artist below anyways. renderer = pi.artist.axes.get_renderer_cache() ann = pi.artist.axes.annotate( _pick_info.get_ann_text(*pi), xy=pi.target, xytext=(np.nan, np.nan), ha=_MarkedStr("center"), va=_MarkedStr("center"), visible=self.visible, **self.annotation_kwargs) ann.draggable(use_blit=not self._multiple) extras = [] if self._highlight: hl = self.add_highlight(*pi) if hl: extras.append(hl) sel = pi._replace(annotation=ann, extras=extras) self._selections.append(sel) for cb in self._callbacks["add"]: cb(sel) # Check that `ann.axes` is still set, as callbacks may have removed the # annotation. if ann.axes and ann.xyann == (np.nan, np.nan): fig_bbox = figure.get_window_extent() ax_bbox = axes.get_window_extent() overlaps = [] for idx, annotation_position in enumerate( self.annotation_positions): ann.set(**annotation_position) # Work around matplotlib/matplotlib#7614: position update is # missing. ann.update_positions(renderer) bbox = ann.get_window_extent(renderer) overlaps.append( (_get_rounded_intersection_area(fig_bbox, bbox), _get_rounded_intersection_area(ax_bbox, bbox), # Avoid needlessly jumping around by breaking ties using # the last used position as default. idx == self._last_auto_position)) auto_position = max(range(len(overlaps)), key=overlaps.__getitem__) ann.set(**self.annotation_positions[auto_position]) self._last_auto_position = auto_position else: if isinstance(ann.get_ha(), _MarkedStr): ann.set_ha({-1: "right", 0: "center", 1: "left"}[ np.sign(np.nan_to_num(ann.xyann[0]))]) if isinstance(ann.get_va(), _MarkedStr): ann.set_va({-1: "top", 0: "center", 1: "bottom"}[ np.sign(np.nan_to_num(ann.xyann[1]))]) if (extras or len(self.selections) > 1 and not self._multiple or not figure.canvas.supports_blit): # Either: # - there may be more things to draw, or # - annotation removal will make a full redraw necessary, or # - blitting is not (yet) supported. figure.canvas.draw_idle() elif ann.axes: # Fast path, only needed if the annotation has not been immediately # removed. figure.draw_artist(ann) # Explicit argument needed on MacOSX backend. figure.canvas.blit(figure.bbox) # Removal comes after addition so that the fast blitting path works. if not self._multiple: for sel in self.selections[:-1]: self.remove_selection(sel) return sel
[ "def", "add_selection", "(", "self", ",", "pi", ")", ":", "# pi: \"pick_info\", i.e. an incomplete selection.", "# Pre-fetch the figure and axes, as callbacks may actually unset them.", "figure", "=", "pi", ".", "artist", ".", "figure", "axes", "=", "pi", ".", "artist", "...
Create an annotation for a `Selection` and register it. Returns a new `Selection`, that has been registered by the `Cursor`, with the added annotation set in the :attr:`annotation` field and, if applicable, the highlighting artist in the :attr:`extras` field. Emits the ``"add"`` event with the new `Selection` as argument. When the event is emitted, the position of the annotation is temporarily set to ``(nan, nan)``; if this position is not explicitly set by a callback, then a suitable position will be automatically computed. Likewise, if the text alignment is not explicitly set but the position is, then a suitable alignment will be automatically computed.
[ "Create", "an", "annotation", "for", "a", "Selection", "and", "register", "it", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L284-L372
7,939
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
Cursor.add_highlight
def add_highlight(self, artist, *args, **kwargs): """ Create, add, and return a highlighting artist. This method is should be called with an "unpacked" `Selection`, possibly with some fields set to None. It is up to the caller to register the artist with the proper `Selection` (by calling ``sel.extras.append`` on the result of this method) in order to ensure cleanup upon deselection. """ hl = _pick_info.make_highlight( artist, *args, **ChainMap({"highlight_kwargs": self.highlight_kwargs}, kwargs)) if hl: artist.axes.add_artist(hl) return hl
python
def add_highlight(self, artist, *args, **kwargs): hl = _pick_info.make_highlight( artist, *args, **ChainMap({"highlight_kwargs": self.highlight_kwargs}, kwargs)) if hl: artist.axes.add_artist(hl) return hl
[ "def", "add_highlight", "(", "self", ",", "artist", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "hl", "=", "_pick_info", ".", "make_highlight", "(", "artist", ",", "*", "args", ",", "*", "*", "ChainMap", "(", "{", "\"highlight_kwargs\"", ":", ...
Create, add, and return a highlighting artist. This method is should be called with an "unpacked" `Selection`, possibly with some fields set to None. It is up to the caller to register the artist with the proper `Selection` (by calling ``sel.extras.append`` on the result of this method) in order to ensure cleanup upon deselection.
[ "Create", "add", "and", "return", "a", "highlighting", "artist", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L374-L390
7,940
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
Cursor.connect
def connect(self, event, func=None): """ Connect a callback to a `Cursor` event; return the callback. Two events can be connected to: - callbacks connected to the ``"add"`` event are called when a `Selection` is added, with that selection as only argument; - callbacks connected to the ``"remove"`` event are called when a `Selection` is removed, with that selection as only argument. This method can also be used as a decorator:: @cursor.connect("add") def on_add(sel): ... Examples of callbacks:: # Change the annotation text and alignment: lambda sel: sel.annotation.set( text=sel.artist.get_label(), # or use e.g. sel.target.index ha="center", va="bottom") # Make label non-draggable: lambda sel: sel.draggable(False) """ if event not in self._callbacks: raise ValueError("{!r} is not a valid cursor event".format(event)) if func is None: return partial(self.connect, event) self._callbacks[event].append(func) return func
python
def connect(self, event, func=None): if event not in self._callbacks: raise ValueError("{!r} is not a valid cursor event".format(event)) if func is None: return partial(self.connect, event) self._callbacks[event].append(func) return func
[ "def", "connect", "(", "self", ",", "event", ",", "func", "=", "None", ")", ":", "if", "event", "not", "in", "self", ".", "_callbacks", ":", "raise", "ValueError", "(", "\"{!r} is not a valid cursor event\"", ".", "format", "(", "event", ")", ")", "if", ...
Connect a callback to a `Cursor` event; return the callback. Two events can be connected to: - callbacks connected to the ``"add"`` event are called when a `Selection` is added, with that selection as only argument; - callbacks connected to the ``"remove"`` event are called when a `Selection` is removed, with that selection as only argument. This method can also be used as a decorator:: @cursor.connect("add") def on_add(sel): ... Examples of callbacks:: # Change the annotation text and alignment: lambda sel: sel.annotation.set( text=sel.artist.get_label(), # or use e.g. sel.target.index ha="center", va="bottom") # Make label non-draggable: lambda sel: sel.draggable(False)
[ "Connect", "a", "callback", "to", "a", "Cursor", "event", ";", "return", "the", "callback", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L392-L424
7,941
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
Cursor.disconnect
def disconnect(self, event, cb): """ Disconnect a previously connected callback. If a callback is connected multiple times, only one connection is removed. """ try: self._callbacks[event].remove(cb) except KeyError: raise ValueError("{!r} is not a valid cursor event".format(event)) except ValueError: raise ValueError("Callback {} is not registered".format(event))
python
def disconnect(self, event, cb): try: self._callbacks[event].remove(cb) except KeyError: raise ValueError("{!r} is not a valid cursor event".format(event)) except ValueError: raise ValueError("Callback {} is not registered".format(event))
[ "def", "disconnect", "(", "self", ",", "event", ",", "cb", ")", ":", "try", ":", "self", ".", "_callbacks", "[", "event", "]", ".", "remove", "(", "cb", ")", "except", "KeyError", ":", "raise", "ValueError", "(", "\"{!r} is not a valid cursor event\"", "."...
Disconnect a previously connected callback. If a callback is connected multiple times, only one connection is removed.
[ "Disconnect", "a", "previously", "connected", "callback", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L426-L438
7,942
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
Cursor.remove
def remove(self): """ Remove a cursor. Remove all `Selection`\\s, disconnect all callbacks, and allow the cursor to be garbage collected. """ for disconnectors in self._disconnectors: disconnectors() for sel in self.selections: self.remove_selection(sel) for s in type(self)._keep_alive.values(): with suppress(KeyError): s.remove(self)
python
def remove(self): for disconnectors in self._disconnectors: disconnectors() for sel in self.selections: self.remove_selection(sel) for s in type(self)._keep_alive.values(): with suppress(KeyError): s.remove(self)
[ "def", "remove", "(", "self", ")", ":", "for", "disconnectors", "in", "self", ".", "_disconnectors", ":", "disconnectors", "(", ")", "for", "sel", "in", "self", ".", "selections", ":", "self", ".", "remove_selection", "(", "sel", ")", "for", "s", "in", ...
Remove a cursor. Remove all `Selection`\\s, disconnect all callbacks, and allow the cursor to be garbage collected.
[ "Remove", "a", "cursor", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L440-L453
7,943
anntzer/mplcursors
lib/mplcursors/_mplcursors.py
Cursor.remove_selection
def remove_selection(self, sel): """Remove a `Selection`.""" self._selections.remove(sel) # <artist>.figure will be unset so we save them first. figures = {artist.figure for artist in [sel.annotation] + sel.extras} # ValueError is raised if the artist has already been removed. with suppress(ValueError): sel.annotation.remove() for artist in sel.extras: with suppress(ValueError): artist.remove() for cb in self._callbacks["remove"]: cb(sel) for figure in figures: figure.canvas.draw_idle()
python
def remove_selection(self, sel): self._selections.remove(sel) # <artist>.figure will be unset so we save them first. figures = {artist.figure for artist in [sel.annotation] + sel.extras} # ValueError is raised if the artist has already been removed. with suppress(ValueError): sel.annotation.remove() for artist in sel.extras: with suppress(ValueError): artist.remove() for cb in self._callbacks["remove"]: cb(sel) for figure in figures: figure.canvas.draw_idle()
[ "def", "remove_selection", "(", "self", ",", "sel", ")", ":", "self", ".", "_selections", ".", "remove", "(", "sel", ")", "# <artist>.figure will be unset so we save them first.", "figures", "=", "{", "artist", ".", "figure", "for", "artist", "in", "[", "sel", ...
Remove a `Selection`.
[ "Remove", "a", "Selection", "." ]
a4bce17a978162b5a1837cc419114c910e7992f9
https://github.com/anntzer/mplcursors/blob/a4bce17a978162b5a1837cc419114c910e7992f9/lib/mplcursors/_mplcursors.py#L529-L543
7,944
mozilla/jupyter-notebook-gist
src/jupyter_notebook_gist/handlers.py
BaseHandler.request_access_token
def request_access_token(self, access_code): "Request access token from GitHub" token_response = request_session.post( "https://github.com/login/oauth/access_token", data={ "client_id": self.oauth_client_id, "client_secret": self.oauth_client_secret, "code": access_code }, headers={"Accept": "application/json"}, ) return helper_request_access_token(token_response.json())
python
def request_access_token(self, access_code): "Request access token from GitHub" token_response = request_session.post( "https://github.com/login/oauth/access_token", data={ "client_id": self.oauth_client_id, "client_secret": self.oauth_client_secret, "code": access_code }, headers={"Accept": "application/json"}, ) return helper_request_access_token(token_response.json())
[ "def", "request_access_token", "(", "self", ",", "access_code", ")", ":", "token_response", "=", "request_session", ".", "post", "(", "\"https://github.com/login/oauth/access_token\"", ",", "data", "=", "{", "\"client_id\"", ":", "self", ".", "oauth_client_id", ",", ...
Request access token from GitHub
[ "Request", "access", "token", "from", "GitHub" ]
dae052f0998fa80dff515345cd516b586eff8e43
https://github.com/mozilla/jupyter-notebook-gist/blob/dae052f0998fa80dff515345cd516b586eff8e43/src/jupyter_notebook_gist/handlers.py#L39-L50
7,945
rdobson/python-hwinfo
hwinfo/util/__init__.py
combine_dicts
def combine_dicts(recs): """Combine a list of recs, appending values to matching keys""" if not recs: return None if len(recs) == 1: return recs.pop() new_rec = {} for rec in recs: for k, v in rec.iteritems(): if k in new_rec: new_rec[k] = "%s, %s" % (new_rec[k], v) else: new_rec[k] = v return new_rec
python
def combine_dicts(recs): if not recs: return None if len(recs) == 1: return recs.pop() new_rec = {} for rec in recs: for k, v in rec.iteritems(): if k in new_rec: new_rec[k] = "%s, %s" % (new_rec[k], v) else: new_rec[k] = v return new_rec
[ "def", "combine_dicts", "(", "recs", ")", ":", "if", "not", "recs", ":", "return", "None", "if", "len", "(", "recs", ")", "==", "1", ":", "return", "recs", ".", "pop", "(", ")", "new_rec", "=", "{", "}", "for", "rec", "in", "recs", ":", "for", ...
Combine a list of recs, appending values to matching keys
[ "Combine", "a", "list", "of", "recs", "appending", "values", "to", "matching", "keys" ]
ba93a112dac6863396a053636ea87df027daa5de
https://github.com/rdobson/python-hwinfo/blob/ba93a112dac6863396a053636ea87df027daa5de/hwinfo/util/__init__.py#L5-L20
7,946
rdobson/python-hwinfo
hwinfo/tools/inspector.py
combine_recs
def combine_recs(rec_list, key): """Use a common key to combine a list of recs""" final_recs = {} for rec in rec_list: rec_key = rec[key] if rec_key in final_recs: for k, v in rec.iteritems(): if k in final_recs[rec_key] and final_recs[rec_key][k] != v: raise Exception("Mis-match for key '%s'" % k) final_recs[rec_key][k] = v else: final_recs[rec_key] = rec return final_recs.values()
python
def combine_recs(rec_list, key): final_recs = {} for rec in rec_list: rec_key = rec[key] if rec_key in final_recs: for k, v in rec.iteritems(): if k in final_recs[rec_key] and final_recs[rec_key][k] != v: raise Exception("Mis-match for key '%s'" % k) final_recs[rec_key][k] = v else: final_recs[rec_key] = rec return final_recs.values()
[ "def", "combine_recs", "(", "rec_list", ",", "key", ")", ":", "final_recs", "=", "{", "}", "for", "rec", "in", "rec_list", ":", "rec_key", "=", "rec", "[", "key", "]", "if", "rec_key", "in", "final_recs", ":", "for", "k", ",", "v", "in", "rec", "."...
Use a common key to combine a list of recs
[ "Use", "a", "common", "key", "to", "combine", "a", "list", "of", "recs" ]
ba93a112dac6863396a053636ea87df027daa5de
https://github.com/rdobson/python-hwinfo/blob/ba93a112dac6863396a053636ea87df027daa5de/hwinfo/tools/inspector.py#L195-L207
7,947
rdobson/python-hwinfo
hwinfo/tools/inspector.py
HostFromTarball._load_from_file
def _load_from_file(self, filename): """Find filename in tar, and load it""" if filename in self.fdata: return self.fdata[filename] else: filepath = find_in_tarball(self.tarloc, filename) return read_from_tarball(self.tarloc, filepath)
python
def _load_from_file(self, filename): if filename in self.fdata: return self.fdata[filename] else: filepath = find_in_tarball(self.tarloc, filename) return read_from_tarball(self.tarloc, filepath)
[ "def", "_load_from_file", "(", "self", ",", "filename", ")", ":", "if", "filename", "in", "self", ".", "fdata", ":", "return", "self", ".", "fdata", "[", "filename", "]", "else", ":", "filepath", "=", "find_in_tarball", "(", "self", ".", "tarloc", ",", ...
Find filename in tar, and load it
[ "Find", "filename", "in", "tar", "and", "load", "it" ]
ba93a112dac6863396a053636ea87df027daa5de
https://github.com/rdobson/python-hwinfo/blob/ba93a112dac6863396a053636ea87df027daa5de/hwinfo/tools/inspector.py#L272-L278
7,948
LuminosoInsight/langcodes
langcodes/tag_parser.py
parse_extlang
def parse_extlang(subtags): """ Parse an 'extended language' tag, which consists of 1 to 3 three-letter language codes. Extended languages are used for distinguishing dialects/sublanguages (depending on your view) of macrolanguages such as Arabic, Bahasa Malay, and Chinese. It's supposed to also be acceptable to just use the sublanguage as the primary language code, and your code should know what's a macrolanguage of what. For example, 'zh-yue' and 'yue' are the same language (Cantonese), and differ only in whether they explicitly spell out that Cantonese is a kind of Chinese. """ index = 0 parsed = [] while index < len(subtags) and len(subtags[index]) == 3 and index < 3: parsed.append(('extlang', subtags[index])) index += 1 return parsed + parse_subtags(subtags[index:], SCRIPT)
python
def parse_extlang(subtags): index = 0 parsed = [] while index < len(subtags) and len(subtags[index]) == 3 and index < 3: parsed.append(('extlang', subtags[index])) index += 1 return parsed + parse_subtags(subtags[index:], SCRIPT)
[ "def", "parse_extlang", "(", "subtags", ")", ":", "index", "=", "0", "parsed", "=", "[", "]", "while", "index", "<", "len", "(", "subtags", ")", "and", "len", "(", "subtags", "[", "index", "]", ")", "==", "3", "and", "index", "<", "3", ":", "pars...
Parse an 'extended language' tag, which consists of 1 to 3 three-letter language codes. Extended languages are used for distinguishing dialects/sublanguages (depending on your view) of macrolanguages such as Arabic, Bahasa Malay, and Chinese. It's supposed to also be acceptable to just use the sublanguage as the primary language code, and your code should know what's a macrolanguage of what. For example, 'zh-yue' and 'yue' are the same language (Cantonese), and differ only in whether they explicitly spell out that Cantonese is a kind of Chinese.
[ "Parse", "an", "extended", "language", "tag", "which", "consists", "of", "1", "to", "3", "three", "-", "letter", "language", "codes", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/tag_parser.py#L252-L272
7,949
LuminosoInsight/langcodes
langcodes/tag_parser.py
order_error
def order_error(subtag, got, expected): """ Output an error indicating that tags were out of order. """ options = SUBTAG_TYPES[expected:] if len(options) == 1: expect_str = options[0] elif len(options) == 2: expect_str = '%s or %s' % (options[0], options[1]) else: expect_str = '%s, or %s' % (', '.join(options[:-1]), options[-1]) got_str = SUBTAG_TYPES[got] raise LanguageTagError("This %s subtag, %r, is out of place. " "Expected %s." % (got_str, subtag, expect_str))
python
def order_error(subtag, got, expected): options = SUBTAG_TYPES[expected:] if len(options) == 1: expect_str = options[0] elif len(options) == 2: expect_str = '%s or %s' % (options[0], options[1]) else: expect_str = '%s, or %s' % (', '.join(options[:-1]), options[-1]) got_str = SUBTAG_TYPES[got] raise LanguageTagError("This %s subtag, %r, is out of place. " "Expected %s." % (got_str, subtag, expect_str))
[ "def", "order_error", "(", "subtag", ",", "got", ",", "expected", ")", ":", "options", "=", "SUBTAG_TYPES", "[", "expected", ":", "]", "if", "len", "(", "options", ")", "==", "1", ":", "expect_str", "=", "options", "[", "0", "]", "elif", "len", "(", ...
Output an error indicating that tags were out of order.
[ "Output", "an", "error", "indicating", "that", "tags", "were", "out", "of", "order", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/tag_parser.py#L315-L328
7,950
LuminosoInsight/langcodes
langcodes/__init__.py
tag_match_score
def tag_match_score(desired: {str, Language}, supported: {str, Language}) -> int: """ Return a number from 0 to 100 indicating the strength of match between the language the user desires, D, and a supported language, S. Higher numbers are better. A reasonable cutoff for not messing with your users is to only accept scores of 75 or more. A score of 100 means the languages are the same, possibly after normalizing and filling in likely values. >>> tag_match_score('en', 'en') 100 >>> tag_match_score('en', 'en-US') 100 >>> tag_match_score('zh-Hant', 'zh-TW') 100 >>> tag_match_score('ru-Cyrl', 'ru') 100 >>> # Serbo-Croatian is a politically contentious idea, but in practice >>> # it's considered equivalent to Serbian in Latin characters. >>> tag_match_score('sh', 'sr-Latn') 100 A score of 92 to 97 indicates a regional difference. >>> tag_match_score('zh-HK', 'zh-MO') # Chinese is similar in Hong Kong and Macao 97 >>> tag_match_score('en-AU', 'en-GB') # Australian English is similar to British English 96 >>> tag_match_score('en-IN', 'en-GB') # Indian English is also similar to British English 96 >>> tag_match_score('es-PR', 'es-419') # Peruvian Spanish is Latin American Spanish 96 >>> tag_match_score('en-US', 'en-GB') # American and British English are somewhat different 94 >>> tag_match_score('es-MX', 'es-ES') # Mexican Spanish is different from Spanish Spanish 92 >>> # Serbian has two scripts, and people might prefer one but understand both >>> tag_match_score('sr-Latn', 'sr-Cyrl') 95 >>> # European Portuguese is different from the most common form (Brazilian Portuguese) >>> tag_match_score('pt', 'pt-PT') 92 A score of 86 to 90 indicates that people who use the desired language are demographically likely to understand the supported language, even if the languages themselves are unrelated. There are many languages that have a one-way connection of this kind to English or French. >>> tag_match_score('ta', 'en') # Tamil to English 86 >>> tag_match_score('mg', 'fr') # Malagasy to French 86 Sometimes it's more straightforward than that: people who use the desired language are demographically likely to understand the supported language because it's demographically relevant and highly related. >>> tag_match_score('af', 'nl') # Afrikaans to Dutch 86 >>> tag_match_score('ms', 'id') # Malay to Indonesian 86 >>> tag_match_score('nn', 'nb') # Nynorsk to Norwegian Bokmål 90 >>> tag_match_score('nb', 'da') # Norwegian Bokmål to Danish 88 A score of 80 to 85 indicates a particularly contentious difference in script, where people who understand one script can learn the other but probably won't be happy with it. This specifically applies to Chinese. >>> tag_match_score('zh-Hans', 'zh-Hant') 85 >>> tag_match_score('zh-CN', 'zh-HK') 85 >>> tag_match_score('zh-CN', 'zh-TW') 85 >>> tag_match_score('zh-Hant', 'zh-Hans') 81 >>> tag_match_score('zh-TW', 'zh-CN') 81 When the supported script is a different one than desired, this is usually a major difference with score of 60 or less. >>> tag_match_score('ja', 'ja-Latn-US-hepburn') 56 >>> # You can read the Shavian script, right? >>> tag_match_score('en', 'en-Shaw') 56 When there is no indication the supported language will be understood, the score will be 20 or less, to a minimum of 0. >>> tag_match_score('es', 'fr') # Spanish and French are different. 16 >>> tag_match_score('en', 'ta') # English speakers generally do not know Tamil. 0 CLDR doesn't take into account which languages are considered part of a common 'macrolanguage'. We have this data, so we can use it in matching. If two languages have no other rule that would allow them to match, but share a macrolanguage, they'll get a match score of 20 less than what they would get if the language matched. >>> tag_match_score('arz', 'ar') # Egyptian Arabic to Standard Arabic 80 >>> tag_match_score('arz', 'ary') # Egyptian Arabic to Moroccan Arabic 76 Here's an example that has script, region, and language differences, but a macrolanguage in common. Written Chinese is usually presumed to be Mandarin Chinese, but colloquial Cantonese can be written as well. When it is, it probably has region, script, and language differences from the usual mainland Chinese. But it is still part of the 'Chinese' macrolanguage, so there is more similarity than, say, comparing Mandarin to Hindi. >>> tag_match_score('yue', 'zh') 36 Comparing Swiss German ('gsw') to standardized German ('de') shows how these scores can be asymmetrical. Swiss German speakers will understand German, so the score in that direction is 92. Most German speakers find Swiss German unintelligible, and CLDR in fact assigns this a score of 16. This seems a little bit extreme, but the asymmetry is certainly there. And if your text is tagged as 'gsw', it must be that way for a reason. >>> tag_match_score('gsw', 'de') 92 >>> tag_match_score('de', 'gsw') 16 """ desired_ld = Language.get(desired) supported_ld = Language.get(supported) return desired_ld.match_score(supported_ld)
python
def tag_match_score(desired: {str, Language}, supported: {str, Language}) -> int: desired_ld = Language.get(desired) supported_ld = Language.get(supported) return desired_ld.match_score(supported_ld)
[ "def", "tag_match_score", "(", "desired", ":", "{", "str", ",", "Language", "}", ",", "supported", ":", "{", "str", ",", "Language", "}", ")", "->", "int", ":", "desired_ld", "=", "Language", ".", "get", "(", "desired", ")", "supported_ld", "=", "Langu...
Return a number from 0 to 100 indicating the strength of match between the language the user desires, D, and a supported language, S. Higher numbers are better. A reasonable cutoff for not messing with your users is to only accept scores of 75 or more. A score of 100 means the languages are the same, possibly after normalizing and filling in likely values. >>> tag_match_score('en', 'en') 100 >>> tag_match_score('en', 'en-US') 100 >>> tag_match_score('zh-Hant', 'zh-TW') 100 >>> tag_match_score('ru-Cyrl', 'ru') 100 >>> # Serbo-Croatian is a politically contentious idea, but in practice >>> # it's considered equivalent to Serbian in Latin characters. >>> tag_match_score('sh', 'sr-Latn') 100 A score of 92 to 97 indicates a regional difference. >>> tag_match_score('zh-HK', 'zh-MO') # Chinese is similar in Hong Kong and Macao 97 >>> tag_match_score('en-AU', 'en-GB') # Australian English is similar to British English 96 >>> tag_match_score('en-IN', 'en-GB') # Indian English is also similar to British English 96 >>> tag_match_score('es-PR', 'es-419') # Peruvian Spanish is Latin American Spanish 96 >>> tag_match_score('en-US', 'en-GB') # American and British English are somewhat different 94 >>> tag_match_score('es-MX', 'es-ES') # Mexican Spanish is different from Spanish Spanish 92 >>> # Serbian has two scripts, and people might prefer one but understand both >>> tag_match_score('sr-Latn', 'sr-Cyrl') 95 >>> # European Portuguese is different from the most common form (Brazilian Portuguese) >>> tag_match_score('pt', 'pt-PT') 92 A score of 86 to 90 indicates that people who use the desired language are demographically likely to understand the supported language, even if the languages themselves are unrelated. There are many languages that have a one-way connection of this kind to English or French. >>> tag_match_score('ta', 'en') # Tamil to English 86 >>> tag_match_score('mg', 'fr') # Malagasy to French 86 Sometimes it's more straightforward than that: people who use the desired language are demographically likely to understand the supported language because it's demographically relevant and highly related. >>> tag_match_score('af', 'nl') # Afrikaans to Dutch 86 >>> tag_match_score('ms', 'id') # Malay to Indonesian 86 >>> tag_match_score('nn', 'nb') # Nynorsk to Norwegian Bokmål 90 >>> tag_match_score('nb', 'da') # Norwegian Bokmål to Danish 88 A score of 80 to 85 indicates a particularly contentious difference in script, where people who understand one script can learn the other but probably won't be happy with it. This specifically applies to Chinese. >>> tag_match_score('zh-Hans', 'zh-Hant') 85 >>> tag_match_score('zh-CN', 'zh-HK') 85 >>> tag_match_score('zh-CN', 'zh-TW') 85 >>> tag_match_score('zh-Hant', 'zh-Hans') 81 >>> tag_match_score('zh-TW', 'zh-CN') 81 When the supported script is a different one than desired, this is usually a major difference with score of 60 or less. >>> tag_match_score('ja', 'ja-Latn-US-hepburn') 56 >>> # You can read the Shavian script, right? >>> tag_match_score('en', 'en-Shaw') 56 When there is no indication the supported language will be understood, the score will be 20 or less, to a minimum of 0. >>> tag_match_score('es', 'fr') # Spanish and French are different. 16 >>> tag_match_score('en', 'ta') # English speakers generally do not know Tamil. 0 CLDR doesn't take into account which languages are considered part of a common 'macrolanguage'. We have this data, so we can use it in matching. If two languages have no other rule that would allow them to match, but share a macrolanguage, they'll get a match score of 20 less than what they would get if the language matched. >>> tag_match_score('arz', 'ar') # Egyptian Arabic to Standard Arabic 80 >>> tag_match_score('arz', 'ary') # Egyptian Arabic to Moroccan Arabic 76 Here's an example that has script, region, and language differences, but a macrolanguage in common. Written Chinese is usually presumed to be Mandarin Chinese, but colloquial Cantonese can be written as well. When it is, it probably has region, script, and language differences from the usual mainland Chinese. But it is still part of the 'Chinese' macrolanguage, so there is more similarity than, say, comparing Mandarin to Hindi. >>> tag_match_score('yue', 'zh') 36 Comparing Swiss German ('gsw') to standardized German ('de') shows how these scores can be asymmetrical. Swiss German speakers will understand German, so the score in that direction is 92. Most German speakers find Swiss German unintelligible, and CLDR in fact assigns this a score of 16. This seems a little bit extreme, but the asymmetry is certainly there. And if your text is tagged as 'gsw', it must be that way for a reason. >>> tag_match_score('gsw', 'de') 92 >>> tag_match_score('de', 'gsw') 16
[ "Return", "a", "number", "from", "0", "to", "100", "indicating", "the", "strength", "of", "match", "between", "the", "language", "the", "user", "desires", "D", "and", "a", "supported", "language", "S", ".", "Higher", "numbers", "are", "better", ".", "A", ...
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L1021-L1159
7,951
LuminosoInsight/langcodes
langcodes/__init__.py
best_match
def best_match(desired_language: {str, Language}, supported_languages: list, min_score: int=75) -> (str, int): """ You have software that supports any of the `supported_languages`. You want to use `desired_language`. This function lets you choose the right language, even if there isn't an exact match. Returns: - The best-matching language code, which will be one of the `supported_languages` or 'und' - The score of the match, from 0 to 100 `min_score` sets the minimum match score. If all languages match with a lower score than that, the result will be 'und' with a score of 0. When there is a tie for the best matching language, the first one in the tie will be used. Setting `min_score` lower will enable more things to match, at the cost of possibly mis-handling data or upsetting users. Read the documentation for :func:`tag_match_score` to understand what the numbers mean. >>> best_match('fr', ['de', 'en', 'fr']) ('fr', 100) >>> best_match('sh', ['hr', 'bs', 'sr-Latn', 'sr-Cyrl']) ('sr-Latn', 100) >>> best_match('zh-CN', ['zh-Hant', 'zh-Hans', 'gan', 'nan']) ('zh-Hans', 100) >>> best_match('zh-CN', ['cmn-Hant', 'cmn-Hans', 'gan', 'nan']) ('cmn-Hans', 100) >>> best_match('pt', ['pt-BR', 'pt-PT']) ('pt-BR', 100) >>> best_match('en-AU', ['en-GB', 'en-US']) ('en-GB', 96) >>> best_match('es-MX', ['es-ES', 'es-419', 'en-US']) ('es-419', 96) >>> best_match('es-MX', ['es-PU', 'es-AR', 'es-PY']) ('es-PU', 95) >>> best_match('es-MX', ['es-AR', 'es-PU', 'es-PY']) ('es-AR', 95) >>> best_match('zsm', ['id', 'mhp']) ('id', 86) >>> best_match('eu', ['el', 'en', 'es']) ('es', 90) >>> best_match('eu', ['el', 'en', 'es'], min_score=92) ('und', 0) """ # Quickly return if the desired language is directly supported if desired_language in supported_languages: return desired_language, 100 # Reduce the desired language to a standard form that could also match desired_language = standardize_tag(desired_language) if desired_language in supported_languages: return desired_language, 100 match_scores = [ (supported, tag_match_score(desired_language, supported)) for supported in supported_languages ] match_scores = [ (supported, score) for (supported, score) in match_scores if score >= min_score ] + [('und', 0)] match_scores.sort(key=lambda item: -item[1]) return match_scores[0]
python
def best_match(desired_language: {str, Language}, supported_languages: list, min_score: int=75) -> (str, int): # Quickly return if the desired language is directly supported if desired_language in supported_languages: return desired_language, 100 # Reduce the desired language to a standard form that could also match desired_language = standardize_tag(desired_language) if desired_language in supported_languages: return desired_language, 100 match_scores = [ (supported, tag_match_score(desired_language, supported)) for supported in supported_languages ] match_scores = [ (supported, score) for (supported, score) in match_scores if score >= min_score ] + [('und', 0)] match_scores.sort(key=lambda item: -item[1]) return match_scores[0]
[ "def", "best_match", "(", "desired_language", ":", "{", "str", ",", "Language", "}", ",", "supported_languages", ":", "list", ",", "min_score", ":", "int", "=", "75", ")", "->", "(", "str", ",", "int", ")", ":", "# Quickly return if the desired language is dir...
You have software that supports any of the `supported_languages`. You want to use `desired_language`. This function lets you choose the right language, even if there isn't an exact match. Returns: - The best-matching language code, which will be one of the `supported_languages` or 'und' - The score of the match, from 0 to 100 `min_score` sets the minimum match score. If all languages match with a lower score than that, the result will be 'und' with a score of 0. When there is a tie for the best matching language, the first one in the tie will be used. Setting `min_score` lower will enable more things to match, at the cost of possibly mis-handling data or upsetting users. Read the documentation for :func:`tag_match_score` to understand what the numbers mean. >>> best_match('fr', ['de', 'en', 'fr']) ('fr', 100) >>> best_match('sh', ['hr', 'bs', 'sr-Latn', 'sr-Cyrl']) ('sr-Latn', 100) >>> best_match('zh-CN', ['zh-Hant', 'zh-Hans', 'gan', 'nan']) ('zh-Hans', 100) >>> best_match('zh-CN', ['cmn-Hant', 'cmn-Hans', 'gan', 'nan']) ('cmn-Hans', 100) >>> best_match('pt', ['pt-BR', 'pt-PT']) ('pt-BR', 100) >>> best_match('en-AU', ['en-GB', 'en-US']) ('en-GB', 96) >>> best_match('es-MX', ['es-ES', 'es-419', 'en-US']) ('es-419', 96) >>> best_match('es-MX', ['es-PU', 'es-AR', 'es-PY']) ('es-PU', 95) >>> best_match('es-MX', ['es-AR', 'es-PU', 'es-PY']) ('es-AR', 95) >>> best_match('zsm', ['id', 'mhp']) ('id', 86) >>> best_match('eu', ['el', 'en', 'es']) ('es', 90) >>> best_match('eu', ['el', 'en', 'es'], min_score=92) ('und', 0)
[ "You", "have", "software", "that", "supports", "any", "of", "the", "supported_languages", ".", "You", "want", "to", "use", "desired_language", ".", "This", "function", "lets", "you", "choose", "the", "right", "language", "even", "if", "there", "isn", "t", "a...
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L1162-L1229
7,952
LuminosoInsight/langcodes
langcodes/__init__.py
Language.make
def make(cls, language=None, extlangs=None, script=None, region=None, variants=None, extensions=None, private=None): """ Create a Language object by giving any subset of its attributes. If this value has been created before, return the existing value. """ values = (language, tuple(extlangs or ()), script, region, tuple(variants or ()), tuple(extensions or ()), private) if values in cls._INSTANCES: return cls._INSTANCES[values] instance = cls( language=language, extlangs=extlangs, script=script, region=region, variants=variants, extensions=extensions, private=private ) cls._INSTANCES[values] = instance return instance
python
def make(cls, language=None, extlangs=None, script=None, region=None, variants=None, extensions=None, private=None): values = (language, tuple(extlangs or ()), script, region, tuple(variants or ()), tuple(extensions or ()), private) if values in cls._INSTANCES: return cls._INSTANCES[values] instance = cls( language=language, extlangs=extlangs, script=script, region=region, variants=variants, extensions=extensions, private=private ) cls._INSTANCES[values] = instance return instance
[ "def", "make", "(", "cls", ",", "language", "=", "None", ",", "extlangs", "=", "None", ",", "script", "=", "None", ",", "region", "=", "None", ",", "variants", "=", "None", ",", "extensions", "=", "None", ",", "private", "=", "None", ")", ":", "val...
Create a Language object by giving any subset of its attributes. If this value has been created before, return the existing value.
[ "Create", "a", "Language", "object", "by", "giving", "any", "subset", "of", "its", "attributes", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L104-L122
7,953
LuminosoInsight/langcodes
langcodes/__init__.py
Language.get
def get(tag: {str, 'Language'}, normalize=True) -> 'Language': """ Create a Language object from a language tag string. If normalize=True, non-standard or overlong tags will be replaced as they're interpreted. This is recommended. Here are several examples of language codes, which are also test cases. Most language codes are straightforward, but these examples will get pretty obscure toward the end. >>> Language.get('en-US') Language.make(language='en', region='US') >>> Language.get('zh-Hant') Language.make(language='zh', script='Hant') >>> Language.get('und') Language.make() This function is idempotent, in case you already have a Language object: >>> Language.get(Language.get('en-us')) Language.make(language='en', region='US') The non-code 'root' is sometimes used to represent the lack of any language information, similar to 'und'. >>> Language.get('root') Language.make() By default, getting a Language object will automatically convert deprecated tags: >>> Language.get('iw') Language.make(language='he') >>> Language.get('in') Language.make(language='id') One type of deprecated tag that should be replaced is for sign languages, which used to all be coded as regional variants of a fictitious global sign language called 'sgn'. Of course, there is no global sign language, so sign languages now have their own language codes. >>> Language.get('sgn-US') Language.make(language='ase') >>> Language.get('sgn-US', normalize=False) Language.make(language='sgn', region='US') 'en-gb-oed' is a tag that's grandfathered into the standard because it has been used to mean "spell-check this with Oxford English Dictionary spelling", but that tag has the wrong shape. We interpret this as the new standardized tag 'en-gb-oxendict', unless asked not to normalize. >>> Language.get('en-gb-oed') Language.make(language='en', region='GB', variants=['oxendict']) >>> Language.get('en-gb-oed', normalize=False) Language.make(language='en-gb-oed') 'zh-min-nan' is another oddly-formed tag, used to represent the Southern Min language, which includes Taiwanese as a regional form. It now has its own language code. >>> Language.get('zh-min-nan') Language.make(language='nan') There's not much we can do with the vague tag 'zh-min': >>> Language.get('zh-min') Language.make(language='zh-min') Occasionally Wiktionary will use 'extlang' tags in strange ways, such as using the tag 'und-ibe' for some unspecified Iberian language. >>> Language.get('und-ibe') Language.make(extlangs=['ibe']) Here's an example of replacing multiple deprecated tags. The language tag 'sh' (Serbo-Croatian) ended up being politically problematic, and different standards took different steps to address this. The IANA made it into a macrolanguage that contains 'sr', 'hr', and 'bs'. Unicode further decided that it's a legacy tag that should be interpreted as 'sr-Latn', which the language matching rules say is mutually intelligible with all those languages. We complicate the example by adding on the region tag 'QU', an old provisional tag for the European Union, which is now standardized as 'EU'. >>> Language.get('sh-QU') Language.make(language='sr', script='Latn', region='EU') """ if isinstance(tag, Language): if not normalize: # shortcut: we have the tag already return tag # We might need to normalize this tag. Convert it back into a # string tag, to cover all the edge cases of normalization in a # way that we've already solved. tag = tag.to_tag() if (tag, normalize) in Language._PARSE_CACHE: return Language._PARSE_CACHE[tag, normalize] data = {} # if the complete tag appears as something to normalize, do the # normalization right away. Smash case when checking, because the # case normalization that comes from parse_tag() hasn't been applied # yet. tag_lower = tag.lower() if normalize and tag_lower in LANGUAGE_REPLACEMENTS: tag = LANGUAGE_REPLACEMENTS[tag_lower] components = parse_tag(tag) for typ, value in components: if typ == 'extlang' and normalize and 'language' in data: # smash extlangs when possible minitag = '%s-%s' % (data['language'], value) norm = LANGUAGE_REPLACEMENTS.get(minitag.lower()) if norm is not None: data.update( Language.get(norm, normalize).to_dict() ) else: data.setdefault('extlangs', []).append(value) elif typ in {'extlang', 'variant', 'extension'}: data.setdefault(typ + 's', []).append(value) elif typ == 'language': if value == 'und': pass elif normalize: replacement = LANGUAGE_REPLACEMENTS.get(value.lower()) if replacement is not None: # parse the replacement if necessary -- this helps with # Serbian and Moldovan data.update( Language.get(replacement, normalize).to_dict() ) else: data['language'] = value else: data['language'] = value elif typ == 'region': if normalize: data['region'] = REGION_REPLACEMENTS.get(value.lower(), value) else: data['region'] = value elif typ == 'grandfathered': # If we got here, we got a grandfathered tag but we were asked # not to normalize it, or the CLDR data doesn't know how to # normalize it. The best we can do is set the entire tag as the # language. data['language'] = value else: data[typ] = value result = Language.make(**data) Language._PARSE_CACHE[tag, normalize] = result return result
python
def get(tag: {str, 'Language'}, normalize=True) -> 'Language': if isinstance(tag, Language): if not normalize: # shortcut: we have the tag already return tag # We might need to normalize this tag. Convert it back into a # string tag, to cover all the edge cases of normalization in a # way that we've already solved. tag = tag.to_tag() if (tag, normalize) in Language._PARSE_CACHE: return Language._PARSE_CACHE[tag, normalize] data = {} # if the complete tag appears as something to normalize, do the # normalization right away. Smash case when checking, because the # case normalization that comes from parse_tag() hasn't been applied # yet. tag_lower = tag.lower() if normalize and tag_lower in LANGUAGE_REPLACEMENTS: tag = LANGUAGE_REPLACEMENTS[tag_lower] components = parse_tag(tag) for typ, value in components: if typ == 'extlang' and normalize and 'language' in data: # smash extlangs when possible minitag = '%s-%s' % (data['language'], value) norm = LANGUAGE_REPLACEMENTS.get(minitag.lower()) if norm is not None: data.update( Language.get(norm, normalize).to_dict() ) else: data.setdefault('extlangs', []).append(value) elif typ in {'extlang', 'variant', 'extension'}: data.setdefault(typ + 's', []).append(value) elif typ == 'language': if value == 'und': pass elif normalize: replacement = LANGUAGE_REPLACEMENTS.get(value.lower()) if replacement is not None: # parse the replacement if necessary -- this helps with # Serbian and Moldovan data.update( Language.get(replacement, normalize).to_dict() ) else: data['language'] = value else: data['language'] = value elif typ == 'region': if normalize: data['region'] = REGION_REPLACEMENTS.get(value.lower(), value) else: data['region'] = value elif typ == 'grandfathered': # If we got here, we got a grandfathered tag but we were asked # not to normalize it, or the CLDR data doesn't know how to # normalize it. The best we can do is set the entire tag as the # language. data['language'] = value else: data[typ] = value result = Language.make(**data) Language._PARSE_CACHE[tag, normalize] = result return result
[ "def", "get", "(", "tag", ":", "{", "str", ",", "'Language'", "}", ",", "normalize", "=", "True", ")", "->", "'Language'", ":", "if", "isinstance", "(", "tag", ",", "Language", ")", ":", "if", "not", "normalize", ":", "# shortcut: we have the tag already",...
Create a Language object from a language tag string. If normalize=True, non-standard or overlong tags will be replaced as they're interpreted. This is recommended. Here are several examples of language codes, which are also test cases. Most language codes are straightforward, but these examples will get pretty obscure toward the end. >>> Language.get('en-US') Language.make(language='en', region='US') >>> Language.get('zh-Hant') Language.make(language='zh', script='Hant') >>> Language.get('und') Language.make() This function is idempotent, in case you already have a Language object: >>> Language.get(Language.get('en-us')) Language.make(language='en', region='US') The non-code 'root' is sometimes used to represent the lack of any language information, similar to 'und'. >>> Language.get('root') Language.make() By default, getting a Language object will automatically convert deprecated tags: >>> Language.get('iw') Language.make(language='he') >>> Language.get('in') Language.make(language='id') One type of deprecated tag that should be replaced is for sign languages, which used to all be coded as regional variants of a fictitious global sign language called 'sgn'. Of course, there is no global sign language, so sign languages now have their own language codes. >>> Language.get('sgn-US') Language.make(language='ase') >>> Language.get('sgn-US', normalize=False) Language.make(language='sgn', region='US') 'en-gb-oed' is a tag that's grandfathered into the standard because it has been used to mean "spell-check this with Oxford English Dictionary spelling", but that tag has the wrong shape. We interpret this as the new standardized tag 'en-gb-oxendict', unless asked not to normalize. >>> Language.get('en-gb-oed') Language.make(language='en', region='GB', variants=['oxendict']) >>> Language.get('en-gb-oed', normalize=False) Language.make(language='en-gb-oed') 'zh-min-nan' is another oddly-formed tag, used to represent the Southern Min language, which includes Taiwanese as a regional form. It now has its own language code. >>> Language.get('zh-min-nan') Language.make(language='nan') There's not much we can do with the vague tag 'zh-min': >>> Language.get('zh-min') Language.make(language='zh-min') Occasionally Wiktionary will use 'extlang' tags in strange ways, such as using the tag 'und-ibe' for some unspecified Iberian language. >>> Language.get('und-ibe') Language.make(extlangs=['ibe']) Here's an example of replacing multiple deprecated tags. The language tag 'sh' (Serbo-Croatian) ended up being politically problematic, and different standards took different steps to address this. The IANA made it into a macrolanguage that contains 'sr', 'hr', and 'bs'. Unicode further decided that it's a legacy tag that should be interpreted as 'sr-Latn', which the language matching rules say is mutually intelligible with all those languages. We complicate the example by adding on the region tag 'QU', an old provisional tag for the European Union, which is now standardized as 'EU'. >>> Language.get('sh-QU') Language.make(language='sr', script='Latn', region='EU')
[ "Create", "a", "Language", "object", "from", "a", "language", "tag", "string", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L125-L290
7,954
LuminosoInsight/langcodes
langcodes/__init__.py
Language.simplify_script
def simplify_script(self) -> 'Language': """ Remove the script from some parsed language data, if the script is redundant with the language. >>> Language.make(language='en', script='Latn').simplify_script() Language.make(language='en') >>> Language.make(language='yi', script='Latn').simplify_script() Language.make(language='yi', script='Latn') >>> Language.make(language='yi', script='Hebr').simplify_script() Language.make(language='yi') """ if self._simplified is not None: return self._simplified if self.language and self.script: if DEFAULT_SCRIPTS.get(self.language) == self.script: result = self.update_dict({'script': None}) self._simplified = result return self._simplified self._simplified = self return self._simplified
python
def simplify_script(self) -> 'Language': if self._simplified is not None: return self._simplified if self.language and self.script: if DEFAULT_SCRIPTS.get(self.language) == self.script: result = self.update_dict({'script': None}) self._simplified = result return self._simplified self._simplified = self return self._simplified
[ "def", "simplify_script", "(", "self", ")", "->", "'Language'", ":", "if", "self", ".", "_simplified", "is", "not", "None", ":", "return", "self", ".", "_simplified", "if", "self", ".", "language", "and", "self", ".", "script", ":", "if", "DEFAULT_SCRIPTS"...
Remove the script from some parsed language data, if the script is redundant with the language. >>> Language.make(language='en', script='Latn').simplify_script() Language.make(language='en') >>> Language.make(language='yi', script='Latn').simplify_script() Language.make(language='yi', script='Latn') >>> Language.make(language='yi', script='Hebr').simplify_script() Language.make(language='yi')
[ "Remove", "the", "script", "from", "some", "parsed", "language", "data", "if", "the", "script", "is", "redundant", "with", "the", "language", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L332-L356
7,955
LuminosoInsight/langcodes
langcodes/__init__.py
Language.assume_script
def assume_script(self) -> 'Language': """ Fill in the script if it's missing, and if it can be assumed from the language subtag. This is the opposite of `simplify_script`. >>> Language.make(language='en').assume_script() Language.make(language='en', script='Latn') >>> Language.make(language='yi').assume_script() Language.make(language='yi', script='Hebr') >>> Language.make(language='yi', script='Latn').assume_script() Language.make(language='yi', script='Latn') This fills in nothing when the script cannot be assumed -- such as when the language has multiple scripts, or it has no standard orthography: >>> Language.make(language='sr').assume_script() Language.make(language='sr') >>> Language.make(language='eee').assume_script() Language.make(language='eee') It also dosn't fill anything in when the language is unspecified. >>> Language.make(region='US').assume_script() Language.make(region='US') """ if self._assumed is not None: return self._assumed if self.language and not self.script: try: self._assumed = self.update_dict({'script': DEFAULT_SCRIPTS[self.language]}) except KeyError: self._assumed = self else: self._assumed = self return self._assumed
python
def assume_script(self) -> 'Language': if self._assumed is not None: return self._assumed if self.language and not self.script: try: self._assumed = self.update_dict({'script': DEFAULT_SCRIPTS[self.language]}) except KeyError: self._assumed = self else: self._assumed = self return self._assumed
[ "def", "assume_script", "(", "self", ")", "->", "'Language'", ":", "if", "self", ".", "_assumed", "is", "not", "None", ":", "return", "self", ".", "_assumed", "if", "self", ".", "language", "and", "not", "self", ".", "script", ":", "try", ":", "self", ...
Fill in the script if it's missing, and if it can be assumed from the language subtag. This is the opposite of `simplify_script`. >>> Language.make(language='en').assume_script() Language.make(language='en', script='Latn') >>> Language.make(language='yi').assume_script() Language.make(language='yi', script='Hebr') >>> Language.make(language='yi', script='Latn').assume_script() Language.make(language='yi', script='Latn') This fills in nothing when the script cannot be assumed -- such as when the language has multiple scripts, or it has no standard orthography: >>> Language.make(language='sr').assume_script() Language.make(language='sr') >>> Language.make(language='eee').assume_script() Language.make(language='eee') It also dosn't fill anything in when the language is unspecified. >>> Language.make(region='US').assume_script() Language.make(region='US')
[ "Fill", "in", "the", "script", "if", "it", "s", "missing", "and", "if", "it", "can", "be", "assumed", "from", "the", "language", "subtag", ".", "This", "is", "the", "opposite", "of", "simplify_script", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L358-L395
7,956
LuminosoInsight/langcodes
langcodes/__init__.py
Language.prefer_macrolanguage
def prefer_macrolanguage(self) -> 'Language': """ BCP 47 doesn't specify what to do with macrolanguages and the languages they contain. The Unicode CLDR, on the other hand, says that when a macrolanguage has a dominant standardized language, the macrolanguage code should be used for that language. For example, Mandarin Chinese is 'zh', not 'cmn', according to Unicode, and Malay is 'ms', not 'zsm'. This isn't a rule you'd want to follow in all cases -- for example, you may want to be able to specifically say that 'ms' (the Malay macrolanguage) contains both 'zsm' (Standard Malay) and 'id' (Indonesian). But applying this rule helps when interoperating with the Unicode CLDR. So, applying `prefer_macrolanguage` to a Language object will return a new object, replacing the language with the macrolanguage if it is the dominant language within that macrolanguage. It will leave non-dominant languages that have macrolanguages alone. >>> Language.get('arb').prefer_macrolanguage() Language.make(language='ar') >>> Language.get('cmn-Hant').prefer_macrolanguage() Language.make(language='zh', script='Hant') >>> Language.get('yue-Hant').prefer_macrolanguage() Language.make(language='yue', script='Hant') """ if self._macrolanguage is not None: return self._macrolanguage language = self.language or 'und' if language in NORMALIZED_MACROLANGUAGES: self._macrolanguage = self.update_dict({ 'language': NORMALIZED_MACROLANGUAGES[language] }) else: self._macrolanguage = self return self._macrolanguage
python
def prefer_macrolanguage(self) -> 'Language': if self._macrolanguage is not None: return self._macrolanguage language = self.language or 'und' if language in NORMALIZED_MACROLANGUAGES: self._macrolanguage = self.update_dict({ 'language': NORMALIZED_MACROLANGUAGES[language] }) else: self._macrolanguage = self return self._macrolanguage
[ "def", "prefer_macrolanguage", "(", "self", ")", "->", "'Language'", ":", "if", "self", ".", "_macrolanguage", "is", "not", "None", ":", "return", "self", ".", "_macrolanguage", "language", "=", "self", ".", "language", "or", "'und'", "if", "language", "in",...
BCP 47 doesn't specify what to do with macrolanguages and the languages they contain. The Unicode CLDR, on the other hand, says that when a macrolanguage has a dominant standardized language, the macrolanguage code should be used for that language. For example, Mandarin Chinese is 'zh', not 'cmn', according to Unicode, and Malay is 'ms', not 'zsm'. This isn't a rule you'd want to follow in all cases -- for example, you may want to be able to specifically say that 'ms' (the Malay macrolanguage) contains both 'zsm' (Standard Malay) and 'id' (Indonesian). But applying this rule helps when interoperating with the Unicode CLDR. So, applying `prefer_macrolanguage` to a Language object will return a new object, replacing the language with the macrolanguage if it is the dominant language within that macrolanguage. It will leave non-dominant languages that have macrolanguages alone. >>> Language.get('arb').prefer_macrolanguage() Language.make(language='ar') >>> Language.get('cmn-Hant').prefer_macrolanguage() Language.make(language='zh', script='Hant') >>> Language.get('yue-Hant').prefer_macrolanguage() Language.make(language='yue', script='Hant')
[ "BCP", "47", "doesn", "t", "specify", "what", "to", "do", "with", "macrolanguages", "and", "the", "languages", "they", "contain", ".", "The", "Unicode", "CLDR", "on", "the", "other", "hand", "says", "that", "when", "a", "macrolanguage", "has", "a", "domina...
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L397-L433
7,957
LuminosoInsight/langcodes
langcodes/__init__.py
Language.broaden
def broaden(self) -> 'List[Language]': """ Iterate through increasingly general versions of this parsed language tag. This isn't actually that useful for matching two arbitrary language tags against each other, but it is useful for matching them against a known standardized form, such as in the CLDR data. The list of broader versions to try appears in UTR 35, section 4.3, "Likely Subtags". >>> for langdata in Language.get('nn-Latn-NO-x-thingy').broaden(): ... print(langdata) nn-Latn-NO-x-thingy nn-Latn-NO nn-NO nn-Latn nn und-Latn und """ if self._broader is not None: return self._broader self._broader = [self] seen = set(self.to_tag()) for keyset in self.BROADER_KEYSETS: filtered = self._filter_attributes(keyset) tag = filtered.to_tag() if tag not in seen: self._broader.append(filtered) seen.add(tag) return self._broader
python
def broaden(self) -> 'List[Language]': if self._broader is not None: return self._broader self._broader = [self] seen = set(self.to_tag()) for keyset in self.BROADER_KEYSETS: filtered = self._filter_attributes(keyset) tag = filtered.to_tag() if tag not in seen: self._broader.append(filtered) seen.add(tag) return self._broader
[ "def", "broaden", "(", "self", ")", "->", "'List[Language]'", ":", "if", "self", ".", "_broader", "is", "not", "None", ":", "return", "self", ".", "_broader", "self", ".", "_broader", "=", "[", "self", "]", "seen", "=", "set", "(", "self", ".", "to_t...
Iterate through increasingly general versions of this parsed language tag. This isn't actually that useful for matching two arbitrary language tags against each other, but it is useful for matching them against a known standardized form, such as in the CLDR data. The list of broader versions to try appears in UTR 35, section 4.3, "Likely Subtags". >>> for langdata in Language.get('nn-Latn-NO-x-thingy').broaden(): ... print(langdata) nn-Latn-NO-x-thingy nn-Latn-NO nn-NO nn-Latn nn und-Latn und
[ "Iterate", "through", "increasingly", "general", "versions", "of", "this", "parsed", "language", "tag", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L435-L466
7,958
LuminosoInsight/langcodes
langcodes/__init__.py
Language.maximize
def maximize(self) -> 'Language': """ The Unicode CLDR contains a "likelySubtags" data file, which can guess reasonable values for fields that are missing from a language tag. This is particularly useful for comparing, for example, "zh-Hant" and "zh-TW", two common language tags that say approximately the same thing via rather different information. (Using traditional Han characters is not the same as being in Taiwan, but each implies that the other is likely.) These implications are provided in the CLDR supplemental data, and are based on the likelihood of people using the language to transmit information on the Internet. (This is why the overall default is English, not Chinese.) >>> str(Language.get('zh-Hant').maximize()) 'zh-Hant-TW' >>> str(Language.get('zh-TW').maximize()) 'zh-Hant-TW' >>> str(Language.get('ja').maximize()) 'ja-Jpan-JP' >>> str(Language.get('pt').maximize()) 'pt-Latn-BR' >>> str(Language.get('und-Arab').maximize()) 'ar-Arab-EG' >>> str(Language.get('und-CH').maximize()) 'de-Latn-CH' >>> str(Language.make().maximize()) # 'MURICA. 'en-Latn-US' >>> str(Language.get('und-ibe').maximize()) 'en-ibe-Latn-US' """ if self._filled is not None: return self._filled for broader in self.broaden(): tag = broader.to_tag() if tag in LIKELY_SUBTAGS: result = Language.get(LIKELY_SUBTAGS[tag], normalize=False) result = result.update(self) self._filled = result return result raise RuntimeError( "Couldn't fill in likely values. This represents a problem with " "the LIKELY_SUBTAGS data." )
python
def maximize(self) -> 'Language': if self._filled is not None: return self._filled for broader in self.broaden(): tag = broader.to_tag() if tag in LIKELY_SUBTAGS: result = Language.get(LIKELY_SUBTAGS[tag], normalize=False) result = result.update(self) self._filled = result return result raise RuntimeError( "Couldn't fill in likely values. This represents a problem with " "the LIKELY_SUBTAGS data." )
[ "def", "maximize", "(", "self", ")", "->", "'Language'", ":", "if", "self", ".", "_filled", "is", "not", "None", ":", "return", "self", ".", "_filled", "for", "broader", "in", "self", ".", "broaden", "(", ")", ":", "tag", "=", "broader", ".", "to_tag...
The Unicode CLDR contains a "likelySubtags" data file, which can guess reasonable values for fields that are missing from a language tag. This is particularly useful for comparing, for example, "zh-Hant" and "zh-TW", two common language tags that say approximately the same thing via rather different information. (Using traditional Han characters is not the same as being in Taiwan, but each implies that the other is likely.) These implications are provided in the CLDR supplemental data, and are based on the likelihood of people using the language to transmit information on the Internet. (This is why the overall default is English, not Chinese.) >>> str(Language.get('zh-Hant').maximize()) 'zh-Hant-TW' >>> str(Language.get('zh-TW').maximize()) 'zh-Hant-TW' >>> str(Language.get('ja').maximize()) 'ja-Jpan-JP' >>> str(Language.get('pt').maximize()) 'pt-Latn-BR' >>> str(Language.get('und-Arab').maximize()) 'ar-Arab-EG' >>> str(Language.get('und-CH').maximize()) 'de-Latn-CH' >>> str(Language.make().maximize()) # 'MURICA. 'en-Latn-US' >>> str(Language.get('und-ibe').maximize()) 'en-ibe-Latn-US'
[ "The", "Unicode", "CLDR", "contains", "a", "likelySubtags", "data", "file", "which", "can", "guess", "reasonable", "values", "for", "fields", "that", "are", "missing", "from", "a", "language", "tag", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L477-L524
7,959
LuminosoInsight/langcodes
langcodes/__init__.py
Language.script_name
def script_name(self, language=DEFAULT_LANGUAGE, min_score: int=75) -> str: """ Describe the script part of the language tag in a natural language. """ return self._get_name('script', language, min_score)
python
def script_name(self, language=DEFAULT_LANGUAGE, min_score: int=75) -> str: return self._get_name('script', language, min_score)
[ "def", "script_name", "(", "self", ",", "language", "=", "DEFAULT_LANGUAGE", ",", "min_score", ":", "int", "=", "75", ")", "->", "str", ":", "return", "self", ".", "_get_name", "(", "'script'", ",", "language", ",", "min_score", ")" ]
Describe the script part of the language tag in a natural language.
[ "Describe", "the", "script", "part", "of", "the", "language", "tag", "in", "a", "natural", "language", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L639-L643
7,960
LuminosoInsight/langcodes
langcodes/__init__.py
Language.region_name
def region_name(self, language=DEFAULT_LANGUAGE, min_score: int=75) -> str: """ Describe the region part of the language tag in a natural language. """ return self._get_name('region', language, min_score)
python
def region_name(self, language=DEFAULT_LANGUAGE, min_score: int=75) -> str: return self._get_name('region', language, min_score)
[ "def", "region_name", "(", "self", ",", "language", "=", "DEFAULT_LANGUAGE", ",", "min_score", ":", "int", "=", "75", ")", "->", "str", ":", "return", "self", ".", "_get_name", "(", "'region'", ",", "language", ",", "min_score", ")" ]
Describe the region part of the language tag in a natural language.
[ "Describe", "the", "region", "part", "of", "the", "language", "tag", "in", "a", "natural", "language", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L645-L649
7,961
LuminosoInsight/langcodes
langcodes/__init__.py
Language.variant_names
def variant_names(self, language=DEFAULT_LANGUAGE, min_score: int=75) -> list: """ Describe each of the variant parts of the language tag in a natural language. """ names = [] for variant in self.variants: var_names = code_to_names('variant', variant) names.append(self._best_name(var_names, language, min_score)) return names
python
def variant_names(self, language=DEFAULT_LANGUAGE, min_score: int=75) -> list: names = [] for variant in self.variants: var_names = code_to_names('variant', variant) names.append(self._best_name(var_names, language, min_score)) return names
[ "def", "variant_names", "(", "self", ",", "language", "=", "DEFAULT_LANGUAGE", ",", "min_score", ":", "int", "=", "75", ")", "->", "list", ":", "names", "=", "[", "]", "for", "variant", "in", "self", ".", "variants", ":", "var_names", "=", "code_to_names...
Describe each of the variant parts of the language tag in a natural language.
[ "Describe", "each", "of", "the", "variant", "parts", "of", "the", "language", "tag", "in", "a", "natural", "language", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L651-L660
7,962
LuminosoInsight/langcodes
langcodes/__init__.py
Language.describe
def describe(self, language=DEFAULT_LANGUAGE, min_score: int=75) -> dict: """ Return a dictionary that describes a given language tag in a specified natural language. See `language_name` and related methods for more specific versions of this. The desired `language` will in fact be matched against the available options using the matching technique that this module provides. We can illustrate many aspects of this by asking for a description of Shavian script (a script devised by author George Bernard Shaw), and where you might find it, in various languages. >>> from pprint import pprint >>> shaw = Language.make(script='Shaw').maximize() >>> pprint(shaw.describe('en')) {'language': 'English', 'region': 'United Kingdom', 'script': 'Shavian'} >>> pprint(shaw.describe('fr')) {'language': 'anglais', 'region': 'Royaume-Uni', 'script': 'shavien'} >>> pprint(shaw.describe('es')) {'language': 'inglés', 'region': 'Reino Unido', 'script': 'shaviano'} >>> pprint(shaw.describe('pt')) {'language': 'inglês', 'region': 'Reino Unido', 'script': 'shaviano'} >>> pprint(shaw.describe('uk')) {'language': 'англійська', 'region': 'Велика Британія', 'script': 'шоу'} >>> pprint(shaw.describe('arb')) {'language': 'الإنجليزية', 'region': 'المملكة المتحدة', 'script': 'الشواني'} >>> pprint(shaw.describe('th')) {'language': 'อังกฤษ', 'region': 'สหราชอาณาจักร', 'script': 'ซอเวียน'} >>> pprint(shaw.describe('zh-Hans')) {'language': '英语', 'region': '英国', 'script': '萧伯纳式文'} >>> pprint(shaw.describe('zh-Hant')) {'language': '英文', 'region': '英國', 'script': '簫柏納字符'} >>> pprint(shaw.describe('ja')) {'language': '英語', 'region': 'イギリス', 'script': 'ショー文字'} When we don't have a localization for the language, we fall back on 'und', which just shows the language codes. >>> pprint(shaw.describe('lol')) {'language': 'en', 'region': 'GB', 'script': 'Shaw'} Wait, is that a real language? >>> pprint(Language.get('lol').maximize().describe()) {'language': 'Mongo', 'region': 'Congo - Kinshasa', 'script': 'Latin'} """ names = {} if self.language: names['language'] = self.language_name(language, min_score) if self.script: names['script'] = self.script_name(language, min_score) if self.region: names['region'] = self.region_name(language, min_score) if self.variants: names['variants'] = self.variant_names(language, min_score) return names
python
def describe(self, language=DEFAULT_LANGUAGE, min_score: int=75) -> dict: names = {} if self.language: names['language'] = self.language_name(language, min_score) if self.script: names['script'] = self.script_name(language, min_score) if self.region: names['region'] = self.region_name(language, min_score) if self.variants: names['variants'] = self.variant_names(language, min_score) return names
[ "def", "describe", "(", "self", ",", "language", "=", "DEFAULT_LANGUAGE", ",", "min_score", ":", "int", "=", "75", ")", "->", "dict", ":", "names", "=", "{", "}", "if", "self", ".", "language", ":", "names", "[", "'language'", "]", "=", "self", ".", ...
Return a dictionary that describes a given language tag in a specified natural language. See `language_name` and related methods for more specific versions of this. The desired `language` will in fact be matched against the available options using the matching technique that this module provides. We can illustrate many aspects of this by asking for a description of Shavian script (a script devised by author George Bernard Shaw), and where you might find it, in various languages. >>> from pprint import pprint >>> shaw = Language.make(script='Shaw').maximize() >>> pprint(shaw.describe('en')) {'language': 'English', 'region': 'United Kingdom', 'script': 'Shavian'} >>> pprint(shaw.describe('fr')) {'language': 'anglais', 'region': 'Royaume-Uni', 'script': 'shavien'} >>> pprint(shaw.describe('es')) {'language': 'inglés', 'region': 'Reino Unido', 'script': 'shaviano'} >>> pprint(shaw.describe('pt')) {'language': 'inglês', 'region': 'Reino Unido', 'script': 'shaviano'} >>> pprint(shaw.describe('uk')) {'language': 'англійська', 'region': 'Велика Британія', 'script': 'шоу'} >>> pprint(shaw.describe('arb')) {'language': 'الإنجليزية', 'region': 'المملكة المتحدة', 'script': 'الشواني'} >>> pprint(shaw.describe('th')) {'language': 'อังกฤษ', 'region': 'สหราชอาณาจักร', 'script': 'ซอเวียน'} >>> pprint(shaw.describe('zh-Hans')) {'language': '英语', 'region': '英国', 'script': '萧伯纳式文'} >>> pprint(shaw.describe('zh-Hant')) {'language': '英文', 'region': '英國', 'script': '簫柏納字符'} >>> pprint(shaw.describe('ja')) {'language': '英語', 'region': 'イギリス', 'script': 'ショー文字'} When we don't have a localization for the language, we fall back on 'und', which just shows the language codes. >>> pprint(shaw.describe('lol')) {'language': 'en', 'region': 'GB', 'script': 'Shaw'} Wait, is that a real language? >>> pprint(Language.get('lol').maximize().describe()) {'language': 'Mongo', 'region': 'Congo - Kinshasa', 'script': 'Latin'}
[ "Return", "a", "dictionary", "that", "describes", "a", "given", "language", "tag", "in", "a", "specified", "natural", "language", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L662-L727
7,963
LuminosoInsight/langcodes
langcodes/__init__.py
Language.find_name
def find_name(tagtype: str, name: str, language: {str, 'Language', None}=None): """ Find the subtag of a particular `tagtype` that has the given `name`. The default language, "und", will allow matching names in any language, so you can get the code 'fr' by looking up "French", "Français", or "francés". Occasionally, names are ambiguous in a way that can be resolved by specifying what name the language is supposed to be in. For example, there is a language named 'Malayo' in English, but it's different from the language named 'Malayo' in Spanish (which is Malay). Specifying the language will look up the name in a trie that is only in that language. In a previous version, we thought we were going to deprecate the `language` parameter, as there weren't significant cases of conflicts in names of things between languages. Well, we got more data, and conflicts in names are everywhere. Specifying the language that the name should be in is still not required, but it will help to make sure that names can be round-tripped. >>> Language.find_name('language', 'francés') Language.make(language='fr') >>> Language.find_name('region', 'United Kingdom') Language.make(region='GB') >>> Language.find_name('script', 'Arabic') Language.make(script='Arab') >>> Language.find_name('language', 'norsk bokmål') Language.make(language='nb') >>> Language.find_name('language', 'norsk') Language.make(language='no') >>> Language.find_name('language', 'norsk', 'en') Traceback (most recent call last): ... LookupError: Can't find any language named 'norsk' >>> Language.find_name('language', 'norsk', 'no') Language.make(language='no') >>> Language.find_name('language', 'malayo', 'en') Language.make(language='mbp') >>> Language.find_name('language', 'malayo', 'es') Language.make(language='ms') Some langauge names resolve to more than a language. For example, the name 'Brazilian Portuguese' resolves to a language and a region, and 'Simplified Chinese' resolves to a language and a script. In these cases, a Language object with multiple subtags will be returned. >>> Language.find_name('language', 'Brazilian Portuguese', 'en') Language.make(language='pt', region='BR') >>> Language.find_name('language', 'Simplified Chinese', 'en') Language.make(language='zh', script='Hans') A small amount of fuzzy matching is supported: if the name can be shortened to match a single language name, you get that language. This allows, for example, "Hakka dialect" to match "Hakka". >>> Language.find_name('language', 'Hakka dialect') Language.make(language='hak') """ # No matter what form of language we got, normalize it to a single # language subtag if isinstance(language, Language): language = language.language elif isinstance(language, str): language = get(language).language if language is None: language = 'und' code = name_to_code(tagtype, name, language) if code is None: raise LookupError("Can't find any %s named %r" % (tagtype, name)) if '-' in code: return Language.get(code) else: data = {tagtype: code} return Language.make(**data)
python
def find_name(tagtype: str, name: str, language: {str, 'Language', None}=None): # No matter what form of language we got, normalize it to a single # language subtag if isinstance(language, Language): language = language.language elif isinstance(language, str): language = get(language).language if language is None: language = 'und' code = name_to_code(tagtype, name, language) if code is None: raise LookupError("Can't find any %s named %r" % (tagtype, name)) if '-' in code: return Language.get(code) else: data = {tagtype: code} return Language.make(**data)
[ "def", "find_name", "(", "tagtype", ":", "str", ",", "name", ":", "str", ",", "language", ":", "{", "str", ",", "'Language'", ",", "None", "}", "=", "None", ")", ":", "# No matter what form of language we got, normalize it to a single", "# language subtag", "if", ...
Find the subtag of a particular `tagtype` that has the given `name`. The default language, "und", will allow matching names in any language, so you can get the code 'fr' by looking up "French", "Français", or "francés". Occasionally, names are ambiguous in a way that can be resolved by specifying what name the language is supposed to be in. For example, there is a language named 'Malayo' in English, but it's different from the language named 'Malayo' in Spanish (which is Malay). Specifying the language will look up the name in a trie that is only in that language. In a previous version, we thought we were going to deprecate the `language` parameter, as there weren't significant cases of conflicts in names of things between languages. Well, we got more data, and conflicts in names are everywhere. Specifying the language that the name should be in is still not required, but it will help to make sure that names can be round-tripped. >>> Language.find_name('language', 'francés') Language.make(language='fr') >>> Language.find_name('region', 'United Kingdom') Language.make(region='GB') >>> Language.find_name('script', 'Arabic') Language.make(script='Arab') >>> Language.find_name('language', 'norsk bokmål') Language.make(language='nb') >>> Language.find_name('language', 'norsk') Language.make(language='no') >>> Language.find_name('language', 'norsk', 'en') Traceback (most recent call last): ... LookupError: Can't find any language named 'norsk' >>> Language.find_name('language', 'norsk', 'no') Language.make(language='no') >>> Language.find_name('language', 'malayo', 'en') Language.make(language='mbp') >>> Language.find_name('language', 'malayo', 'es') Language.make(language='ms') Some langauge names resolve to more than a language. For example, the name 'Brazilian Portuguese' resolves to a language and a region, and 'Simplified Chinese' resolves to a language and a script. In these cases, a Language object with multiple subtags will be returned. >>> Language.find_name('language', 'Brazilian Portuguese', 'en') Language.make(language='pt', region='BR') >>> Language.find_name('language', 'Simplified Chinese', 'en') Language.make(language='zh', script='Hans') A small amount of fuzzy matching is supported: if the name can be shortened to match a single language name, you get that language. This allows, for example, "Hakka dialect" to match "Hakka". >>> Language.find_name('language', 'Hakka dialect') Language.make(language='hak')
[ "Find", "the", "subtag", "of", "a", "particular", "tagtype", "that", "has", "the", "given", "name", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L730-L817
7,964
LuminosoInsight/langcodes
langcodes/__init__.py
Language.to_dict
def to_dict(self): """ Get a dictionary of the attributes of this Language object, which can be useful for constructing a similar object. """ if self._dict is not None: return self._dict result = {} for key in self.ATTRIBUTES: value = getattr(self, key) if value: result[key] = value self._dict = result return result
python
def to_dict(self): if self._dict is not None: return self._dict result = {} for key in self.ATTRIBUTES: value = getattr(self, key) if value: result[key] = value self._dict = result return result
[ "def", "to_dict", "(", "self", ")", ":", "if", "self", ".", "_dict", "is", "not", "None", ":", "return", "self", ".", "_dict", "result", "=", "{", "}", "for", "key", "in", "self", ".", "ATTRIBUTES", ":", "value", "=", "getattr", "(", "self", ",", ...
Get a dictionary of the attributes of this Language object, which can be useful for constructing a similar object.
[ "Get", "a", "dictionary", "of", "the", "attributes", "of", "this", "Language", "object", "which", "can", "be", "useful", "for", "constructing", "a", "similar", "object", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L845-L859
7,965
LuminosoInsight/langcodes
langcodes/__init__.py
Language.update
def update(self, other: 'Language') -> 'Language': """ Update this Language with the fields of another Language. """ return Language.make( language=other.language or self.language, extlangs=other.extlangs or self.extlangs, script=other.script or self.script, region=other.region or self.region, variants=other.variants or self.variants, extensions=other.extensions or self.extensions, private=other.private or self.private )
python
def update(self, other: 'Language') -> 'Language': return Language.make( language=other.language or self.language, extlangs=other.extlangs or self.extlangs, script=other.script or self.script, region=other.region or self.region, variants=other.variants or self.variants, extensions=other.extensions or self.extensions, private=other.private or self.private )
[ "def", "update", "(", "self", ",", "other", ":", "'Language'", ")", "->", "'Language'", ":", "return", "Language", ".", "make", "(", "language", "=", "other", ".", "language", "or", "self", ".", "language", ",", "extlangs", "=", "other", ".", "extlangs",...
Update this Language with the fields of another Language.
[ "Update", "this", "Language", "with", "the", "fields", "of", "another", "Language", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L861-L873
7,966
LuminosoInsight/langcodes
langcodes/__init__.py
Language.update_dict
def update_dict(self, newdata: dict) -> 'Language': """ Update the attributes of this Language from a dictionary. """ return Language.make( language=newdata.get('language', self.language), extlangs=newdata.get('extlangs', self.extlangs), script=newdata.get('script', self.script), region=newdata.get('region', self.region), variants=newdata.get('variants', self.variants), extensions=newdata.get('extensions', self.extensions), private=newdata.get('private', self.private) )
python
def update_dict(self, newdata: dict) -> 'Language': return Language.make( language=newdata.get('language', self.language), extlangs=newdata.get('extlangs', self.extlangs), script=newdata.get('script', self.script), region=newdata.get('region', self.region), variants=newdata.get('variants', self.variants), extensions=newdata.get('extensions', self.extensions), private=newdata.get('private', self.private) )
[ "def", "update_dict", "(", "self", ",", "newdata", ":", "dict", ")", "->", "'Language'", ":", "return", "Language", ".", "make", "(", "language", "=", "newdata", ".", "get", "(", "'language'", ",", "self", ".", "language", ")", ",", "extlangs", "=", "n...
Update the attributes of this Language from a dictionary.
[ "Update", "the", "attributes", "of", "this", "Language", "from", "a", "dictionary", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L875-L887
7,967
LuminosoInsight/langcodes
langcodes/__init__.py
Language._filter_keys
def _filter_keys(d: dict, keys: set) -> dict: """ Select a subset of keys from a dictionary. """ return {key: d[key] for key in keys if key in d}
python
def _filter_keys(d: dict, keys: set) -> dict: return {key: d[key] for key in keys if key in d}
[ "def", "_filter_keys", "(", "d", ":", "dict", ",", "keys", ":", "set", ")", "->", "dict", ":", "return", "{", "key", ":", "d", "[", "key", "]", "for", "key", "in", "keys", "if", "key", "in", "d", "}" ]
Select a subset of keys from a dictionary.
[ "Select", "a", "subset", "of", "keys", "from", "a", "dictionary", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L890-L894
7,968
LuminosoInsight/langcodes
langcodes/__init__.py
Language._filter_attributes
def _filter_attributes(self, keyset): """ Return a copy of this object with a subset of its attributes set. """ filtered = self._filter_keys(self.to_dict(), keyset) return Language.make(**filtered)
python
def _filter_attributes(self, keyset): filtered = self._filter_keys(self.to_dict(), keyset) return Language.make(**filtered)
[ "def", "_filter_attributes", "(", "self", ",", "keyset", ")", ":", "filtered", "=", "self", ".", "_filter_keys", "(", "self", ".", "to_dict", "(", ")", ",", "keyset", ")", "return", "Language", ".", "make", "(", "*", "*", "filtered", ")" ]
Return a copy of this object with a subset of its attributes set.
[ "Return", "a", "copy", "of", "this", "object", "with", "a", "subset", "of", "its", "attributes", "set", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L896-L901
7,969
LuminosoInsight/langcodes
langcodes/__init__.py
Language._searchable_form
def _searchable_form(self) -> 'Language': """ Convert a parsed language tag so that the information it contains is in the best form for looking up information in the CLDR. """ if self._searchable is not None: return self._searchable self._searchable = self._filter_attributes( {'language', 'script', 'region'} ).simplify_script().prefer_macrolanguage() return self._searchable
python
def _searchable_form(self) -> 'Language': if self._searchable is not None: return self._searchable self._searchable = self._filter_attributes( {'language', 'script', 'region'} ).simplify_script().prefer_macrolanguage() return self._searchable
[ "def", "_searchable_form", "(", "self", ")", "->", "'Language'", ":", "if", "self", ".", "_searchable", "is", "not", "None", ":", "return", "self", ".", "_searchable", "self", ".", "_searchable", "=", "self", ".", "_filter_attributes", "(", "{", "'language'"...
Convert a parsed language tag so that the information it contains is in the best form for looking up information in the CLDR.
[ "Convert", "a", "parsed", "language", "tag", "so", "that", "the", "information", "it", "contains", "is", "in", "the", "best", "form", "for", "looking", "up", "information", "in", "the", "CLDR", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/__init__.py#L903-L914
7,970
LuminosoInsight/langcodes
langcodes/build_data.py
read_cldr_names
def read_cldr_names(path, language, category): """ Read CLDR's names for things in a particular language. """ filename = data_filename('{}/{}/{}.json'.format(path, language, category)) fulldata = json.load(open(filename, encoding='utf-8')) data = fulldata['main'][language]['localeDisplayNames'][category] return data
python
def read_cldr_names(path, language, category): filename = data_filename('{}/{}/{}.json'.format(path, language, category)) fulldata = json.load(open(filename, encoding='utf-8')) data = fulldata['main'][language]['localeDisplayNames'][category] return data
[ "def", "read_cldr_names", "(", "path", ",", "language", ",", "category", ")", ":", "filename", "=", "data_filename", "(", "'{}/{}/{}.json'", ".", "format", "(", "path", ",", "language", ",", "category", ")", ")", "fulldata", "=", "json", ".", "load", "(", ...
Read CLDR's names for things in a particular language.
[ "Read", "CLDR", "s", "names", "for", "things", "in", "a", "particular", "language", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/build_data.py#L192-L199
7,971
LuminosoInsight/langcodes
langcodes/registry_parser.py
parse_file
def parse_file(file): """ Take an open file containing the IANA subtag registry, and yield a dictionary of information for each subtag it describes. """ lines = [] for line in file: line = line.rstrip('\n') if line == '%%': # This is a separator between items. Parse the data we've # collected and yield the result. yield from parse_item(lines) lines.clear() elif line.startswith(' '): # This is a continuation line. Concatenate it to the previous # line, including one of the spaces. lines[-1] += line[1:] else: lines.append(line) yield from parse_item(lines)
python
def parse_file(file): lines = [] for line in file: line = line.rstrip('\n') if line == '%%': # This is a separator between items. Parse the data we've # collected and yield the result. yield from parse_item(lines) lines.clear() elif line.startswith(' '): # This is a continuation line. Concatenate it to the previous # line, including one of the spaces. lines[-1] += line[1:] else: lines.append(line) yield from parse_item(lines)
[ "def", "parse_file", "(", "file", ")", ":", "lines", "=", "[", "]", "for", "line", "in", "file", ":", "line", "=", "line", ".", "rstrip", "(", "'\\n'", ")", "if", "line", "==", "'%%'", ":", "# This is a separator between items. Parse the data we've", "# coll...
Take an open file containing the IANA subtag registry, and yield a dictionary of information for each subtag it describes.
[ "Take", "an", "open", "file", "containing", "the", "IANA", "subtag", "registry", "and", "yield", "a", "dictionary", "of", "information", "for", "each", "subtag", "it", "describes", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/registry_parser.py#L6-L25
7,972
LuminosoInsight/langcodes
langcodes/names.py
load_trie
def load_trie(filename): """ Load a BytesTrie from the marisa_trie on-disk format. """ trie = marisa_trie.BytesTrie() # marisa_trie raises warnings that make no sense. Ignore them. with warnings.catch_warnings(): warnings.simplefilter("ignore") trie.load(filename) return trie
python
def load_trie(filename): trie = marisa_trie.BytesTrie() # marisa_trie raises warnings that make no sense. Ignore them. with warnings.catch_warnings(): warnings.simplefilter("ignore") trie.load(filename) return trie
[ "def", "load_trie", "(", "filename", ")", ":", "trie", "=", "marisa_trie", ".", "BytesTrie", "(", ")", "# marisa_trie raises warnings that make no sense. Ignore them.", "with", "warnings", ".", "catch_warnings", "(", ")", ":", "warnings", ".", "simplefilter", "(", "...
Load a BytesTrie from the marisa_trie on-disk format.
[ "Load", "a", "BytesTrie", "from", "the", "marisa_trie", "on", "-", "disk", "format", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/names.py#L27-L36
7,973
LuminosoInsight/langcodes
langcodes/names.py
name_to_code
def name_to_code(category, name, language: str='und'): """ Get a language, script, or region by its name in some language. The language here must be a string representing a language subtag only. The `Language.find` method can handle other representations of a language and normalize them to this form. The default language, "und", will allow matching names in any language, so you can get the code 'fr' by looking up "French", "Français", or "francés". A small amount of fuzzy matching is supported: if the name can be shortened or lengthened to match a single language name, you get that language. This allows, for example, "Hakka Chinese" to match "Hakka". Occasionally, names are ambiguous in a way that can be resolved by specifying what name the language is supposed to be in. For example, there is a language named 'Malayo' in English, but it's different from the language named 'Malayo' in Spanish (which is Malay). Specifying the language will look up the name in a trie that is only in that language. """ assert '/' not in language, "Language codes cannot contain slashes" assert '-' not in language, "This code should be reduced to a language subtag only" trie_name = '{}/name_to_{}'.format(language, category) if trie_name not in TRIES: TRIES[trie_name] = load_trie(data_filename('trie/{}.marisa'.format(trie_name))) trie = TRIES[trie_name] lookup = normalize_name(name) if lookup in trie: return get_trie_value(trie, lookup) else: # Is this a language plus extra junk? Maybe it has "...isch", "... language", # or "... Chinese" attached to it, for example. prefixes = trie.prefixes(lookup) if prefixes and len(prefixes[-1]) >= 4: return get_trie_value(trie, prefixes[-1]) else: return None
python
def name_to_code(category, name, language: str='und'): assert '/' not in language, "Language codes cannot contain slashes" assert '-' not in language, "This code should be reduced to a language subtag only" trie_name = '{}/name_to_{}'.format(language, category) if trie_name not in TRIES: TRIES[trie_name] = load_trie(data_filename('trie/{}.marisa'.format(trie_name))) trie = TRIES[trie_name] lookup = normalize_name(name) if lookup in trie: return get_trie_value(trie, lookup) else: # Is this a language plus extra junk? Maybe it has "...isch", "... language", # or "... Chinese" attached to it, for example. prefixes = trie.prefixes(lookup) if prefixes and len(prefixes[-1]) >= 4: return get_trie_value(trie, prefixes[-1]) else: return None
[ "def", "name_to_code", "(", "category", ",", "name", ",", "language", ":", "str", "=", "'und'", ")", ":", "assert", "'/'", "not", "in", "language", ",", "\"Language codes cannot contain slashes\"", "assert", "'-'", "not", "in", "language", ",", "\"This code shou...
Get a language, script, or region by its name in some language. The language here must be a string representing a language subtag only. The `Language.find` method can handle other representations of a language and normalize them to this form. The default language, "und", will allow matching names in any language, so you can get the code 'fr' by looking up "French", "Français", or "francés". A small amount of fuzzy matching is supported: if the name can be shortened or lengthened to match a single language name, you get that language. This allows, for example, "Hakka Chinese" to match "Hakka". Occasionally, names are ambiguous in a way that can be resolved by specifying what name the language is supposed to be in. For example, there is a language named 'Malayo' in English, but it's different from the language named 'Malayo' in Spanish (which is Malay). Specifying the language will look up the name in a trie that is only in that language.
[ "Get", "a", "language", "script", "or", "region", "by", "its", "name", "in", "some", "language", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/names.py#L47-L86
7,974
LuminosoInsight/langcodes
langcodes/names.py
code_to_names
def code_to_names(category, code): """ Given the code for a language, script, or region, get a dictionary of its names in various languages. """ trie_name = '{}_to_name'.format(category) if trie_name not in TRIES: TRIES[trie_name] = load_trie(data_filename('trie/{}.marisa'.format(trie_name))) trie = TRIES[trie_name] lookup = code.lower() + '@' possible_keys = trie.keys(lookup) names = {} for key in possible_keys: target_language = key.split('@')[1] names[target_language] = get_trie_value(trie, key) return names
python
def code_to_names(category, code): trie_name = '{}_to_name'.format(category) if trie_name not in TRIES: TRIES[trie_name] = load_trie(data_filename('trie/{}.marisa'.format(trie_name))) trie = TRIES[trie_name] lookup = code.lower() + '@' possible_keys = trie.keys(lookup) names = {} for key in possible_keys: target_language = key.split('@')[1] names[target_language] = get_trie_value(trie, key) return names
[ "def", "code_to_names", "(", "category", ",", "code", ")", ":", "trie_name", "=", "'{}_to_name'", ".", "format", "(", "category", ")", "if", "trie_name", "not", "in", "TRIES", ":", "TRIES", "[", "trie_name", "]", "=", "load_trie", "(", "data_filename", "("...
Given the code for a language, script, or region, get a dictionary of its names in various languages.
[ "Given", "the", "code", "for", "a", "language", "script", "or", "region", "get", "a", "dictionary", "of", "its", "names", "in", "various", "languages", "." ]
0cedf9ca257ebf7250de5d3a63ec33a7d198db58
https://github.com/LuminosoInsight/langcodes/blob/0cedf9ca257ebf7250de5d3a63ec33a7d198db58/langcodes/names.py#L89-L105
7,975
Ezhil-Language-Foundation/open-tamil
examples/tamilmorse/huffman.py
print_huffman_code_cwl
def print_huffman_code_cwl(code,p,v): """ code - code dictionary with symbol -> code map, p, v is probability map """ cwl = 0.0 for k,_v in code.items(): print(u"%s -> %s"%(k,_v)) cwl += p[v.index(k)]*len(_v) print(u"cwl = %g"%cwl) return cwl,code.values()
python
def print_huffman_code_cwl(code,p,v): cwl = 0.0 for k,_v in code.items(): print(u"%s -> %s"%(k,_v)) cwl += p[v.index(k)]*len(_v) print(u"cwl = %g"%cwl) return cwl,code.values()
[ "def", "print_huffman_code_cwl", "(", "code", ",", "p", ",", "v", ")", ":", "cwl", "=", "0.0", "for", "k", ",", "_v", "in", "code", ".", "items", "(", ")", ":", "print", "(", "u\"%s -> %s\"", "%", "(", "k", ",", "_v", ")", ")", "cwl", "+=", "p"...
code - code dictionary with symbol -> code map, p, v is probability map
[ "code", "-", "code", "dictionary", "with", "symbol", "-", ">", "code", "map", "p", "v", "is", "probability", "map" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/examples/tamilmorse/huffman.py#L77-L84
7,976
Ezhil-Language-Foundation/open-tamil
solthiruthi/typographical.py
oridam_generate_patterns
def oridam_generate_patterns(word_in,cm,ed=1,level=0,pos=0,candidates=None): """ ed = 1 by default, pos - internal variable for algorithm """ alternates = cm.get(word_in[pos],[]) if not candidates: candidates = [] assert ed <= len(word_in), 'edit distance has to be comparable to word size [ins/del not explored]' if (pos >len(word_in)) or ed == 0: return candidates pfx = '' sfx = '' curr_candidates = [] for p in range(0,pos): pfx = pfx + word_in[p] for p in range(pos+1,len(word_in)): sfx = sfx + word_in[p] for alt in alternates: word_alt = pfx + alt + sfx if not (word_alt in candidates): candidates.append( word_alt ) curr_candidates.append( word_alt ) for n_pos in range(pos,len(word_in)): # already what we have ' candidates ' of this round are edit-distance 1 for word in curr_candidates: oridam_generate_patterns(word,cm,ed-1,level+1,n_pos,candidates) if level == 0: #candidates.append(word_in) for n_pos in range(pos,len(word_in)): oridam_generate_patterns(word_in,cm,ed, level+1,n_pos,candidates) return candidates
python
def oridam_generate_patterns(word_in,cm,ed=1,level=0,pos=0,candidates=None): alternates = cm.get(word_in[pos],[]) if not candidates: candidates = [] assert ed <= len(word_in), 'edit distance has to be comparable to word size [ins/del not explored]' if (pos >len(word_in)) or ed == 0: return candidates pfx = '' sfx = '' curr_candidates = [] for p in range(0,pos): pfx = pfx + word_in[p] for p in range(pos+1,len(word_in)): sfx = sfx + word_in[p] for alt in alternates: word_alt = pfx + alt + sfx if not (word_alt in candidates): candidates.append( word_alt ) curr_candidates.append( word_alt ) for n_pos in range(pos,len(word_in)): # already what we have ' candidates ' of this round are edit-distance 1 for word in curr_candidates: oridam_generate_patterns(word,cm,ed-1,level+1,n_pos,candidates) if level == 0: #candidates.append(word_in) for n_pos in range(pos,len(word_in)): oridam_generate_patterns(word_in,cm,ed, level+1,n_pos,candidates) return candidates
[ "def", "oridam_generate_patterns", "(", "word_in", ",", "cm", ",", "ed", "=", "1", ",", "level", "=", "0", ",", "pos", "=", "0", ",", "candidates", "=", "None", ")", ":", "alternates", "=", "cm", ".", "get", "(", "word_in", "[", "pos", "]", ",", ...
ed = 1 by default, pos - internal variable for algorithm
[ "ed", "=", "1", "by", "default", "pos", "-", "internal", "variable", "for", "algorithm" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/solthiruthi/typographical.py#L20-L48
7,977
Ezhil-Language-Foundation/open-tamil
tamil/utf8.py
uyirmei_constructed
def uyirmei_constructed( mei_idx, uyir_idx): """ construct uyirmei letter give mei index and uyir index """ idx,idy = mei_idx,uyir_idx assert ( idy >= 0 and idy < uyir_len() ) assert ( idx >= 0 and idx < 6+mei_len() ) return grantha_agaram_letters[mei_idx]+accent_symbols[uyir_idx]
python
def uyirmei_constructed( mei_idx, uyir_idx): idx,idy = mei_idx,uyir_idx assert ( idy >= 0 and idy < uyir_len() ) assert ( idx >= 0 and idx < 6+mei_len() ) return grantha_agaram_letters[mei_idx]+accent_symbols[uyir_idx]
[ "def", "uyirmei_constructed", "(", "mei_idx", ",", "uyir_idx", ")", ":", "idx", ",", "idy", "=", "mei_idx", ",", "uyir_idx", "assert", "(", "idy", ">=", "0", "and", "idy", "<", "uyir_len", "(", ")", ")", "assert", "(", "idx", ">=", "0", "and", "idx",...
construct uyirmei letter give mei index and uyir index
[ "construct", "uyirmei", "letter", "give", "mei", "index", "and", "uyir", "index" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/utf8.py#L265-L270
7,978
Ezhil-Language-Foundation/open-tamil
tamil/utf8.py
has_english
def has_english( word_in ): """ return True if word_in has any English letters in the string""" return not all_tamil(word_in) and len(word_in) > 0 and any([l in word_in for l in string.ascii_letters])
python
def has_english( word_in ): return not all_tamil(word_in) and len(word_in) > 0 and any([l in word_in for l in string.ascii_letters])
[ "def", "has_english", "(", "word_in", ")", ":", "return", "not", "all_tamil", "(", "word_in", ")", "and", "len", "(", "word_in", ")", ">", "0", "and", "any", "(", "[", "l", "in", "word_in", "for", "l", "in", "string", ".", "ascii_letters", "]", ")" ]
return True if word_in has any English letters in the string
[ "return", "True", "if", "word_in", "has", "any", "English", "letters", "in", "the", "string" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/utf8.py#L305-L307
7,979
Ezhil-Language-Foundation/open-tamil
tamil/utf8.py
all_tamil
def all_tamil( word_in ): """ predicate checks if all letters of the input word are Tamil letters """ if isinstance(word_in,list): word = word_in else: word = get_letters( word_in ) return all( [(letter in tamil_letters) for letter in word] )
python
def all_tamil( word_in ): if isinstance(word_in,list): word = word_in else: word = get_letters( word_in ) return all( [(letter in tamil_letters) for letter in word] )
[ "def", "all_tamil", "(", "word_in", ")", ":", "if", "isinstance", "(", "word_in", ",", "list", ")", ":", "word", "=", "word_in", "else", ":", "word", "=", "get_letters", "(", "word_in", ")", "return", "all", "(", "[", "(", "letter", "in", "tamil_letter...
predicate checks if all letters of the input word are Tamil letters
[ "predicate", "checks", "if", "all", "letters", "of", "the", "input", "word", "are", "Tamil", "letters" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/utf8.py#L309-L315
7,980
Ezhil-Language-Foundation/open-tamil
tamil/utf8.py
compare_words_lexicographic
def compare_words_lexicographic( word_a, word_b ): """ compare words in Tamil lexicographic order """ # sanity check for words to be all Tamil if ( not all_tamil(word_a) ) or (not all_tamil(word_b)) : #print("## ") #print(word_a) #print(word_b) #print("Both operands need to be Tamil words") pass La = len(word_a) Lb = len(word_b) all_TA_letters = u"".join(tamil_letters) for itr in range(0,min(La,Lb)): pos1 = all_TA_letters.find( word_a[itr] ) pos2 = all_TA_letters.find( word_b[itr] ) if pos1 != pos2 : #print not( pos1 > pos2), pos1, pos2 return cmp(pos1, pos2) # result depends on if La is shorter than Lb, or 0 if La == Lb i.e. cmp return cmp(La,Lb)
python
def compare_words_lexicographic( word_a, word_b ): # sanity check for words to be all Tamil if ( not all_tamil(word_a) ) or (not all_tamil(word_b)) : #print("## ") #print(word_a) #print(word_b) #print("Both operands need to be Tamil words") pass La = len(word_a) Lb = len(word_b) all_TA_letters = u"".join(tamil_letters) for itr in range(0,min(La,Lb)): pos1 = all_TA_letters.find( word_a[itr] ) pos2 = all_TA_letters.find( word_b[itr] ) if pos1 != pos2 : #print not( pos1 > pos2), pos1, pos2 return cmp(pos1, pos2) # result depends on if La is shorter than Lb, or 0 if La == Lb i.e. cmp return cmp(La,Lb)
[ "def", "compare_words_lexicographic", "(", "word_a", ",", "word_b", ")", ":", "# sanity check for words to be all Tamil", "if", "(", "not", "all_tamil", "(", "word_a", ")", ")", "or", "(", "not", "all_tamil", "(", "word_b", ")", ")", ":", "#print(\"## \")", "#pr...
compare words in Tamil lexicographic order
[ "compare", "words", "in", "Tamil", "lexicographic", "order" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/utf8.py#L531-L552
7,981
Ezhil-Language-Foundation/open-tamil
tamil/utf8.py
word_intersection
def word_intersection( word_a, word_b ): """ return a list of tuples where word_a, word_b intersect """ positions = [] word_a_letters = get_letters( word_a ) word_b_letters = get_letters( word_b ) for idx,wa in enumerate(word_a_letters): for idy,wb in enumerate(word_b_letters): if ( wa == wb ): positions.append( (idx, idy) ) return positions
python
def word_intersection( word_a, word_b ): positions = [] word_a_letters = get_letters( word_a ) word_b_letters = get_letters( word_b ) for idx,wa in enumerate(word_a_letters): for idy,wb in enumerate(word_b_letters): if ( wa == wb ): positions.append( (idx, idy) ) return positions
[ "def", "word_intersection", "(", "word_a", ",", "word_b", ")", ":", "positions", "=", "[", "]", "word_a_letters", "=", "get_letters", "(", "word_a", ")", "word_b_letters", "=", "get_letters", "(", "word_b", ")", "for", "idx", ",", "wa", "in", "enumerate", ...
return a list of tuples where word_a, word_b intersect
[ "return", "a", "list", "of", "tuples", "where", "word_a", "word_b", "intersect" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/utf8.py#L558-L567
7,982
Ezhil-Language-Foundation/open-tamil
tamil/utf8.py
splitMeiUyir
def splitMeiUyir(uyirmei_char): """ This function split uyirmei compound character into mei + uyir characters and returns in tuple. Input : It must be unicode tamil char. Written By : Arulalan.T Date : 22.09.2014 """ if not isinstance(uyirmei_char, PYTHON3 and str or unicode): raise ValueError("Passed input letter '%s' must be unicode, \ not just string" % uyirmei_char) if uyirmei_char in mei_letters or uyirmei_char in uyir_letters or uyirmei_char in ayudha_letter: return uyirmei_char if uyirmei_char not in grantha_uyirmei_letters: if not is_normalized( uyirmei_char ): norm_char = unicode_normalize(uyirmei_char) rval = splitMeiUyir( norm_char ) return rval raise ValueError("Passed input letter '%s' is not tamil letter" % uyirmei_char) idx = grantha_uyirmei_letters.index(uyirmei_char) uyiridx = idx % 12 meiidx = int((idx - uyiridx)/ 12) return (grantha_mei_letters[meiidx], uyir_letters[uyiridx])
python
def splitMeiUyir(uyirmei_char): if not isinstance(uyirmei_char, PYTHON3 and str or unicode): raise ValueError("Passed input letter '%s' must be unicode, \ not just string" % uyirmei_char) if uyirmei_char in mei_letters or uyirmei_char in uyir_letters or uyirmei_char in ayudha_letter: return uyirmei_char if uyirmei_char not in grantha_uyirmei_letters: if not is_normalized( uyirmei_char ): norm_char = unicode_normalize(uyirmei_char) rval = splitMeiUyir( norm_char ) return rval raise ValueError("Passed input letter '%s' is not tamil letter" % uyirmei_char) idx = grantha_uyirmei_letters.index(uyirmei_char) uyiridx = idx % 12 meiidx = int((idx - uyiridx)/ 12) return (grantha_mei_letters[meiidx], uyir_letters[uyiridx])
[ "def", "splitMeiUyir", "(", "uyirmei_char", ")", ":", "if", "not", "isinstance", "(", "uyirmei_char", ",", "PYTHON3", "and", "str", "or", "unicode", ")", ":", "raise", "ValueError", "(", "\"Passed input letter '%s' must be unicode, \\\n not ...
This function split uyirmei compound character into mei + uyir characters and returns in tuple. Input : It must be unicode tamil char. Written By : Arulalan.T Date : 22.09.2014
[ "This", "function", "split", "uyirmei", "compound", "character", "into", "mei", "+", "uyir", "characters", "and", "returns", "in", "tuple", "." ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/utf8.py#L590-L619
7,983
Ezhil-Language-Foundation/open-tamil
tamil/utf8.py
joinMeiUyir
def joinMeiUyir(mei_char, uyir_char): """ This function join mei character and uyir character, and retuns as compound uyirmei unicode character. Inputs: mei_char : It must be unicode tamil mei char. uyir_char : It must be unicode tamil uyir char. Written By : Arulalan.T Date : 22.09.2014 """ if not mei_char: return uyir_char if not uyir_char: return mei_char if not isinstance(mei_char, PYTHON3 and str or unicode): raise ValueError(u"Passed input mei character '%s' must be unicode, not just string" % mei_char) if not isinstance(uyir_char, PYTHON3 and str or unicode) and uyir_char != None: raise ValueError(u"Passed input uyir character '%s' must be unicode, not just string" % uyir_char) if mei_char not in grantha_mei_letters: raise ValueError(u"Passed input character '%s' is not a tamil mei character" % mei_char) if uyir_char not in uyir_letters: raise ValueError(u"Passed input character '%s' is not a tamil uyir character" % uyir_char) if uyir_char: uyiridx = uyir_letters.index(uyir_char) else: return mei_char meiidx = grantha_mei_letters.index(mei_char) # calculate uyirmei index uyirmeiidx = meiidx*12 + uyiridx return grantha_uyirmei_letters[uyirmeiidx]
python
def joinMeiUyir(mei_char, uyir_char): if not mei_char: return uyir_char if not uyir_char: return mei_char if not isinstance(mei_char, PYTHON3 and str or unicode): raise ValueError(u"Passed input mei character '%s' must be unicode, not just string" % mei_char) if not isinstance(uyir_char, PYTHON3 and str or unicode) and uyir_char != None: raise ValueError(u"Passed input uyir character '%s' must be unicode, not just string" % uyir_char) if mei_char not in grantha_mei_letters: raise ValueError(u"Passed input character '%s' is not a tamil mei character" % mei_char) if uyir_char not in uyir_letters: raise ValueError(u"Passed input character '%s' is not a tamil uyir character" % uyir_char) if uyir_char: uyiridx = uyir_letters.index(uyir_char) else: return mei_char meiidx = grantha_mei_letters.index(mei_char) # calculate uyirmei index uyirmeiidx = meiidx*12 + uyiridx return grantha_uyirmei_letters[uyirmeiidx]
[ "def", "joinMeiUyir", "(", "mei_char", ",", "uyir_char", ")", ":", "if", "not", "mei_char", ":", "return", "uyir_char", "if", "not", "uyir_char", ":", "return", "mei_char", "if", "not", "isinstance", "(", "mei_char", ",", "PYTHON3", "and", "str", "or", "un...
This function join mei character and uyir character, and retuns as compound uyirmei unicode character. Inputs: mei_char : It must be unicode tamil mei char. uyir_char : It must be unicode tamil uyir char. Written By : Arulalan.T Date : 22.09.2014
[ "This", "function", "join", "mei", "character", "and", "uyir", "character", "and", "retuns", "as", "compound", "uyirmei", "unicode", "character", "." ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/utf8.py#L622-L652
7,984
Ezhil-Language-Foundation/open-tamil
tamil/regexp.py
make_pattern
def make_pattern( patt, flags=0 ): """ returns a compile regular expression object """ # print('input',len(patt)) patt_letters = utf8.get_letters( patt ) patt_out = list() idx = 0 # print('output',len(patt_letters)) patt = [None,None] prev = None LEN_PATT = len(patt_letters) while( idx < LEN_PATT ): if utf8.istamil( patt_letters[idx] ) and ( prev == '-' or ((idx+1) < LEN_PATT and patt_letters[idx+1] == u'-') ): if (idx+1) < LEN_PATT and patt_letters[idx+1] == u'-': patt[0] = patt_letters[idx] idx = idx + 2 prev = "-" elif prev == '-': patt[1] = patt_letters[idx] patt_out.extend( expand_tamil( patt[0], patt[1]) ) idx = idx + 1 prev = patt_letters[idx] continue patt_out.extend( patt_letters[idx] ) prev = patt_letters[idx] idx = idx + 1 opattern = u"".join( patt_out ) compile_regexp = re.compile( opattern, flags ) return (compile_regexp,opattern)
python
def make_pattern( patt, flags=0 ): # print('input',len(patt)) patt_letters = utf8.get_letters( patt ) patt_out = list() idx = 0 # print('output',len(patt_letters)) patt = [None,None] prev = None LEN_PATT = len(patt_letters) while( idx < LEN_PATT ): if utf8.istamil( patt_letters[idx] ) and ( prev == '-' or ((idx+1) < LEN_PATT and patt_letters[idx+1] == u'-') ): if (idx+1) < LEN_PATT and patt_letters[idx+1] == u'-': patt[0] = patt_letters[idx] idx = idx + 2 prev = "-" elif prev == '-': patt[1] = patt_letters[idx] patt_out.extend( expand_tamil( patt[0], patt[1]) ) idx = idx + 1 prev = patt_letters[idx] continue patt_out.extend( patt_letters[idx] ) prev = patt_letters[idx] idx = idx + 1 opattern = u"".join( patt_out ) compile_regexp = re.compile( opattern, flags ) return (compile_regexp,opattern)
[ "def", "make_pattern", "(", "patt", ",", "flags", "=", "0", ")", ":", "# print('input',len(patt))", "patt_letters", "=", "utf8", ".", "get_letters", "(", "patt", ")", "patt_out", "=", "list", "(", ")", "idx", "=", "0", "# print('output',len(patt_letters))", "p...
returns a compile regular expression object
[ "returns", "a", "compile", "regular", "expression", "object" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/regexp.py#L36-L65
7,985
Ezhil-Language-Foundation/open-tamil
examples/wordxsec.py
WordXSec.compute
def compute( self ): # compute the intersection graph into @xsections dictionary wordlist = self.wordlist """ build a dictionary of words, and their intersections """ xsections = {} for i in range(len(wordlist)): word_i = wordlist[i] for j in range(len(wordlist)): word_j = wordlist[j] if i == j: # force self-intersection to be 0 if not xsections.get(word_i,None): xsections[word_i] = [''] else: xsections[word_i].extend(['']) continue # optimize for, i > j, info is calculated already if i > j: xsec_counts = xsections[word_j][i] else: xsec_counts = tamil.utf8.word_intersection( word_i, word_j ) if not xsections.get(word_i,None): xsections[word_i] = [xsec_counts] else: xsections[word_i].extend( [ xsec_counts ] ) self.xsections = xsections
python
def compute( self ): # compute the intersection graph into @xsections dictionary wordlist = self.wordlist xsections = {} for i in range(len(wordlist)): word_i = wordlist[i] for j in range(len(wordlist)): word_j = wordlist[j] if i == j: # force self-intersection to be 0 if not xsections.get(word_i,None): xsections[word_i] = [''] else: xsections[word_i].extend(['']) continue # optimize for, i > j, info is calculated already if i > j: xsec_counts = xsections[word_j][i] else: xsec_counts = tamil.utf8.word_intersection( word_i, word_j ) if not xsections.get(word_i,None): xsections[word_i] = [xsec_counts] else: xsections[word_i].extend( [ xsec_counts ] ) self.xsections = xsections
[ "def", "compute", "(", "self", ")", ":", "# compute the intersection graph into @xsections dictionary\r", "wordlist", "=", "self", ".", "wordlist", "xsections", "=", "{", "}", "for", "i", "in", "range", "(", "len", "(", "wordlist", ")", ")", ":", "word_i", "="...
build a dictionary of words, and their intersections
[ "build", "a", "dictionary", "of", "words", "and", "their", "intersections" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/examples/wordxsec.py#L26-L51
7,986
Ezhil-Language-Foundation/open-tamil
tamilstemmer/basestemmer.py
BaseStemmer.set_current
def set_current(self, value): ''' Set the self.current string. ''' self.current = value self.cursor = 0 self.limit = len(self.current) self.limit_backward = 0 self.bra = self.cursor self.ket = self.limit
python
def set_current(self, value): ''' Set the self.current string. ''' self.current = value self.cursor = 0 self.limit = len(self.current) self.limit_backward = 0 self.bra = self.cursor self.ket = self.limit
[ "def", "set_current", "(", "self", ",", "value", ")", ":", "self", ".", "current", "=", "value", "self", ".", "cursor", "=", "0", "self", ".", "limit", "=", "len", "(", "self", ".", "current", ")", "self", ".", "limit_backward", "=", "0", "self", "...
Set the self.current string.
[ "Set", "the", "self", ".", "current", "string", "." ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamilstemmer/basestemmer.py#L12-L21
7,987
Ezhil-Language-Foundation/open-tamil
tamilstemmer/basestemmer.py
BaseStemmer.replace_s
def replace_s(self, c_bra, c_ket, s): ''' to replace chars between c_bra and c_ket in self.current by the chars in s. @type c_bra int @type c_ket int @type s: string ''' adjustment = len(s) - (c_ket - c_bra) self.current = self.current[0:c_bra] + s + self.current[c_ket:] self.limit += adjustment if self.cursor >= c_ket: self.cursor += adjustment elif self.cursor > c_bra: self.cursor = c_bra return adjustment
python
def replace_s(self, c_bra, c_ket, s): ''' to replace chars between c_bra and c_ket in self.current by the chars in s. @type c_bra int @type c_ket int @type s: string ''' adjustment = len(s) - (c_ket - c_bra) self.current = self.current[0:c_bra] + s + self.current[c_ket:] self.limit += adjustment if self.cursor >= c_ket: self.cursor += adjustment elif self.cursor > c_bra: self.cursor = c_bra return adjustment
[ "def", "replace_s", "(", "self", ",", "c_bra", ",", "c_ket", ",", "s", ")", ":", "adjustment", "=", "len", "(", "s", ")", "-", "(", "c_ket", "-", "c_bra", ")", "self", ".", "current", "=", "self", ".", "current", "[", "0", ":", "c_bra", "]", "+...
to replace chars between c_bra and c_ket in self.current by the chars in s. @type c_bra int @type c_ket int @type s: string
[ "to", "replace", "chars", "between", "c_bra", "and", "c_ket", "in", "self", ".", "current", "by", "the", "chars", "in", "s", "." ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamilstemmer/basestemmer.py#L261-L277
7,988
Ezhil-Language-Foundation/open-tamil
tamilstemmer/basestemmer.py
BaseStemmer.slice_to
def slice_to(self, s): ''' Copy the slice into the supplied StringBuffer @type s: string ''' result = '' if self.slice_check(): result = self.current[self.bra:self.ket] return result
python
def slice_to(self, s): ''' Copy the slice into the supplied StringBuffer @type s: string ''' result = '' if self.slice_check(): result = self.current[self.bra:self.ket] return result
[ "def", "slice_to", "(", "self", ",", "s", ")", ":", "result", "=", "''", "if", "self", ".", "slice_check", "(", ")", ":", "result", "=", "self", ".", "current", "[", "self", ".", "bra", ":", "self", ".", "ket", "]", "return", "result" ]
Copy the slice into the supplied StringBuffer @type s: string
[ "Copy", "the", "slice", "into", "the", "supplied", "StringBuffer" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamilstemmer/basestemmer.py#L309-L318
7,989
Ezhil-Language-Foundation/open-tamil
tamil/tweetparser.py
TamilTweetParser.getTamilWords
def getTamilWords( tweet ): """" word needs to all be in the same tamil language """ tweet = TamilTweetParser.cleanupPunct( tweet ); nonETwords = filter( lambda x: len(x) > 0 , re.split(r'\s+',tweet) );#|"+|\'+|#+ tamilWords = filter( TamilTweetParser.isTamilPredicate, nonETwords ); return tamilWords
python
def getTamilWords( tweet ): "tweet = TamilTweetParser.cleanupPunct( tweet ); nonETwords = filter( lambda x: len(x) > 0 , re.split(r'\s+',tweet) );#|"+|\'+|#+ tamilWords = filter( TamilTweetParser.isTamilPredicate, nonETwords ); return tamilWords
[ "def", "getTamilWords", "(", "tweet", ")", ":", "tweet", "=", "TamilTweetParser", ".", "cleanupPunct", "(", "tweet", ")", "nonETwords", "=", "filter", "(", "lambda", "x", ":", "len", "(", "x", ")", ">", "0", ",", "re", ".", "split", "(", "r'\\s+'", "...
word needs to all be in the same tamil language
[ "word", "needs", "to", "all", "be", "in", "the", "same", "tamil", "language" ]
b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0
https://github.com/Ezhil-Language-Foundation/open-tamil/blob/b7556e88878d29bbc6c944ee17cdd3f75b8ea9f0/tamil/tweetparser.py#L85-L90
7,990
wdecoster/nanocomp
nanocomp/NanoComp.py
validate_split_runs_file
def validate_split_runs_file(split_runs_file): """Check if structure of file is as expected and return dictionary linking names to run_IDs.""" try: content = [l.strip() for l in split_runs_file.readlines()] if content[0].upper().split('\t') == ['NAME', 'RUN_ID']: return {c.split('\t')[1]: c.split('\t')[0] for c in content[1:] if c} else: sys.exit("ERROR: Mandatory header of --split_runs tsv file not found: 'NAME', 'RUN_ID'") logging.error("Mandatory header of --split_runs tsv file not found: 'NAME', 'RUN_ID'") except IndexError: sys.exit("ERROR: Format of --split_runs tab separated file not as expected") logging.error("ERROR: Format of --split_runs tab separated file not as expected")
python
def validate_split_runs_file(split_runs_file): try: content = [l.strip() for l in split_runs_file.readlines()] if content[0].upper().split('\t') == ['NAME', 'RUN_ID']: return {c.split('\t')[1]: c.split('\t')[0] for c in content[1:] if c} else: sys.exit("ERROR: Mandatory header of --split_runs tsv file not found: 'NAME', 'RUN_ID'") logging.error("Mandatory header of --split_runs tsv file not found: 'NAME', 'RUN_ID'") except IndexError: sys.exit("ERROR: Format of --split_runs tab separated file not as expected") logging.error("ERROR: Format of --split_runs tab separated file not as expected")
[ "def", "validate_split_runs_file", "(", "split_runs_file", ")", ":", "try", ":", "content", "=", "[", "l", ".", "strip", "(", ")", "for", "l", "in", "split_runs_file", ".", "readlines", "(", ")", "]", "if", "content", "[", "0", "]", ".", "upper", "(", ...
Check if structure of file is as expected and return dictionary linking names to run_IDs.
[ "Check", "if", "structure", "of", "file", "is", "as", "expected", "and", "return", "dictionary", "linking", "names", "to", "run_IDs", "." ]
0533f258201263858ac0467da37c855880560d2d
https://github.com/wdecoster/nanocomp/blob/0533f258201263858ac0467da37c855880560d2d/nanocomp/NanoComp.py#L195-L206
7,991
wdecoster/nanocomp
nanocomp/NanoComp.py
change_identifiers
def change_identifiers(datadf, split_dict): """Change the dataset identifiers based on the names in the dictionary.""" for rid, name in split_dict.items(): datadf.loc[datadf["runIDs"] == rid, "dataset"] = name
python
def change_identifiers(datadf, split_dict): for rid, name in split_dict.items(): datadf.loc[datadf["runIDs"] == rid, "dataset"] = name
[ "def", "change_identifiers", "(", "datadf", ",", "split_dict", ")", ":", "for", "rid", ",", "name", "in", "split_dict", ".", "items", "(", ")", ":", "datadf", ".", "loc", "[", "datadf", "[", "\"runIDs\"", "]", "==", "rid", ",", "\"dataset\"", "]", "=",...
Change the dataset identifiers based on the names in the dictionary.
[ "Change", "the", "dataset", "identifiers", "based", "on", "the", "names", "in", "the", "dictionary", "." ]
0533f258201263858ac0467da37c855880560d2d
https://github.com/wdecoster/nanocomp/blob/0533f258201263858ac0467da37c855880560d2d/nanocomp/NanoComp.py#L209-L212
7,992
adafruit/Adafruit_Python_MCP9808
Adafruit_MCP9808/MCP9808.py
MCP9808.begin
def begin(self): """Start taking temperature measurements. Returns True if the device is intialized, False otherwise. """ # Check manufacturer and device ID match expected values. mid = self._device.readU16BE(MCP9808_REG_MANUF_ID) did = self._device.readU16BE(MCP9808_REG_DEVICE_ID) self._logger.debug('Read manufacturer ID: {0:04X}'.format(mid)) self._logger.debug('Read device ID: {0:04X}'.format(did)) return mid == 0x0054 and did == 0x0400
python
def begin(self): # Check manufacturer and device ID match expected values. mid = self._device.readU16BE(MCP9808_REG_MANUF_ID) did = self._device.readU16BE(MCP9808_REG_DEVICE_ID) self._logger.debug('Read manufacturer ID: {0:04X}'.format(mid)) self._logger.debug('Read device ID: {0:04X}'.format(did)) return mid == 0x0054 and did == 0x0400
[ "def", "begin", "(", "self", ")", ":", "# Check manufacturer and device ID match expected values.", "mid", "=", "self", ".", "_device", ".", "readU16BE", "(", "MCP9808_REG_MANUF_ID", ")", "did", "=", "self", ".", "_device", ".", "readU16BE", "(", "MCP9808_REG_DEVICE...
Start taking temperature measurements. Returns True if the device is intialized, False otherwise.
[ "Start", "taking", "temperature", "measurements", ".", "Returns", "True", "if", "the", "device", "is", "intialized", "False", "otherwise", "." ]
5524605a15cfce5668f259de72c88d5be74565f4
https://github.com/adafruit/Adafruit_Python_MCP9808/blob/5524605a15cfce5668f259de72c88d5be74565f4/Adafruit_MCP9808/MCP9808.py#L67-L76
7,993
adafruit/Adafruit_Python_MCP9808
Adafruit_MCP9808/MCP9808.py
MCP9808.readTempC
def readTempC(self): """Read sensor and return its value in degrees celsius.""" # Read temperature register value. t = self._device.readU16BE(MCP9808_REG_AMBIENT_TEMP) self._logger.debug('Raw ambient temp register value: 0x{0:04X}'.format(t & 0xFFFF)) # Scale and convert to signed value. temp = (t & 0x0FFF) / 16.0 if t & 0x1000: temp -= 256.0 return temp
python
def readTempC(self): # Read temperature register value. t = self._device.readU16BE(MCP9808_REG_AMBIENT_TEMP) self._logger.debug('Raw ambient temp register value: 0x{0:04X}'.format(t & 0xFFFF)) # Scale and convert to signed value. temp = (t & 0x0FFF) / 16.0 if t & 0x1000: temp -= 256.0 return temp
[ "def", "readTempC", "(", "self", ")", ":", "# Read temperature register value.", "t", "=", "self", ".", "_device", ".", "readU16BE", "(", "MCP9808_REG_AMBIENT_TEMP", ")", "self", ".", "_logger", ".", "debug", "(", "'Raw ambient temp register value: 0x{0:04X}'", ".", ...
Read sensor and return its value in degrees celsius.
[ "Read", "sensor", "and", "return", "its", "value", "in", "degrees", "celsius", "." ]
5524605a15cfce5668f259de72c88d5be74565f4
https://github.com/adafruit/Adafruit_Python_MCP9808/blob/5524605a15cfce5668f259de72c88d5be74565f4/Adafruit_MCP9808/MCP9808.py#L78-L87
7,994
bmihelac/django-cruds
cruds/utils.py
crud_url_name
def crud_url_name(model, action, prefix=None): """ Returns url name for given model and action. """ if prefix is None: prefix = "" app_label = model._meta.app_label model_lower = model.__name__.lower() return '%s%s_%s_%s' % (prefix, app_label, model_lower, action)
python
def crud_url_name(model, action, prefix=None): if prefix is None: prefix = "" app_label = model._meta.app_label model_lower = model.__name__.lower() return '%s%s_%s_%s' % (prefix, app_label, model_lower, action)
[ "def", "crud_url_name", "(", "model", ",", "action", ",", "prefix", "=", "None", ")", ":", "if", "prefix", "is", "None", ":", "prefix", "=", "\"\"", "app_label", "=", "model", ".", "_meta", ".", "app_label", "model_lower", "=", "model", ".", "__name__", ...
Returns url name for given model and action.
[ "Returns", "url", "name", "for", "given", "model", "and", "action", "." ]
7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7
https://github.com/bmihelac/django-cruds/blob/7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7/cruds/utils.py#L34-L42
7,995
bmihelac/django-cruds
cruds/utils.py
crud_url
def crud_url(instance_or_model, action, prefix=None, additional_kwargs=None): """Shortcut function returns URL for instance or model and action. Example:: crud_url(author, 'update') Is same as: reverse('testapp_author_update', kwargs={'pk': author.pk}) Example:: crud_url(Author, 'update') Is same as: reverse('testapp_author_list') """ if additional_kwargs is None: additional_kwargs = {} if isinstance(instance_or_model, Model): additional_kwargs['pk'] = instance_or_model.pk model_name = instance_or_model._meta.model else: model_name = instance_or_model return reverse( crud_url_name(model_name, action, prefix), kwargs=additional_kwargs )
python
def crud_url(instance_or_model, action, prefix=None, additional_kwargs=None): if additional_kwargs is None: additional_kwargs = {} if isinstance(instance_or_model, Model): additional_kwargs['pk'] = instance_or_model.pk model_name = instance_or_model._meta.model else: model_name = instance_or_model return reverse( crud_url_name(model_name, action, prefix), kwargs=additional_kwargs )
[ "def", "crud_url", "(", "instance_or_model", ",", "action", ",", "prefix", "=", "None", ",", "additional_kwargs", "=", "None", ")", ":", "if", "additional_kwargs", "is", "None", ":", "additional_kwargs", "=", "{", "}", "if", "isinstance", "(", "instance_or_mod...
Shortcut function returns URL for instance or model and action. Example:: crud_url(author, 'update') Is same as: reverse('testapp_author_update', kwargs={'pk': author.pk}) Example:: crud_url(Author, 'update') Is same as: reverse('testapp_author_list')
[ "Shortcut", "function", "returns", "URL", "for", "instance", "or", "model", "and", "action", "." ]
7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7
https://github.com/bmihelac/django-cruds/blob/7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7/cruds/utils.py#L60-L89
7,996
bmihelac/django-cruds
cruds/templatetags/crud_tags.py
format_value
def format_value(obj, field_name): """ Simple value formatting. If value is model instance returns link to detail view if exists. """ display_func = getattr(obj, 'get_%s_display' % field_name, None) if display_func: return display_func() value = getattr(obj, field_name) if isinstance(value, models.fields.files.FieldFile): if value: return mark_safe('<a href="%s">%s</a>' % ( value.url, os.path.basename(value.name), )) else: return '' if isinstance(value, models.Model): return format_value_instance(value) if isinstance(value, models.Manager): return mark_safe(', '.join( [format_value_instance(instance) for instance in value.all()] )) if value is None: value = "" return value
python
def format_value(obj, field_name): display_func = getattr(obj, 'get_%s_display' % field_name, None) if display_func: return display_func() value = getattr(obj, field_name) if isinstance(value, models.fields.files.FieldFile): if value: return mark_safe('<a href="%s">%s</a>' % ( value.url, os.path.basename(value.name), )) else: return '' if isinstance(value, models.Model): return format_value_instance(value) if isinstance(value, models.Manager): return mark_safe(', '.join( [format_value_instance(instance) for instance in value.all()] )) if value is None: value = "" return value
[ "def", "format_value", "(", "obj", ",", "field_name", ")", ":", "display_func", "=", "getattr", "(", "obj", ",", "'get_%s_display'", "%", "field_name", ",", "None", ")", "if", "display_func", ":", "return", "display_func", "(", ")", "value", "=", "getattr", ...
Simple value formatting. If value is model instance returns link to detail view if exists.
[ "Simple", "value", "formatting", "." ]
7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7
https://github.com/bmihelac/django-cruds/blob/7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7/cruds/templatetags/crud_tags.py#L52-L81
7,997
bmihelac/django-cruds
cruds/templatetags/crud_tags.py
get_fields
def get_fields(model, fields=None): """ Assigns fields for model. """ include = [f.strip() for f in fields.split(',')] if fields else None return utils.get_fields( model, include )
python
def get_fields(model, fields=None): include = [f.strip() for f in fields.split(',')] if fields else None return utils.get_fields( model, include )
[ "def", "get_fields", "(", "model", ",", "fields", "=", "None", ")", ":", "include", "=", "[", "f", ".", "strip", "(", ")", "for", "f", "in", "fields", ".", "split", "(", "','", ")", "]", "if", "fields", "else", "None", "return", "utils", ".", "ge...
Assigns fields for model.
[ "Assigns", "fields", "for", "model", "." ]
7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7
https://github.com/bmihelac/django-cruds/blob/7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7/cruds/templatetags/crud_tags.py#L113-L121
7,998
bmihelac/django-cruds
cruds/urls.py
crud_urls
def crud_urls(model, list_view=None, create_view=None, update_view=None, detail_view=None, delete_view=None, url_prefix=None, name_prefix=None, list_views=None, **kwargs): """Returns a list of url patterns for model. :param list_view: :param create_view: :param update_view: :param detail_view: :param delete_view: :param url_prefix: prefix to prepend, default is `'$'` :param name_prefix: prefix to prepend to name, default is empty string :param list_views(dict): additional list views :param **kwargs: additional detail views :returns: urls """ if url_prefix is None: url_prefix = r'^' urls = [] if list_view: urls.append(url( url_prefix + '$', list_view, name=utils.crud_url_name(model, utils.ACTION_LIST, name_prefix) )) if create_view: urls.append(url( url_prefix + r'new/$', create_view, name=utils.crud_url_name(model, utils.ACTION_CREATE, name_prefix) )) if detail_view: urls.append(url( url_prefix + r'(?P<pk>\d+)/$', detail_view, name=utils.crud_url_name(model, utils.ACTION_DETAIL, name_prefix) )) if update_view: urls.append(url( url_prefix + r'(?P<pk>\d+)/edit/$', update_view, name=utils.crud_url_name(model, utils.ACTION_UPDATE, name_prefix) )) if delete_view: urls.append(url( url_prefix + r'(?P<pk>\d+)/remove/$', delete_view, name=utils.crud_url_name(model, utils.ACTION_DELETE, name_prefix) )) if list_views is not None: for name, view in list_views.items(): urls.append(url( url_prefix + r'%s/$' % name, view, name=utils.crud_url_name(model, name, name_prefix) )) for name, view in kwargs.items(): urls.append(url( url_prefix + r'(?P<pk>\d+)/%s/$' % name, view, name=utils.crud_url_name(model, name, name_prefix) )) return urls
python
def crud_urls(model, list_view=None, create_view=None, update_view=None, detail_view=None, delete_view=None, url_prefix=None, name_prefix=None, list_views=None, **kwargs): if url_prefix is None: url_prefix = r'^' urls = [] if list_view: urls.append(url( url_prefix + '$', list_view, name=utils.crud_url_name(model, utils.ACTION_LIST, name_prefix) )) if create_view: urls.append(url( url_prefix + r'new/$', create_view, name=utils.crud_url_name(model, utils.ACTION_CREATE, name_prefix) )) if detail_view: urls.append(url( url_prefix + r'(?P<pk>\d+)/$', detail_view, name=utils.crud_url_name(model, utils.ACTION_DETAIL, name_prefix) )) if update_view: urls.append(url( url_prefix + r'(?P<pk>\d+)/edit/$', update_view, name=utils.crud_url_name(model, utils.ACTION_UPDATE, name_prefix) )) if delete_view: urls.append(url( url_prefix + r'(?P<pk>\d+)/remove/$', delete_view, name=utils.crud_url_name(model, utils.ACTION_DELETE, name_prefix) )) if list_views is not None: for name, view in list_views.items(): urls.append(url( url_prefix + r'%s/$' % name, view, name=utils.crud_url_name(model, name, name_prefix) )) for name, view in kwargs.items(): urls.append(url( url_prefix + r'(?P<pk>\d+)/%s/$' % name, view, name=utils.crud_url_name(model, name, name_prefix) )) return urls
[ "def", "crud_urls", "(", "model", ",", "list_view", "=", "None", ",", "create_view", "=", "None", ",", "update_view", "=", "None", ",", "detail_view", "=", "None", ",", "delete_view", "=", "None", ",", "url_prefix", "=", "None", ",", "name_prefix", "=", ...
Returns a list of url patterns for model. :param list_view: :param create_view: :param update_view: :param detail_view: :param delete_view: :param url_prefix: prefix to prepend, default is `'$'` :param name_prefix: prefix to prepend to name, default is empty string :param list_views(dict): additional list views :param **kwargs: additional detail views :returns: urls
[ "Returns", "a", "list", "of", "url", "patterns", "for", "model", "." ]
7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7
https://github.com/bmihelac/django-cruds/blob/7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7/cruds/urls.py#L17-L88
7,999
bmihelac/django-cruds
cruds/urls.py
crud_for_model
def crud_for_model(model, urlprefix=None): """Returns list of ``url`` items to CRUD a model. """ model_lower = model.__name__.lower() if urlprefix is None: urlprefix = '' urlprefix += model_lower + '/' urls = crud_urls( model, list_view=CRUDListView.as_view(model=model), create_view=CRUDCreateView.as_view(model=model), detail_view=CRUDDetailView.as_view(model=model), update_view=CRUDUpdateView.as_view(model=model), delete_view=CRUDDeleteView.as_view(model=model), url_prefix=urlprefix, ) return urls
python
def crud_for_model(model, urlprefix=None): model_lower = model.__name__.lower() if urlprefix is None: urlprefix = '' urlprefix += model_lower + '/' urls = crud_urls( model, list_view=CRUDListView.as_view(model=model), create_view=CRUDCreateView.as_view(model=model), detail_view=CRUDDetailView.as_view(model=model), update_view=CRUDUpdateView.as_view(model=model), delete_view=CRUDDeleteView.as_view(model=model), url_prefix=urlprefix, ) return urls
[ "def", "crud_for_model", "(", "model", ",", "urlprefix", "=", "None", ")", ":", "model_lower", "=", "model", ".", "__name__", ".", "lower", "(", ")", "if", "urlprefix", "is", "None", ":", "urlprefix", "=", "''", "urlprefix", "+=", "model_lower", "+", "'/...
Returns list of ``url`` items to CRUD a model.
[ "Returns", "list", "of", "url", "items", "to", "CRUD", "a", "model", "." ]
7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7
https://github.com/bmihelac/django-cruds/blob/7828aac3eb2b4c02e5f3843c4cbff654d57cf1e7/cruds/urls.py#L91-L109