Skip to content

Commit

Permalink
examples: Import from top level
Browse files Browse the repository at this point in the history
Signed-off-by: John Andersen <[email protected]>
  • Loading branch information
pdxjohnny committed Apr 30, 2020
1 parent 13c959b commit 3b476b2
Show file tree
Hide file tree
Showing 26 changed files with 62 additions and 103 deletions.
2 changes: 1 addition & 1 deletion docs/quickstart/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ The ouput should be as follows
{'Years': 7, 'Expertise': 15, 'Trust': 0.8, 'Salary': 80.0}
Check out the plugin docs for :doc:`/plugins/dffml_model` for usage of other
models. The :doc:`API <api/high_level>` docs may also be useful.
models. The :doc:`API </api/high_level>` docs may also be useful.

Data Sources
------------
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorials/operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -371,31 +371,31 @@ Let's write an operation to grab the JSON information about a package.
**shouldi/pypi.py**

.. literalinclude:: /../examples/shouldi/shouldi/pypi.py
:lines: 1-35
:lines: 1-34

After we have the package information, we extract the version and URL where we
can get the source code.

**shouldi/pypi.py**

.. literalinclude:: /../examples/shouldi/shouldi/pypi.py
:lines: 38-60
:lines: 37-59

Once we have the URL, we download the package source and extract it to a
temporary directory.

**shouldi/pypi.py**

.. literalinclude:: /../examples/shouldi/shouldi/pypi.py
:lines: 63-82
:lines: 62-81

Finally, we make a ``cleanup`` operation to remove the directory once we're done
with it.

**shouldi/pypi.py**

.. literalinclude:: /../examples/shouldi/shouldi/pypi.py
:lines: 85-90
:lines: 84-89

Now we write tests for each operation.

Expand Down
6 changes: 3 additions & 3 deletions examples/MNIST/test_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import ast
import sys
import json
import shutil
import pathlib
import tempfile
import contextlib
import subprocess
import unittest.mock
import shutil
import pathlib

from dffml.util.os import chdir
from dffml import chdir


def sh_filepath(filename):
Expand Down
13 changes: 2 additions & 11 deletions examples/io/io_usage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import asyncio

from dffml import train, Features, DefFeature
from dffml.operation.output import GetSingle
from dffml.df.memory import MemoryOrchestrator
from dffml.operation.mapping import create_mapping
from dffml.operation.preprocess import literal_eval
from dffml.df.types import DataFlow, Input, InputFlow
from dffml.operation.io import AcceptUserInput, print_output
from dffml.operation.model import model_predict, ModelPredictConfig
from dffml.model.slr import SLRModel
from dffml import *

