當前位置: 首頁>>代碼示例>>Python>>正文


Python Dependency.find_one方法代碼示例

本文整理匯總了Python中common.utilities.inversion_of_control.Dependency.find_one方法的典型用法代碼示例。如果您正苦於以下問題:Python Dependency.find_one方法的具體用法?Python Dependency.find_one怎麽用?Python Dependency.find_one使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在common.utilities.inversion_of_control.Dependency的用法示例。


在下文中一共展示了Dependency.find_one方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: GP16GetStoreWeatherLight

# 需要導入模塊: from common.utilities.inversion_of_control import Dependency [as 別名]
# 或者: from common.utilities.inversion_of_control.Dependency import find_one [as 別名]

#.........這裏部分代碼省略.........

        # allows you to pass global start/end dates.
        # for example if you want to process 1 day, not the entire store's lifetime
        # plan B allows these to be set to the default, which is None
        self.date_parser = FastDateParser()
        self._start_date = self.date_parser.parse_date(gp_start_date)
        self._end_date = self.date_parser.parse_date(gp_end_date)

    # ------------------------ GP Template Methods  ------------------------ #

    def _initialize(self):

        # get class member fields from entity, which is expected to be a trade_area
        self._latitude = self._entity["data"]["latitude"]
        self._longitude = self._entity["data"]["longitude"]
        self._store_id = str(self._entity["data"]["store_id"])
        self._company_id = str(self._entity["data"]["company_id"])

        # get opened/closed dates from trade area
        store_opened_date = self.date_parser.parse_date(self._entity['data']['store_opened_date'])
        store_closed_date = self.date_parser.parse_date(self._entity['data']['store_closed_date'])

        # set start/end dates to stores lifecycle, if it's not passed in
        if self._start_date is None:
            self._start_date = store_opened_date
        if self._end_date is None:
            self._end_date = store_closed_date

        # normalize to start/end of world in case everything is null...
        self._start_date = normalize_start_date(self._start_date)
        self._end_date = normalize_end_date(self._end_date)

        # if store has opened after gp16 start date, then only get weather after store opened
        if store_opened_date and store_opened_date > self._start_date:
            self._start_date = store_opened_date

        #if store has closed before gp16 end date, then only get weather until store closed
        if store_closed_date and store_closed_date < self._end_date:
            self._end_date = store_closed_date

    def _do_geoprocessing(self):

        latitude = self._latitude
        longitude = self._longitude

        repository = weather_repository.WeatherRepository()

        # find the closest precip/temp stations
        closest_stations = repository.select_best_temp_and_precip_stations(latitude, longitude, self._start_date, self._end_date)

        # get the data
        existing_store_weather_data = self._get_existing_store_weather_data()

        # set the keys
        self.existing_weather_code = existing_store_weather_data.get("weather_code", None)
        self.existing_temp_distance = existing_store_weather_data.get("temp_station_distance", None)
        self.existing_precip_distance = existing_store_weather_data.get("precip_station_distance", None)

        # get the unique combined codes for these stations
        self.weather_station_unique_combined_code = weather_helper.get_weather_station_code(closest_stations["precip_station_code"], closest_stations["temp_station_code"])
        self.temp_station_distance = closest_stations["temp_station_distance"]
        self.precip_station_distance = closest_stations["precip_station_distance"]

    def _preprocess_data_for_save(self):
        pass

    def _save_processed_data(self):

        # if store doesn't have weather code, or it changed, set it
        if self.existing_weather_code != self.weather_station_unique_combined_code or \
                self.existing_temp_distance != self.temp_station_distance or \
                self.existing_precip_distance != self.precip_station_distance:

            # update the store object and audit the change
            self._update_store_weather_code()

    # ------------------------ Internal Methods  ------------------------ #

    def _update_store_weather_code(self):

        # run update on store
        query = { "_id": ObjectId(self._store_id) }
        update = {
            "$set": {
                "data.weather_code": self.weather_station_unique_combined_code,
                "data.temp_station_distance": self.temp_station_distance,
                "data.precip_station_distance": self.precip_station_distance
            }
        }
        self.mds_db_access.update("store", query, update)

    def _get_existing_store_weather_data(self):

        # run query
        query = { "_id": ObjectId(self._store_id) }
        projection = { "data.weather_code": 1, "data.temp_station_distance": 1, "data.precip_station_distance": 1 }
        store = self.mds_db_access.find_one("store", query, projection)

        # return code
        return store["data"]
