Skip to content

Commit

Permalink
Map test
Browse files Browse the repository at this point in the history
  • Loading branch information
deeplook committed Jul 27, 2020
1 parent 27c2270 commit 763a1b5
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 27 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ black:
build:
python3 -m pip install -r requirements.txt

clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts
clean: clean-build clean-pyc clean-test clean-doc ## remove all build, test, coverage and Python artifacts

clean-build: ## remove build artifacts
rm -fr build/
Expand All @@ -25,13 +25,18 @@ clean-pyc: ## remove Python file artifacts
find . -name '__pycache__' -exec rm -fr {} +

clean-test: ## remove test and coverage artifacts
# rm -fr .tox/
rm -f .coverage
rm -fr htmlcov/
rm -fr .mypy_cache
rm -fr .pytest_cache
find . -name '.ipynb_checkpoints' -exec rm -fr {} +

clean-doc:
pushd docs && make clean && popd

doc:
pushd docs && make html && popd

install:
python3 -m pip install -e .

Expand All @@ -44,9 +49,7 @@ lint:
black -l 79 --diff --check pyteen tests

test:
# pytest -v -s --cov=pyteen.py tests
pytest -s -v --cov-report=xml --cov=pyteen tests pyteen/snippets/tests
# coverage html

tree:
tree .
6 changes: 1 addition & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ Pyteen
Intro
-----

This is a tiny collection of self-contained, useful, short Python code snippets that are (1) all tested and validated, (2) easy to contribute to, and (3) simple to reuse.
This is a tiny collection of self-contained, short Python code snippets that are (1) all tested and validated, (2) easy to contribute to, and (3) simple to reuse.

Size Matters
------------

The main aspect here is code size which at the start was a strict limit of ten lines of code because in Python this already allows for solving useful little tasks, and the first chosen project name was "tenliners" (not so funny, agreed). Of course, some code is more useful than some other, hence it makes little sense to count comments or docstrings or empty lines (non-code). In fact, you soon realize you only really care about "effective code", the one that has the most effect (or value) to solve your task or demonstrate something. And there is very little effect of this in lines with only opening or closing brackets, braces or parentheses which people sometimes write to manually structure data. Same with imports which only enable you to use some modules. What is left is code that really does something when it executes. And this is what we are trying to measure here. Now the number ten is somewhat arbitrary (why not 11?) and it can be hard or impossible to express some interesting things with ten or less lines, even in Python. Therefore the concept of "ten" was stretched a bit into "teen" leading to the name "Pyteen" and a limit of 19 lines of effective code. Let's see where that goes!

More to come...
16 changes: 9 additions & 7 deletions pyteen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

class ImportFinder(ast.NodeVisitor):
"""An AST node visitor to collect information about import statements.
"""
The package named found are collected into the instance variable
``imported_names`` (a set).
"""
def __init__(self, *args, **kwargs):
self.imported_names = set()
super(*args, **kwargs)
Expand Down Expand Up @@ -114,18 +116,18 @@ def validate(source_code: str, raise_immediately: bool = True) -> str:
# Check imports are published on PyPI.org/Anaconda.org
# https://stackoverflow.com/questions/21419009/json-api-for-pypi-how-to-list-packages
imported_names = ImportFinder().analyse_code(source_code)
pip_available = [
pip_available = set(
pkg_name for pkg_name in imported_names if pip_installable(pkg_name)
]
)
# conda_available = [pkg_name for pkg_name in imported_names if conda_installable(pkg_name)]
stdlib_available = [
stdlib_available = set(
pkg_name for pkg_name in imported_names if pkg_name in std_libs
]
unknown = [
)
unknown = set(
pkg_name
for pkg_name in imported_names
if pkg_name not in stdlib_available and pkg_name not in pip_available
]
)
if raise_immediately and len(unknown) > 0:
msg = (
f"Found unknown dependencies {unknown}. "
Expand Down
12 changes: 12 additions & 0 deletions pyteen/snippets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from pathlib import Path
from types import ModuleType

import pytest


def search(query: str = ""):
"""Search snippets containing some query string and yield their paths.
"""
for path in Path(__file__).parent.rglob("*.py"):
if path.name.lower().startswith("test"):
continue
if not query:
yield path.name
elif query in path.read_text():
Expand All @@ -18,3 +22,11 @@ def show(mod: ModuleType = None):
"""Show code of the given snippet module.
"""
print(Path(mod.__file__).read_text())


def test():
"""Execute the tests inside the pyteens collection.
"""
folder = Path(__file__).parent.absolute()
print("++++", folder)
# pytest.main(["-s", "-v", folder])
4 changes: 4 additions & 0 deletions pyteen/snippets/algorithms/test_sieve_eratosthenes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from sieve_eratosthenes import sieve_of_eratosthenes as sieve

def test():
assert list(sieve(20)) == [2, 3, 5, 7, 11, 13, 17, 19]
12 changes: 8 additions & 4 deletions pyteen/snippets/maps/map_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def search(sender, m):
m.center = [lat, lon]
m += Marker(location=m.center)

m = Map(center=[0, 0], zoom=2)
tx = Text(layout=Layout(width="200px"))
tx.on_submit(partial(search, m=m))
m += WidgetControl(widget=tx, position="topright")
def make_map():
m = Map(center=[0, 0], zoom=2)
tx = Text(layout=Layout(width="200px"))
# tx.on_submit(partial(search, m=m))
tx.continuous_update = False
tx.observe(partial(search, m=m), 'value')
m += WidgetControl(widget=tx, position="topright")
return m, tx
3 changes: 0 additions & 3 deletions pyteen/snippets/tests/test_sieve_eratosthenes.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pytest
pytest-cov
pytest-flake8
pytest-mypy
sphinx
10 changes: 9 additions & 1 deletion tests/failing/failing1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This should fail because it has too many effective lines.
# This should fail because it has too many "effective" lines.

pass
pass
pass
Expand All @@ -13,3 +14,10 @@
pass
pass
pass
pass
pass
pass
pass
pass
pass

1 change: 1 addition & 0 deletions tests/failing/failing2.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# This should fail because it has semicolon(s).

pass; pass
5 changes: 3 additions & 2 deletions tests/failing/failing3.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# This should fail because it has semicolon(s).
import my_private_code
# This should fail because it imports an unknown module.

import my_private_local_code
8 changes: 7 additions & 1 deletion tests/test_search_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
from pyteen.snippets.algorithms import sieve_eratosthenes


def test_search():
def test_search_Fibonacci():
"""Do we find the Fibonacci snippets we know we have?
"""
for path in list(search("Fibo")):
assert re.match("fibo.*\\.py", os.path.basename(path)) != None


def test_search():
"""Do we find any snippets?
"""
assert len(list(search())) >= 3


def test_show(capsys):
"""Do we get the expected code for some snippet module?
"""
Expand Down

0 comments on commit 763a1b5

Please sign in to comment.