Skip to content

Instantly share code, notes, and snippets.

@sanfx sanfx/filterList.py
Created Oct 12, 2013

Embed
What would you like to do?
This script is for purpose to explain how logging works as well how mox testing done; see test_filterList.py
import sys
import ast
import logging
class ItemNotString(Exception):
pass
class FilterList(object):
""" docstring for FilterList
"""
def __init__(self, filterby):
super(FilterList, self).__init__()
self._filter = filterby
def stringListItems(self, lst):
for index, item in enumerate(lst):
if type(item) == str:
continue
else:
raise ItemNotString("%i item '%s' is not of type string" % (index+1, item))
return True
def filterList(self, lst):
if self.stringListItems(lst):
if not self._filter:
logging.info("Nothing passed to filter list %s" % lst)
return
filteredList = []
for eachItem in lst:
if eachItem.startswith(self._filter):
filteredList.append(eachItem)
if filteredList == []:
logging.info("The list %s doesn't have any matching string item starting with letter '%s'." %(lst, self._filter))
return
return filteredList
if __name__ == "__main__":
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
# logging.basicConfig(filename='myapp.log', level=logging.INFO)
try:
filterby = sys.argv[2]
except IndexError:
filterby = None
flObj = FilterList(filterby)
result = flObj.filterList(ast.literal_eval(sys.argv[1]))
if result:
logging.info(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.