開發者ID:erezrubinstein,項目名稱:aa,代碼行數:104,代碼來源:gp16_get_store_weather_light.py

示例2: GP16Tests

# 需要導入模塊: from common.utilities.inversion_of_control import Dependency [as 別名]
# 或者: from common.utilities.inversion_of_control.Dependency import find_one [as 別名]

#.........這裏部分代碼省略.........
        gp.precip_station_distance = 2
        gp.weather_ids_to_delete = [1, 2, 3]

        # stub out some data
        self.mox.StubOutWithMock(weather_helper, "upsert_new_weather_data")
        self.mox.StubOutWithMock(gp, "_update_store_weather_code")
        self.mox.StubOutWithMock(weather_helper, "delete_existing_weather_records")

        # begin recording
        weather_helper.delete_existing_weather_records([1, 2, 3])

        # replay all
        self.mox.ReplayAll()

        # Big Poppa
        gp._save_processed_data()


    def test_save_processed_data__nothing_changes(self):

        # define the gp and its properties
        gp = GP16GetStoreWeather()
        gp._store_id = "whatever"
        gp.new_weather_data = []
        gp.existing_weather_code = "chicken_woot"
        gp.weather_station_unique_combined_code = "chicken_woot"
        gp.existing_temp_distance = 1
        gp.existing_precip_distance = 2
        gp.temp_station_distance = 1
        gp.precip_station_distance = 2
        gp.weather_ids_to_delete = []

        # stub out some data
        self.mox.StubOutWithMock(weather_helper, "upsert_new_weather_data")
        self.mox.StubOutWithMock(gp, "_update_store_weather_code")
        self.mox.StubOutWithMock(weather_helper, "delete_existing_weather_records")

        # replay all
        self.mox.ReplayAll()

        # Big Poppa
        gp._save_processed_data()


    def test_update_store_weather_code(self):

        # create the gop and mock some of its properties
        gp = GP16GetStoreWeather()
        gp._store_id = str(ObjectId())
        gp.existing_weather_code = "chilly_willy"
        gp.weather_station_unique_combined_code = "chicken_woot"
        gp.temp_station_distance = "tourettes"
        gp.precip_station_distance = "guy"
        gp._context = "hola"

        # stub out stuff
        self.mox.StubOutWithMock(datetime, "datetime")

        # create some mocks
        mock_query = { "_id": ObjectId(gp._store_id) }
        mock_update = {
            "$set": {
                "data.weather_code": "chicken_woot",
                "data.temp_station_distance": "tourettes",
                "data.precip_station_distance": "guy"
            }
        }

        # begin recording
        self.mock_mds_access.update("store", mock_query, mock_update)
        datetime.datetime.utcnow().AndReturn("date_1")
        self.mock_main_access.mds.call_add_audit("store", gp._store_id, "data.weather_code", "chilly_willy", "chicken_woot",
                                        "date_1", context = "hola")

        # replay all
        self.mox.ReplayAll()

        # I love gooooold!
        gp._update_store_weather_code()


    def test_get_existing_store_weather_data(self):

        # create the gop and mock some of its properties
        gp = GP16GetStoreWeather()
        gp._store_id = str(ObjectId())

        # create some mocks
        mock_query = { "_id": ObjectId(gp._store_id) }
        mock_projection = { "data.weather_code": 1, "data.temp_station_distance": 1, "data.precip_station_distance": 1 }
        mock_store = { "data": { "weather_code": "shalom" }}

        # record away
        self.mock_mds_access.find_one("store", mock_query, mock_projection).AndReturn(mock_store)

        # replay all
        self.mox.ReplayAll()

        # taco party!
        self.assertEqual(gp._get_existing_store_weather_data(), { "weather_code": "shalom" })
開發者ID:erezrubinstein,項目名稱:aa,代碼行數:104,代碼來源:test_gp16.py


注:本文中的common.utilities.inversion_of_control.Dependency.find_one方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。