├── .gitignore ├── LICENSE ├── README.md ├── pytip ├── __init__.py ├── __main__.py ├── conftest.py └── tips.py ├── requirements.txt ├── setup.py └── tests └── test_tips.py /.gitignore: -------------------------------------------------------------------------------- 1 | # PyCharm Config Folder 2 | .idea/ 3 | 4 | # Cache files and objects 5 | __pycache__/ 6 | *.pyd 7 | *.pyc 8 | *.swp 9 | 10 | # Virtual environments 11 | venv/ 12 | env/ 13 | 14 | # Setup Directories & Files 15 | dist/ 16 | build/ 17 | **egg-info 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 PyBites 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyBites Tips CLI 2 | 3 | A wrapper to read [PyBites Python tips](https://codechalleng.es/tips) from the command line. 4 | 5 | ## Installation 6 | 7 | You can install _PyBites Tips CLI_ from [PyPI](https://pypi.org/project/pybites-tips/): 8 | 9 | pip install pybites-tips 10 | 11 | This tool uses Python 3.x 12 | 13 | ## Usage 14 | 15 | PyBites Tips CLI is a command line application. There are two ways to run it: 16 | 17 | 1. Interactive mode: 18 | 19 | $ pytip 20 | 21 | Search tips (press 'q' to exit): functools 22 | 3 tips found 23 | 24 | === TIP 153 === 25 | Title: functools.partial 26 | ... 27 | ... 28 | 29 | 2. Search for tips from the command line using the `-s` flag: 30 | 31 | $ pytip -s itertools 32 | 7 tips found 33 | 34 | === TIP 53 === 35 | Title: random.choice and itertools.product 36 | Tip: #Python's random, range and itertools.product make it easy to simulate 5 dice rolls: 37 | ... 38 | ... 39 | 40 | ## Paging 41 | 42 | If you want to _page_ through the results use the `-p` flag: 43 | 44 | $ pytip -s itertools -p 45 | 7 tips found 46 | 47 | Press any key to start paging them, then press 'q' to go to the next one ... or hit 'c' bail out: 48 | ... 49 | << resulting tips are paged (you see them one by one in your terminal) >> 50 | 51 | You can also just pipe `pytip`'s output to `more`: 52 | 53 | `pytip -s itertools|more` 54 | 55 | --- 56 | 57 | Enjoy! 58 | -------------------------------------------------------------------------------- /pytip/__init__.py: -------------------------------------------------------------------------------- 1 | from pytip.tips import PyBitesTips 2 | -------------------------------------------------------------------------------- /pytip/__main__.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from pytip.tips import PyBitesTips 4 | 5 | 6 | def main(): 7 | parser = argparse.ArgumentParser(description='Search term') 8 | parser.add_argument("-s", "--search", type=str, 9 | help='Search PyBites Python tips') 10 | parser.add_argument("-p", "--pager", action='store_true', 11 | help='Go through the resulting tips one by one') 12 | parser.add_argument("-c", "--colors", action='store_true', 13 | help='Do syntax highlighting on the snippets') 14 | 15 | args = parser.parse_args() 16 | 17 | pb_tips = PyBitesTips(use_pager=args.pager, use_colors=args.colors) 18 | if args.search: 19 | tips = pb_tips.filter_tips(args.search) 20 | pb_tips.show_tips(tips) 21 | else: 22 | pb_tips() 23 | 24 | 25 | if __name__ == '__main__': 26 | main() 27 | -------------------------------------------------------------------------------- /pytip/conftest.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PyBites-Open-Source/pybites-tips/b8b300bfdba8930c143bf0e64c4f00ac9a0bb6fa/pytip/conftest.py -------------------------------------------------------------------------------- /pytip/tips.py: -------------------------------------------------------------------------------- 1 | from collections import namedtuple 2 | import pydoc 3 | from rich.syntax import Syntax 4 | from rich.console import Console 5 | import os 6 | import requests 7 | from colorama import init 8 | 9 | # Windows shells (pycharm) act weird without this when printing colors... 10 | if os.name == 'nt': 11 | init(autoreset=True) 12 | 13 | TIPS_API_ENDPOINT = "https://codechalleng.es/api/tips/" 14 | 15 | Tip = namedtuple('Tip', ('id title tip code ' 16 | 'link image_link share_link')) 17 | EXIT, CANCEL = 'q', 'c' 18 | TIP = """ 19 | === TIP {id} === 20 | Title: {title} 21 | Tip: {tip} 22 | 23 | Code: 24 | {code} 25 | 26 | Links: 27 | {links} 28 | --- 29 | """ 30 | 31 | console = Console() 32 | 33 | class PyBitesTips: 34 | 35 | def __init__(self, use_pager=False, use_colors=False): 36 | self.use_pager = use_pager 37 | self.use_colors = use_colors 38 | self.tips = self._get_tips() 39 | 40 | 41 | def _get_tips(self): 42 | resp = requests.get(TIPS_API_ENDPOINT) 43 | resp.raise_for_status() 44 | return [Tip(**tip) for tip in resp.json()] 45 | 46 | def filter_tips(self, search): 47 | search = search.lower() 48 | return sorted( 49 | [tip for tip in self.tips if search in 50 | (tip.title + tip.tip + tip.code).lower()], 51 | key=lambda tip: tip.id) 52 | 53 | def show_tips(self, tips): 54 | hits = len(tips) 55 | if hits == 0: 56 | print("No hits, try another search term") 57 | return 58 | elif hits > 1: 59 | # if multiple tips and pager, explain interface 60 | print(f"{hits} tips found") 61 | if self.use_pager: 62 | choice = input( 63 | ("\nPress any key to start paging them, " 64 | f"then press '{EXIT}' to go to the next one ... " 65 | f"or hit '{CANCEL}' bail out: ") 66 | ) 67 | if choice == CANCEL: 68 | return 69 | self.print_tips(tips) 70 | 71 | def _generate_tip_output(self, tip): 72 | links = '\n'.join( 73 | link for link in 74 | (tip.link, tip.image_link, tip.share_link) 75 | if link) 76 | 77 | if self.use_colors: 78 | # Highlight the code with rich syntax highlighter 79 | code_highlighted = Syntax(tip.code, "python") 80 | 81 | # We don't want to use console.print as output, so we use capture() 82 | with console.capture() as capture: 83 | console.print(code_highlighted) 84 | code_output = capture.get() 85 | else: 86 | code_output = tip.code 87 | 88 | return TIP.format(id=tip.id, 89 | title=tip.title, 90 | tip=tip.tip, 91 | code=code_output, 92 | links=links) 93 | 94 | def print_tips(self, tips): 95 | for tip in tips: 96 | tip_fmt = self._generate_tip_output(tip) 97 | if self.use_pager: 98 | pydoc.pager(tip_fmt) 99 | else: 100 | print(tip_fmt) 101 | 102 | def __call__(self): 103 | while True: 104 | search = input( 105 | f"\nSearch tips (press '{EXIT}' to exit): ") 106 | if not search.strip(): 107 | print("Please enter a search term") 108 | continue 109 | if search == EXIT: 110 | print("Bye") 111 | break 112 | tips = self.filter_tips(search) 113 | self.show_tips(tips) 114 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | atomicwrites==1.4.0 2 | attrs==21.4.0 3 | certifi==2022.5.18.1 4 | charset-normalizer==2.0.12 5 | colorama==0.4.4 6 | commonmark==0.9.1 7 | idna==3.3 8 | iniconfig==1.1.1 9 | packaging==21.3 10 | pluggy==1.0.0 11 | py==1.11.0 12 | Pygments==2.12.0 13 | pyparsing==3.0.9 14 | pytest==7.1.2 15 | requests==2.28.0 16 | rich==12.4.4 17 | tomli==2.0.1 18 | urllib3==1.26.9 19 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | from setuptools import setup 3 | 4 | HERE = pathlib.Path(__file__).parent 5 | README = (HERE / "README.md").read_text() 6 | 7 | setup( 8 | name="pybites-tips", 9 | version="1.0.1", 10 | description="Read PyBites Python tips from the command line", 11 | long_description=README, 12 | long_description_content_type="text/markdown", 13 | url="https://github.com/PyBites-Open-Source/pybites-tips", 14 | author="PyBites", 15 | author_email="support@pybit.es", 16 | license="MIT", 17 | classifiers=[ 18 | "License :: OSI Approved :: MIT License", 19 | "Programming Language :: Python :: 3", 20 | "Programming Language :: Python :: 3.8", 21 | ], 22 | packages=["pytip"], 23 | include_package_data=True, 24 | install_requires=["requests"], 25 | entry_points={ 26 | "console_scripts": [ 27 | "pytip=pytip.__main__:main", 28 | ] 29 | }, 30 | ) 31 | -------------------------------------------------------------------------------- /tests/test_tips.py: -------------------------------------------------------------------------------- 1 | from unittest.mock import patch, Mock 2 | 3 | import pytest 4 | import requests 5 | 6 | from pytip import PyBitesTips 7 | 8 | tips_payload = [ 9 | {'code': ">>> 'Python' ' is ' 'fun'\n'Python is fun'", 10 | 'id': 11, 11 | 'image_link': None, 12 | 'link': None, 13 | 'share_link': 'https://twitter.com/pybites/status/1106182595866513409', 14 | 'tip': 'String literals will concatenate in #Python:', 15 | 'title': 'concatenate string literals'}, 16 | {'code': '>>> list(range(1, 11))\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]', 17 | 'id': 10, 18 | 'image_link': None, 19 | 'link': 'https://codechalleng.es/bites/1', 20 | 'share_link': 'https://twitter.com/pybites/status/1112944740830531585', 21 | 'tip': 'In #Python you can use the range builtin to generate ' 22 | 'a sequence of numbers', 23 | 'title': 'range'}, 24 | {'code': ">>> names = 'bob julian tim sara'.split()\n" 25 | ">>> names\n" 26 | "['bob', 'julian', 'tim', 'sara']", 27 | 'id': 8, 28 | 'image_link': None, 29 | 'link': None, 30 | 'share_link': 'https://twitter.com/pybites/status/1111157389317808129', 31 | 'tip': 'An easy way to make a list in #Python is to use split()' 32 | ' on a string...', 33 | 'title': 'split a string into a list'} 34 | ] 35 | 36 | 37 | @pytest.fixture 38 | @patch.object(requests, 'get') 39 | def pb_tips(mockget, scope="module"): 40 | """Mock requests working with our small sample set instead""" 41 | mockget.return_value = Mock(raise_for_status=lambda: None, 42 | json=lambda: tips_payload, 43 | status_code=200) 44 | pb_tips = PyBitesTips() 45 | assert len(pb_tips.tips) == 3 46 | return pb_tips 47 | 48 | 49 | def test_tip_output(pb_tips): 50 | """Test the format of a tip as bounced to the console""" 51 | first_tip = pb_tips.tips[0] 52 | actual = pb_tips._generate_tip_output(first_tip) 53 | expected = ( 54 | "\n=== TIP 11 ===\n" 55 | "Title: concatenate string literals\n" 56 | "Tip: String literals will concatenate in #Python:\n\n" 57 | "Code:\n>>> 'Python' ' is ' 'fun'\n'Python is fun'\n\n" 58 | "Links:\nhttps://twitter.com/pybites/status/1106182595866513409\n" 59 | "---\n") 60 | assert actual == expected 61 | 62 | 63 | @pytest.mark.parametrize("search, expected", [ 64 | ("julian", [8]), 65 | ("twitter", []), 66 | ("string", [8, 11]), 67 | ("STRING", [8, 11]), 68 | ("range", [10]), 69 | ("pYthon", [8, 10, 11]), 70 | (">>>", [8, 10, 11]), 71 | ("split", [8]), 72 | ("literal", [11]), 73 | ("list", [8, 10]), 74 | ]) 75 | def test_filter_tips(search, expected, pb_tips): 76 | """Test for multiple pattern matches on our mini sample set""" 77 | tips = pb_tips.filter_tips(search) 78 | returned_ids = [tip.id for tip in tips] 79 | assert returned_ids == expected 80 | 81 | 82 | @patch("builtins.input", side_effect=['twitter', '', 'julian', 'STRING', 'q']) 83 | def test_user_interface(input_mock, capsys, pb_tips): 84 | """Test a typical user interacting with the tool: 85 | first enter a non matching search string, then an empty string, 86 | then match one tip, then match two tips, and finally exit the app 87 | """ 88 | pb_tips() 89 | actual = capsys.readouterr()[0].strip() 90 | expected = """No hits, try another search term 91 | Please enter a search term 92 | 93 | === TIP 8 === 94 | Title: split a string into a list 95 | Tip: An easy way to make a list in #Python is to use split() on a string... 96 | 97 | Code: 98 | >>> names = 'bob julian tim sara'.split() 99 | >>> names 100 | ['bob', 'julian', 'tim', 'sara'] 101 | 102 | Links: 103 | https://twitter.com/pybites/status/1111157389317808129 104 | --- 105 | 106 | 2 tips found 107 | 108 | === TIP 8 === 109 | Title: split a string into a list 110 | Tip: An easy way to make a list in #Python is to use split() on a string... 111 | 112 | Code: 113 | >>> names = 'bob julian tim sara'.split() 114 | >>> names 115 | ['bob', 'julian', 'tim', 'sara'] 116 | 117 | Links: 118 | https://twitter.com/pybites/status/1111157389317808129 119 | --- 120 | 121 | 122 | === TIP 11 === 123 | Title: concatenate string literals 124 | Tip: String literals will concatenate in #Python: 125 | 126 | Code: 127 | >>> 'Python' ' is ' 'fun' 128 | 'Python is fun' 129 | 130 | Links: 131 | https://twitter.com/pybites/status/1106182595866513409 132 | --- 133 | 134 | Bye""" 135 | expected = '\n'.join(line.lstrip() 136 | for line in expected.splitlines()) 137 | assert actual == expected 138 | --------------------------------------------------------------------------------