Python Forum
Understanding the module python-craigslist
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding the module python-craigslist
#1
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.
Reply
#2
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
Reply
#3
Sure, I didn't notice they were two different variables!
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  updating pip, python - understanding terminal and pycharm. endlessnameless 0 2,251 Jun-24-2024, 05:48 AM
Last Post: endlessnameless
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 8,794 May-10-2023, 02:13 PM
Last Post: Calab
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 4,550 May-09-2023, 08:47 AM
Last Post: buran
  Understanding Python classes PythonNewbee 3 2,755 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  Understanding Python super() for classes OmegaRed94 1 2,832 Jun-09-2021, 09:02 AM
Last Post: buran
  Better Understanding Of Object Orientation In Python JoeDainton123 3 4,386 Aug-30-2020, 02:49 PM
Last Post: deanhystad
  Understanding Python's Import Engine MysticaL 1 3,503 Feb-07-2020, 11:26 PM
Last Post: snippsat
  Help with understanding a python package pyhill00 4 4,941 Mar-21-2019, 12:42 AM
Last Post: Larz60+
  Understanding if Statements in Python Kathleen 1 3,612 Mar-05-2019, 07:55 PM
Last Post: Yoriz
  Understanding Scoping in Python yksingh1097 5 5,931 Aug-06-2018, 07:42 PM
Last Post: nilamo

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.