Sep-28-2017, 09:19 AM
HI,
I am trying to understand how the module python-craigslist works.
The __init__.py file is too long to be posted here, so I cut a simplified version to give an idea.
The code is run as following:
Can anyone explain it to me?
Thank you
PS: Here's the link to the original module, with the full functioning code.
I am trying to understand how the module python-craigslist works.
The __init__.py file is too long to be posted here, so I cut a simplified version to give an idea.
from six import iteritems class CraigslistBase(object): """ Base class for all Craiglist wrappers. """ base_filters = { 'query': {'url_key': 'query', 'value': None}, 'search_titles': {'url_key': 'srchType', 'value': 'T'}, 'has_image': {'url_key': 'hasPic', 'value': 1}, 'posted_today': {'url_key': 'postedToday', 'value': 1}, 'search_distance': {'url_key': 'search_distance', 'value': None}, 'zip_code': {'url_key': 'postal', 'value': None}, } extra_filters = {} def __init__(self, filters=None): self.filters = {} for key, value in iteritems((filters or {})): filter = (self.base_filters.get(key) or self.extra_filters.get(key)) if filter['value'] is None: self.filters[filter['url_key']] = value elif isinstance(filter['value'], list): valid_options = filter['value'] if not hasattr(value, '__iter__'): value = [value] # Force to list options = [] for opt in value: options.append(valid_options.index(opt) + 1) self.filters[filter['url_key']] = options elif value: # Don't add filter if ...=False self.filters[filter['url_key']] = filter['value'] class CraigslistHousing(CraigslistBase): """ Craigslist housing wrapper. """ default_category = 'hhh' custom_result_fields = True extra_filters = { 'private_room': {'url_key': 'private_room', 'value': 1}, 'private_bath': {'url_key': 'private_bath', 'value': 1}, 'cats_ok': {'url_key': 'pets_cat', 'value': 1}, 'dogs_ok': {'url_key': 'pets_dog', 'value': 1}, 'min_price': {'url_key': 'min_price', 'value': None}, 'max_price': {'url_key': 'max_price', 'value': None}, 'min_ft2': {'url_key': 'minSqft', 'value': None}, 'max_ft2': {'url_key': 'maxSqft', 'value': None}, 'min_bedrooms': {'url_key': 'min_bedrooms', 'value': None}, 'max_bedrooms': {'url_key': 'max_bedrooms', 'value': None}, 'min_bathrooms': {'url_key': 'min_bathrooms', 'value': None}, 'max_bathrooms': {'url_key': 'max_bathrooms', 'value': None}, 'no_smoking': {'url_key': 'no_smoking', 'value': 1}, 'is_furnished': {'url_key': 'is_furnished', 'value': 1}, 'wheelchair_acccess': {'url_key': 'wheelchaccess', 'value': 1}, }
The code is run as following:
ch_l = CraigslistHousing(filters={'max_price': 1200, 'private_room': True}) print(ch_l.filters)I am interested in what happens in line 28. As I understand, we pass "filters" as a dictionary through the sub-class CraigslistHousing. I'd assume the code would loop through that but, before the loop, the code sets
filters = {}As far as I understand, this overrides what we passed as a parameter to the function but clearly that is not the case.
Can anyone explain it to me?
Thank you
PS: Here's the link to the original module, with the full functioning code.