add some usage examples for viv

This commit is contained in:
Daylin Morgan 2022-12-19 23:22:23 -06:00
parent 97bcf2ee28
commit 5034768684
7 changed files with 162 additions and 0 deletions

28
examples/cli.py Executable file
View file

@ -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()

21
examples/exe_specific.py Normal file
View file

@ -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()

22
examples/frozen_import.py Normal file
View file

@ -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())

17
examples/named_env.py Normal file
View file

@ -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)

50
examples/scrape.py Executable file
View file

@ -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)

20
examples/sys_path.py Normal file
View file

@ -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))

4
examples/wrong_input.py Normal file
View file

@ -0,0 +1,4 @@
#!/usr/bin/env python3
# ints are not allowed
__import__("viv").activate(5)