def point(faker, field, srid): lat = random.uniform(-180.0, 180.0) lng = random.uniform(-90, 90) if field.dim == 2: return geos.Point(lat, lng, srid=srid) else: alt = random.uniform(-4000.0, 9000.0) return geos.Point(lat, lng, alt, srid=srid)
def geo_point(self, lon_min=-180.0, lon_max=180.0, lat_min=-90.0, lat_max=90.0): assert isinstance(lon_min, float) assert isinstance(lon_max, float) assert isinstance(lat_min, float) assert isinstance(lat_max, float) # longitude: -180 .. 0 .. +180 (E-W) # latitude: -90 .. 0 .. +90 (S-N) return [ random.uniform(lon_min, lon_max), random.uniform(lat_min, lat_max) ]
def test_time_series(self): from faker.providers.date_time import Provider series = [i for i in Provider.time_series()] self.assertTrue(len(series), 30) self.assertTrue(series[1][0] - series[0][0], timedelta(days=1)) uniform = lambda dt: random.uniform(0, 5) # noqa series = [ i for i in Provider.time_series('now', '+1w', '+1d', uniform) ] self.assertTrue(len(series), 7) self.assertTrue(series[1][0] - series[0][0], timedelta(days=1)) end = datetime.now() + timedelta(days=7) series = [i for i in Provider.time_series('now', end, '+1d', uniform)] self.assertTrue(len(series), 7) self.assertTrue(series[1][0] - series[0][0], timedelta(days=1)) self.assertTrue(series[-1][0] <= end) with self.assertRaises(ValueError): [i for i in Provider.time_series('+1w', 'now', '+1d', uniform)] with self.assertRaises(ValueError): [i for i in Provider.time_series('now', '+1w', '+1d', 'uniform')] series = [ i for i in Provider.time_series( 'now', end, '+1d', uniform, tzinfo=utc) ] self.assertTrue(len(series), 7) self.assertTrue(series[1][0] - series[0][0], timedelta(days=1))
def geo_coordinate(cls, center=None, radius=0.001): """ Optionally center the coord and pick a point within radius. """ if not center: return Decimal(str(random.randint(-180000000, 180000000) / 1000000.0)).quantize(Decimal('.000001')) else: geo = random.uniform(center - radius, center + radius) return Decimal(str(geo))
def cust_id(self): id = int(random.uniform(1000000, 9999999)) return str(id)
def floatrange(faker, field, *args, **kwargs): lower = random.uniform(-2147483647, 2147483646) upper = random.uniform(lower, 2147483647) return NumericRange(lower, upper)
def random_sample(): return random.uniform(0.0, 1.0)
def _get_random_birthday(cls): now = datetime.datetime.now() days = random.uniform(-7300, -21900) delta = datetime.timedelta(days=days) n_days = now + delta return n_days.strftime('%Y%m%d')
def _get_order_code(cls, sex): code = int(random.uniform(1, 998)) SEX_FIX = {'F': 0, 'M': 1} if code % 2 != SEX_FIX[sex]: code += 1 return str(code).zfill(3)
def fake_item(self, cpv_group=None): """ Generate a random item for openprocurement tenders :param cpv_group: gives possibility to generate items from a specific cpv group. Cpv group is three digits in the beginning of each cpv id. """ item_base_data = None cpv = None if cpv_group is None: cpv = self.random_element(self.cpvs) elif cpv_group == 336: cpv = self.random_element(self.moz_cpvs) elif cpv_group == 'road': cpv = self.random_element(self.road_cpvs) elif cpv_group == 'gmdn': cpv = self.random_element(self.gmdn_cpvs) else: cpv_group = str(cpv_group) similar_cpvs = [] for cpv_element in self.cpvs: if cpv_element.startswith(cpv_group): similar_cpvs.append(cpv_element) else: for cpv_element in self.moz_cpvs: if cpv_element.startswith(cpv_group): similar_cpvs.append(cpv_element) cpv = self.random_element(similar_cpvs) for entity in self.items_base_data: if entity["cpv_id"] == cpv: item_base_data = entity break if not item_base_data: raise ValueError('unable to find an item with CPV ' + cpv) # choose appropriate additional classification for item_base_data's cpv additional_class = [] for entity in self.classifications: if entity["classification"]["id"] == item_base_data["cpv_id"]: additional_class.append(entity) if not additional_class: raise ValueError( 'unable to find a matching additional classification for CPV ' + cpv) classification = self.random_element(additional_class) dk_descriptions = { u'ДК003': (u'Послуги фахівців', u'Услуги специалистов', u'Specialists services'), u'ДК015': (u'Дослідження та розробки', u'Исследования и разработки', u'Research and development'), u'ДК018': (u'Будівлі та споруди', u'Здания и сооружения', u'Buildings and structures') } address = self.random_element(self.addresses) item = { "classification": classification["classification"], "deliveryAddress": address["deliveryAddress"], "deliveryLocation": address["deliveryLocation"], "unit": item_base_data["unit"], "quantity": round(random.uniform(3, 150), 3) } if item_base_data["cpv_id"] == "99999999-9": scheme = classification["additionalClassifications"][0]["scheme"] item.update({ "description": dk_descriptions[scheme][0], "description_ru": dk_descriptions[scheme][1], "description_en": dk_descriptions[scheme][2] }) else: item.update({ "additionalClassifications": classification["additionalClassifications"], "description": item_base_data["description"], "description_ru": item_base_data["description_ru"], "description_en": item_base_data["description_en"] }) return deepcopy(item)