From 5034768684f39874a347986a247f822d376bb418 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 19 Dec 2022 23:22:23 -0600 Subject: [PATCH] add some usage examples for viv --- examples/cli.py | 28 ++++++++++++++++++++++ examples/exe_specific.py | 21 ++++++++++++++++ examples/frozen_import.py | 22 +++++++++++++++++ examples/named_env.py | 17 +++++++++++++ examples/scrape.py | 50 +++++++++++++++++++++++++++++++++++++++ examples/sys_path.py | 20 ++++++++++++++++ examples/wrong_input.py | 4 ++++ 7 files changed, 162 insertions(+) create mode 100755 examples/cli.py create mode 100644 examples/exe_specific.py create mode 100644 examples/frozen_import.py create mode 100644 examples/named_env.py create mode 100755 examples/scrape.py create mode 100644 examples/sys_path.py create mode 100644 examples/wrong_input.py diff --git a/examples/cli.py b/examples/cli.py new file mode 100755 index 0000000..9324527 --- /dev/null +++ b/examples/cli.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +""" +It can be convenient to quickly generate a cli for a short script. +Or to add simple visualization of data using the wonderful rich library. +""" + +__import__("viv").activate("typer", "rich-click") # noqa + +import typer + +app = typer.Typer() + + +@app.command() +def hello(name: str): + print(f"Hello {name}") + + +@app.command() +def goodbye(name: str, formal: bool = False): + if formal: + print(f"Goodbye Ms. {name}. Have a good day.") + else: + print(f"Bye {name}!") + + +if __name__ == "__main__": + app() diff --git a/examples/exe_specific.py b/examples/exe_specific.py new file mode 100644 index 0000000..85107c1 --- /dev/null +++ b/examples/exe_specific.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +""" +This script will generate a vivenv that is executable specific. +This means if it is run using a different python version or executable +it will generate a new vivenv. + +It may be important to require a exe specificty if you are frequently running +different version of pythons and rely on c extension modules as in numpy. +""" + +__import__("viv").activate("numpy", "termplotlib", track_exe=True) + +import numpy as np +import termplotlib as tpl + +x = np.linspace(0, 2 * np.pi, 10) +y = np.sin(x) + +fig = tpl.figure() +fig.plot(x, y, label="data", width=50, height=15) +fig.show() diff --git a/examples/frozen_import.py b/examples/frozen_import.py new file mode 100644 index 0000000..e31f728 --- /dev/null +++ b/examples/frozen_import.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +""" +This import statement was generated using +`viv freeze pandas tabulate` on 2022.12.19 + +Using viv freeze ensures future runs of this +script will use the same essential environment +""" +__import__("viv").activate( + "numpy==1.24.0", + "pandas==1.5.2", + "python-dateutil==2.8.2", + "pytz==2022.7", + "six==1.16.0", + "tabulate==0.9.0", +) # noqa + +import pandas as pd + +df = pd.DataFrame({"x": range(10), "y": range(10)}) + +print(df.to_markdown()) diff --git a/examples/named_env.py b/examples/named_env.py new file mode 100644 index 0000000..304c649 --- /dev/null +++ b/examples/named_env.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +""" +# Viv env is named + +Meaning that it will save it within the viv cache not using a hash. + +*This environment could then be reused by specifying the name* +""" + +__import__("viv").activate("rich", name="rich-env") + +from rich.console import Console +from rich.markdown import Markdown + +console = Console() +md = Markdown(__doc__) +console.print(md) diff --git a/examples/scrape.py b/examples/scrape.py new file mode 100755 index 0000000..4179d92 --- /dev/null +++ b/examples/scrape.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +"""A Simple Script for Extracting Data from a Webpage +This script allows the user to extract data from a webapge +and then export the data to a csv file with column(s). +modified from: +https://medium.com/analytics-vidhya/a-super-easy-python-script-for-web-scraping-that-anybody-can-use-d3bd6ab86c89 +""" + +__import__("viv").activate("requests", "bs4", "rich") # noqa + +import requests +from bs4 import BeautifulSoup +from rich import box +from rich.console import Console +from rich.table import Table + +# Put your URL here +url = "https://www.nytimes.com/books/best-sellers/combined-print-and-e-book-nonfiction/" + +# Fetching the html +r = requests.get(url) + +# Parsing the html +parse = BeautifulSoup(r.content, "html.parser") + +# Provide html elements' attributes to extract the data +text1 = list( + e.get_text().strip() for e in parse.find_all("h3", attrs={"class": "css-5pe77f"}) +) +text2 = list( + e.get_text().strip().replace("by ", "") + for e in parse.find_all("p", attrs={"class": "css-hjukut"}) +) +max_len = max((len(txt) for txt in text1)) + +print() +table = Table(title="NY Times Best Sellers", box=box.ROUNDED, title_justify="left") +table.add_column( + "Title", + justify="right", + style="cyan", + no_wrap=True, +) +table.add_column("Author", style="magenta") + +for col1, col2 in zip(text1, text2): + table.add_row(col1, col2) + +console = Console() +console.print(table) diff --git a/examples/sys_path.py b/examples/sys_path.py new file mode 100644 index 0000000..73f2641 --- /dev/null +++ b/examples/sys_path.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +""" +Embed the viv.py on the sys.path at runtime rather than using PYTHONPATH +""" + + +__import__("sys").path.append( + __import__("os").path.expanduser("~/.viv/src") +) # noqa # isort: off +__import__("viv").activate("pyfiglet") # noqa # isort: off + +import sys + +from pyfiglet import Figlet + +f = Figlet(font="slant") +print(f.renderText("Viv isn't venv!")) + +print("Sys path:") +print("\n".join(sys.path)) diff --git a/examples/wrong_input.py b/examples/wrong_input.py new file mode 100644 index 0000000..073d6e2 --- /dev/null +++ b/examples/wrong_input.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python3 + +# ints are not allowed +__import__("viv").activate(5)