Python Forum
Understanding the module python-craigslist - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Understanding the module python-craigslist (/thread-5317.html)



Understanding the module python-craigslist - duccio - Sep-28-2017

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.

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.


RE: Understanding the module python-craigslist - buran - Sep-28-2017

you need to make difference between filters that is passed as argument when instantiate the object and self.filters which is filters attribute of the [instance of] the class.
On line 28 it sets self.filters to be empty dict and then it iterates over items in the filters that you pass when create/init the class instance and populates the self.filters. The reason - it that does need some preprocessing of the filters argument key/values


RE: Understanding the module python-craigslist - duccio - Sep-29-2017

Sure, I didn't notice they were two different variables!
Thank you