Skip to content

Commit

Permalink
Merge pull request #35 from sara-nl/fix-scanner
Browse files Browse the repository at this point in the history
Minor fix in scanner example
  • Loading branch information
lnauta authored Jan 23, 2025
2 parents 32535d3 + c2cf68b commit 8e36b14
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ Now we want to select the tokens that have a specific number of cores, and start
To start scanning the different design documents, for example, to execute the work with different numbers of cores, run:

```
python core-scanner.py
python core_scanner.py
```

which will default to view `SingleCore` that was created above and filters on a core count of 1. This is equivalent to running explicitly:
Expand Down
26 changes: 20 additions & 6 deletions examples/core_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@

import logging
import sys
import argparse

import picasconfig
from picas.picaslogger import picaslogger
from picas.clients import CouchDB
from picas.executers import execute
from picas.util import arg_parser


picaslogger.propagate = False

# expand the parser for the example
parser = arg_parser()
parser.add_argument("--cores", default=1, type=str, help="Number of cores for the job")
parser.set_defaults(design_doc="SingleCore")
args = parser.parse_args()

def arg_parser():
"""
Arguments parser for optional values of the example
returns: argparse object
"""
parser = argparse.ArgumentParser(description="Arguments used in the different classes in the example.")
parser.add_argument("--design_doc", default="Monitor", type=str, help="Select the designdoc used by the actor class")
parser.add_argument("--view", default="todo", type=str, help="Select the view used by the actor class")
parser.add_argument("-v", "--verbose", action="store_true", help="Set verbose")
parser.add_argument("--cores", default=1, type=str, help="Number of cores for the job")
parser.set_defaults(design_doc="SingleCore")

return parser

# parse user arguments
args = arg_parser().parse_args()
# setup connection to db
client = CouchDB(url=picasconfig.PICAS_HOST_URL, db=picasconfig.PICAS_DATABASE, username=picasconfig.PICAS_USERNAME, password=picasconfig.PICAS_PASSWORD)

# check if there is work available, i.e. if there are tokens in the specified view
work_avail = client.is_view_nonempty(args.view, design_doc=args.design_doc)
if work_avail:
picaslogger.info(f"Starting a picas clients checking view {args.view} in design document {args.design_doc}")
Expand Down
File renamed without changes.
16 changes: 15 additions & 1 deletion examples/local_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,24 @@
from picas.iterators import TaskViewIterator
from picas.iterators import EndlessViewIterator
from picas.modifiers import BasicTokenModifier
from picas.util import Timer, arg_parser
from picas.util import Timer


log = logging.getLogger(__name__)


def arg_parser():
"""
Arguments parser for optional values of the example
returns: argparse object
"""
parser = argparse.ArgumentParser(description="Arguments used in the different classes in the example.")
parser.add_argument("--design_doc", default="Monitor", type=str, help="Select the designdoc used by the actor class")
parser.add_argument("--view", default="todo", type=str, help="Select the view used by the actor class")
parser.add_argument("-v", "--verbose", action="store_true", help="Set verbose")
return parser


class ExampleActor(RunActor):
"""
The ExampleActor is the custom implementation of a RunActor that the user needs for the processing.
Expand Down
6 changes: 3 additions & 3 deletions picas/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,17 @@ def is_view_nonempty(self, view, **view_params):
"""
# To ensure proper logging when design_doc is not passed into is_view_nonempty,
# the variable is created as the default used in self.view. Otherwise the f-string below breaks on default input.
design_doc = view_params.setdefault('design_doc', "Monitor")
design_doc = view_params.setdefault('design_doc', "Monitor")
try:
doc = self.get_single_from_view(view, **view_params)
task = Task(doc)
picaslogger.debug(doc)
picaslogger.debug(task['input'])
picaslogger.debug(task['_id'])
picaslogger.info(f"View {view} under design document {design_doc} is non-empty.")
return True
except IndexError as e:
picaslogger.info(f"View {view} under design document {design_doc} is empty: {e}")
return False
except ResourceNotFound as e:
except ResourceNotFound:
picaslogger.info(f"Non-existing view and design document passed: {view} in {design_doc}")
return False
13 changes: 0 additions & 13 deletions picas/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
@author Joris Borgdorff
"""

import argparse
import time
from copy import deepcopy

Expand Down Expand Up @@ -30,18 +29,6 @@ def seconds():
return int(time.time())


def arg_parser():
"""
Arguments parser for optional values of the example
returns: argparse object
"""
parser = argparse.ArgumentParser(description="Arguments used in the different classes in the example.")
parser.add_argument("--design_doc", default="Monitor", type=str, help="Select the designdoc used by the actor class")
parser.add_argument("--view", default="todo", type=str, help="Select the view used by the actor class")
parser.add_argument("-v", "--verbose", action="store_true", help="Set verbose")
return parser


class Timer:
"""Timer class"""

Expand Down
60 changes: 53 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e36b14

Please sign in to comment.