{ "info": { "author": "thautwarm", "author_email": "twshere@outlook.com", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: Implementation :: CPython" ], "description": "Linq.py\n=======\n\n|Build Status| |License| |codecov| |Coverage Status| |PyPI version|\n\nInstall\n-------\n\n::\n\n pip install -U Linq\n\nAdditional: Some magic here: `Mix Linq with\nPipe `__\n\nHere is an example to get top 10 frequent pixels in a picture.\n\n.. code:: python\n\n\n from linq import Flow\n import numpy as np\n\n def most_frequent(arr: np.ndarray) -> np.ndarray:\n return Flow(arr.flatten()) \\\n .GroupBy(lambda _: _) \\\n .Then(lambda x: x.items()) \\\n .Map(lambda k, v: (k, len(v))) \\\n .Sorted(by=lambda _, v: -v) \\\n .Take(10) \\\n .Map(lambda k, _: k) \\\n .ToList() \\\n .Then(np.array).Unboxed()\n\nAbout Linq\n----------\n\n| The well-known EDSL in .NET, ``Language Integrated Query``, in my\n opinion, is one of the best design in .NET environment.\n| Here is an example of C# Linq.\n\n.. code:: c#\n\n // Calculate MSE loss.\n /// the prediction of the neuron network\n /// the expected target of the neuron network\n\n Prediction.Zip(Expected, (pred, expected)=> Math.Square(pred-expected)).Average()\n\nIt's so human readable and it doesn't cost much.\n\n- Reference:\n\n - Microsoft .NET general introduction => `LINQ: .NET\n Language-Integrated\n Query `__.\n - Wikipedia => `Language Integrated\n Query `__.\n\nAnd there are so many scenes very awkward to Python programmer, using\n``Linq`` might help a lot.\n\nAwkward Scenes in Python\n------------------------\n\n.. code:: python\n\n\n seq1 = range(100)\n seq2 = range(100, 200)\n zipped = zip(seq1, seq2)\n mapped = map(lambda ab: ab[0] / ab[1], zipped)\n grouped = dict();\n group_fn = lambda x: x // 0.2\n for e in mapped:\n group_id = group_fn(e)\n if group_id not in grouped:\n grouped[group_id] = [e]\n continue\n grouped[group_id].append(e)\n for e in grouped.items():\n print(e)\n\nThe codes seems to be too long...\n\nNow we extract the function ``group_by``:\n\n.. code:: python\n\n\n def group_by(f, container):\n grouped = dict()\n for e in container:\n group_id = f(e)\n if group_id not in grouped:\n grouped[group_id] = [e]\n continue\n grouped[group_id].append(e)\n return grouped\n res = group_by(lambda x: x//0.2, map(lambda ab[0]/ab[1], zip(seq1, seq2)))\n\nOkay, it's not at fault, however, it makes me upset \u2014\u2014 why do I have to\nwrite these ugly codes?\n\n**Now, let us try Linq!**\n\n.. code:: python\n\n\n from linq import Flow, extension_std\n seq = Flow(range(100))\n res = seq.Zip(range(100, 200)).Map(lambda fst, snd : fst/snd).GroupBy(lambda num: num//0.2).Unboxed()\n\nHow does `Linq.py `__ work?\n--------------------------------------------------------------\n\n| There is a core class object, ``linq.core.flow.Flow``, which just has\n one member ``stream``.\n| When you want to get a specific extension method from ``Flow`` object,\n the ``type`` of its ``stream`` member will be used to search whether\n the extension method exists.\n| In other words, extension methods are binded with the type(precisely,\n ``{type.__module__}.{type.__name__}``).\n\n.. code:: python\n\n\n class Flow:\n __slots__ = ['stream']\n\n def __init__(self, sequence):\n self.stream = sequence\n\n def __getattr__(self, k):\n for cls in self.stream.__class__.__mro__:\n namespace = Extension['{}.{}'.format(cls.__module__, cls.__name__)]\n if k in namespace:\n return partial(namespace[k], self)\n raise NameError(\n \"No extension method named `{}` for {}.\".format(\n k, '{}.{}'.format(object.__module__, object.__name__)))\n\n def __str__(self):\n return self.stream.__str__()\n\n def __repr__(self):\n return self.__str__()\n\nExtension Method\n----------------\n\nHere are three methods for you to do so.\n\n- Firstly, you can use ``extension_std`` to add extension methods for\n all Flow objects.\n\n- Next, you use ``extension_class(cls: type)`` to add extension methods\n for all Flow objects whose member ``stream``'s type is named\n ``{cls.__module}.{cls.__name__}``.\n\n- Finally, you can use\n ``extension_class(cls_name: str, of_module='builtins')`` to add\n extension methods for all Flow objects whose member ``stream``'s type\n is named is named ``{of_module}.{cls_name}``.\n\n(This way to make extension methods is for the **implicit types** in\nPython, each of which cannot be got except from its instances' meta\nmember ``__class__``.)\n\n.. code:: python\n\n\n @extension_std # For all Flow objects\n def Add(self, i):\n return Flow(self.stream + (i.stream if isinstance(i, Flow) else i)))\n\n @extension_class(int) # Just for type `int`\n def Add(self, i):\n return Flow(self.stream + (i.stream if isinstance(i, Flow) else i)))\n\n @extension_class_name('int', of_module=int.__module__) # Also for type `int`.\n def Add(self, i):\n return Flow(self.stream + (i.stream if isinstance(i, Flow) else i)))\n\nDocuments of Standard Extension Methods\n---------------------------------------\n\nNote: Docs haven't been finished yet.\n\n- General(can be used by all Flow objects)\n\n - `Unboxed `__\n - `Sum `__\n - `Enum `__\n - `Map `__\n - `Reduce `__\n - `Then `__\n - `Each `__\n - `Aggregate `__\n - `Zip `__\n - `Sorted `__\n - `ArgSorted `__\n - `Group `__\n - `GroupBy `__\n - `Take `__\n - `TakeWhile `__\n - `Drop\\|Skip `__\n - `Concat `__\n - `ToList `__\n - `ToTuple `__\n - `ToDict `__\n - `ToSet `__\n - `All `__\n - `Any `__\n\n- List\n\n - `Extended `__\n - `Extend `__\n - `Sort `__\n - `Reversed `__\n - `Reverse `__\n\n- Set\n\n - `Intersects `__\n - `Union `__\n\nHow to Contribute\n-----------------\n\n- Design the `standard\n library `__\n for `Linq.py `__.\n\n- Write documents for the standard library and tutorials about how to\n use `Linq.py `__.\n\n- Join `LinqPy Room `__ to discuss\n about any aspects of `Linq.py `__.\n\nFeel free to pull requests here.\n\n.. |Build Status| image:: https://travis-ci.org/Xython/Linq.py.svg?branch=master\n :target: https://travis-ci.org/Xython/Linq.py\n.. |License| image:: https://img.shields.io/badge/license-MIT-yellow.svg\n :target: https://github.com/Xython/Linq.py/blob/master/LICENSE\n.. |codecov| image:: https://codecov.io/gh/Xython/Linq.py/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/Xython/Linq.py\n.. |Coverage Status| image:: https://coveralls.io/repos/github/Xython/Linq.py/badge.svg?branch=master\n :target: https://coveralls.io/github/Xython/Linq.py?branch=master\n.. |PyPI version| image:: https://img.shields.io/pypi/v/Linq.svg\n :target: https://pypi.python.org/pypi/Linq\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Xython/Linq.py", "keywords": "Linq", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "linq-t", "package_url": "https://pypi.org/project/linq-t/", "platform": "", "project_url": "https://pypi.org/project/linq-t/", "project_urls": { "Homepage": "https://github.com/Xython/Linq.py" }, "release_url": "https://pypi.org/project/linq-t/0.1/", "requires_dist": null, "requires_python": "", "summary": "Language Integrated Query for Python", "version": "0.1" }, "last_serial": 3988752, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "22300ba7dc9d6f93e78b3e74f396ab7c", "sha256": "1ea10ebccf239515e08321bf756d0936bd28bd6b40f726192e6a7ef11044257a" }, "downloads": -1, "filename": "linq_t-0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "22300ba7dc9d6f93e78b3e74f396ab7c", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 16778, "upload_time": "2018-06-22T09:13:12", "url": "https://files.pythonhosted.org/packages/fa/6e/08f9944bd7ba3d29377744bb241fc3b6cdd19d49383f15801455b1455b4e/linq_t-0.1-py3-none-any.whl" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "22300ba7dc9d6f93e78b3e74f396ab7c", "sha256": "1ea10ebccf239515e08321bf756d0936bd28bd6b40f726192e6a7ef11044257a" }, "downloads": -1, "filename": "linq_t-0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "22300ba7dc9d6f93e78b3e74f396ab7c", "packagetype": "bdist_wheel", "python_version": "3.6", "requires_python": null, "size": 16778, "upload_time": "2018-06-22T09:13:12", "url": "https://files.pythonhosted.org/packages/fa/6e/08f9944bd7ba3d29377744bb241fc3b6cdd19d49383f15801455b1455b4e/linq_t-0.1-py3-none-any.whl" } ] }