slr_model = SLRModel(
features=Features(DefFeature("Years", int, 1),),
Expand Down Expand Up @@ -62,7 +53,7 @@


async def main():
# train the model
# Train the model
await train(
slr_model,
{"Years": 0, "Salary": 10},
Expand Down
39 changes: 9 additions & 30 deletions examples/maintained/demoapp/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
import aiomysql
from typing import AsyncIterator, NamedTuple, Dict

from dffml.base import BaseConfig
from dffml.record import Record
from dffml.source.source import BaseSourceContext, BaseSource
from dffml.util.cli.arg import Arg
from dffml.util.entrypoint import entrypoint
from dffml import config, entrypoint, Record, BaseSourceContext, BaseSource


class DemoAppSourceConfig(BaseConfig, NamedTuple):
host: str
port: int
user: str
password: str
db: str
@config
class DemoAppSourceConfig:
host: str = "127.0.0.1"
port: int = 3306
user: str = "user"
password: str = "pass"
db: str = "db"


class DemoAppSourceContext(BaseSourceContext):
Expand Down Expand Up @@ -69,6 +66,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):
@entrypoint("demoapp")
class DemoAppSource(BaseSource):

CONFIG = DemoAppSourceConfig
CONTEXT = DemoAppSourceContext

async def __aenter__(self) -> "DemoAppSource":
Expand All @@ -87,22 +85,3 @@ async def __aexit__(self, exc_type, exc_value, traceback):
await self.__db.__aexit__(exc_type, exc_value, traceback)
self.pool.close()
await self.pool.wait_closed()

@classmethod
def args(cls, args, *above) -> Dict[str, Arg]:
cls.config_set(args, above, "host", Arg(default="127.0.0.1"))
cls.config_set(args, above, "port", Arg(type=int, default=3306))
cls.config_set(args, above, "user", Arg(default="user"))
cls.config_set(args, above, "password", Arg(default="pass"))
cls.config_set(args, above, "db", Arg(default="db"))
return args

@classmethod
def config(cls, config, *above):
return DemoAppSourceConfig(
host=cls.config_get(config, above, "host"),
port=cls.config_get(config, above, "port"),
user=cls.config_get(config, above, "user"),
password=cls.config_get(config, above, "password"),
db=cls.config_get(config, above, "db"),
)
3 changes: 1 addition & 2 deletions examples/model/slr/slr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dffml import Features, DefFeature
from dffml import Features, DefFeature, SLRModel
from dffml.noasync import train, accuracy, predict
from dffml.model.slr import SLRModel

model = SLRModel(
features=Features(DefFeature("f1", float, 1)),
Expand Down
2 changes: 1 addition & 1 deletion examples/model/slr/test_slr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import subprocess
import unittest.mock

from dffml.util.os import chdir
from dffml import chdir


def sh_filepath(filename):
Expand Down
2 changes: 1 addition & 1 deletion examples/quickstart_async.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio

from dffml_model_scikit import LinearRegressionModel
from dffml import train, accuracy, predict, Features, DefFeature
from dffml_model_scikit import LinearRegressionModel


async def main():
Expand Down
4 changes: 2 additions & 2 deletions examples/shouldi/shouldi/bandit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import asyncio
from typing import Dict, Any

from dffml.df.base import op
from dffml.df.types import Definition
from dffml import op
from dffml import Definition

package_src_dir = Definition(name="package_src_dir", primitive="str")
bandit_output = Definition(name="bandit_output", primitive="Dict[str, Any]")
Expand Down
3 changes: 1 addition & 2 deletions examples/shouldi/shouldi/cargo_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import asyncio
from typing import Dict, Any

from dffml.df.base import op
from dffml.df.types import Definition
from dffml import op, Definition

package_src_dir = Definition(name="package_src_dir", primitive="str")
cargo_audit_output = Definition(
Expand Down
3 changes: 1 addition & 2 deletions examples/shouldi/shouldi/dependency_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import asyncio
from typing import Dict, Any

from dffml.df.base import op
from dffml.df.types import Definition
from dffml import op, Definition

package_src_dir = Definition(name="package_src_dir", primitive="str")
dependency_check_output = Definition(
Expand Down
3 changes: 1 addition & 2 deletions examples/shouldi/shouldi/golangci_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import asyncio
from typing import Dict, Any

from dffml.df.base import op
from dffml.df.types import Definition
from dffml import op, Definition

package_src_dir = Definition(name="package_src_dir", primitive="str")
golangci_lint_output = Definition(
Expand Down
4 changes: 2 additions & 2 deletions examples/shouldi/shouldi/npm_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import asyncio
from typing import Dict, Any

from dffml.df.base import op
from dffml.df.types import Definition
from dffml import op
from dffml import Definition

package_src_dir = Definition(name="package_src_dir", primitive="str")
npm_audit_output = Definition(
Expand Down
3 changes: 1 addition & 2 deletions examples/shouldi/shouldi/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import aiohttp

from dffml.df.base import op
from dffml.df.types import Definition, Stage
from dffml import op, Definition, Stage

from .safety import package, package_version
from .bandit import package_src_dir
Expand Down
4 changes: 2 additions & 2 deletions examples/shouldi/shouldi/safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import asyncio
from typing import Dict, Any

from dffml.df.base import op
from dffml.df.types import Definition
from dffml import op
from dffml import Definition

package = Definition(name="package", primitive="str")
package_version = Definition(name="package_version", primitive="str")
Expand Down
2 changes: 1 addition & 1 deletion examples/shouldi/tests/test_bandit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from dffml.util.asynctestcase import AsyncTestCase
from dffml import AsyncTestCase

from shouldi.bandit import run_bandit

Expand Down
8 changes: 5 additions & 3 deletions examples/shouldi/tests/test_cargo_audit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pathlib

from dffml.util.os import prepend_to_path
from dffml.util.net import cached_download_unpack_archive
from dffml.util.asynctestcase import AsyncTestCase
from dffml import (
prepend_to_path,
cached_download_unpack_archive,
AsyncTestCase,
)

from shouldi.cargo_audit import run_cargo_audit, run_cargo_build

Expand Down
2 changes: 1 addition & 1 deletion examples/shouldi/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import io
from unittest.mock import patch

from dffml.util.asynctestcase import AsyncTestCase
from dffml import AsyncTestCase

from shouldi.cli import ShouldI

Expand Down
8 changes: 5 additions & 3 deletions examples/shouldi/tests/test_dependency_check.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pathlib

from dffml.util.os import prepend_to_path
from dffml.util.net import cached_download_unpack_archive
from dffml.util.asynctestcase import AsyncTestCase
from dffml import (
prepend_to_path,
cached_download_unpack_archive,
AsyncTestCase,
)

from shouldi.dependency_check import run_dependency_check

Expand Down
8 changes: 5 additions & 3 deletions examples/shouldi/tests/test_golangci_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import copy
import pathlib

from dffml.util.os import prepend_to_path
from dffml.util.net import cached_download_unpack_archive
from dffml.util.asynctestcase import AsyncTestCase
from dffml import (
prepend_to_path,
cached_download_unpack_archive,
AsyncTestCase,
)

from shouldi.golangci_lint import run_golangci_lint

Expand Down
8 changes: 5 additions & 3 deletions examples/shouldi/tests/test_npm_audit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pathlib

from dffml.util.os import prepend_to_path
from dffml.util.net import cached_download_unpack_archive
from dffml.util.asynctestcase import AsyncTestCase
from dffml import (
prepend_to_path,
cached_download_unpack_archive,
AsyncTestCase,
)

from shouldi.npm_audit import run_npm_audit

Expand Down
2 changes: 1 addition & 1 deletion examples/shouldi/tests/test_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shutil
import tempfile

from dffml.util.asynctestcase import AsyncTestCase
from dffml import AsyncTestCase

from shouldi.pypi import pypi_package_json
from shouldi.pypi import pypi_latest_package_version
Expand Down
2 changes: 1 addition & 1 deletion examples/shouldi/tests/test_safety.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dffml.util.asynctestcase import AsyncTestCase
from dffml import AsyncTestCase

from shouldi.safety import safety_check

Expand Down
20 changes: 4 additions & 16 deletions examples/source/custom_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
from collections import OrderedDict
from typing import AsyncIterator, NamedTuple, Dict

from dffml.base import BaseConfig
from dffml.record import Record
from dffml.source.source import BaseSourceContext, BaseSource
from dffml.util.cli.arg import Arg
from dffml import config, Record, BaseSource, BaseSourceContext


class CustomSQLiteSourceConfig(BaseConfig, NamedTuple):
@config
class CustomSQLiteSourceConfig:
filename: str


Expand Down Expand Up @@ -77,6 +75,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):

class CustomSQLiteSource(BaseSource):

CONFIG = CustomSQLiteSourceConfig
CONTEXT = CustomSQLiteSourceContext
FEATURE_COLS = ["PetalLength", "PetalWidth", "SepalLength", "SepalWidth"]
PREDICTION_COLS = ["value", "confidence"]
Expand Down Expand Up @@ -104,14 +103,3 @@ async def __aenter__(self) -> "BaseSourceContext":

async def __aexit__(self, exc_type, exc_value, traceback):
await self.__db.__aexit__(exc_type, exc_value, traceback)

@classmethod
def args(cls, args, *above) -> Dict[str, Arg]:
cls.config_set(args, above, "filename", Arg())
return args

@classmethod
def config(cls, config, *above):
return CustomSQLiteSourceConfig(
filename=cls.config_get(config, above, "filename")
)
3 changes: 1 addition & 2 deletions examples/source/test_custom_sqlite.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest

from dffml.util.testing.source import FileSourceTest
from dffml.util.asynctestcase import AsyncTestCase
from dffml import AsyncTestCase, FileSourceTest

from .custom_sqlite import CustomSQLiteSourceConfig, CustomSQLiteSource

Expand Down
3 changes: 1 addition & 2 deletions examples/test_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import subprocess
import unittest.mock

from dffml.util.os import chdir
from dffml.util.asynctestcase import AsyncTestCase
from dffml import chdir, AsyncTestCase

from dffml_service_http.cli import HTTPService
from dffml_service_http.util.testing import ServerRunner
Expand Down

0 comments on commit 3b476b2

Please sign in to comment.