Best Python code snippet using gherkin-python
test_dsfaker.py
Source:test_dsfaker.py
...18class TestGenerator:19 def test_raises(self):20 g = Generator()21 with pytest.raises(NotImplementedError):22 g.get_single()23 with pytest.raises(NotImplementedError):24 g.get_batch(200)25 def test_stream_single(self):26 g = Autoincrement()27 c = 028 for i in range(1000):29 for val in g.stream_single():30 assert val == c31 c += 132 if c > 100000:33 break34 def test_stream_batch(self):35 g = Autoincrement()36 c = 037 for i in range(10):38 nb = np.random.randint(2, 1000)39 for values in g.stream_batch(nb):40 for val in values:41 assert val == c42 c += 143 if c > 100000:44 break45 def test_copy(self):46 g1 = Autoincrement()47 g2 = g1.copy()48 g2.get_single()49 assert g1.get_single() == g2.get_single() - 150 def _get_two_unique_gen(self):51 va = np.random.randint(-1000, +1000, dtype=np.int32)52 vb = np.random.randint(0, 10000, dtype=np.int32)53 a = ConstantValueGenerator(va, dtype=np.int64)54 b = ConstantValueGenerator(vb, dtype=np.int64)55 return va, vb, a, b56 def test_add_op(self):57 va, vb, a, b = self._get_two_unique_gen()58 c = a + b59 assert c.get_single() == va + vb60 def test_add_op_scalar(self):61 va, vb, a, b = self._get_two_unique_gen()62 assert (2 + a).get_single() == 2 + va63 assert (b + 6).get_single() == vb + 664 assert sum((7 + b).get_batch(10)) == 10 * (7 + vb)65 def test_sub_op(self):66 va, vb, a, b = self._get_two_unique_gen()67 c = a - b68 assert c.get_single() == va - vb69 def test_sub_op_scalar(self):70 va, vb, a, b = self._get_two_unique_gen()71 assert (2 - a).get_single() == 2 - va72 assert (b - 6).get_single() == vb - 673 def test_truediv_op(self):74 va, vb, a, b = self._get_two_unique_gen()75 c = a / b76 assert c.get_single() == va / vb77 def test_truediv_op_scalar(self):78 va, vb, a, b = self._get_two_unique_gen()79 assert (2 / a).get_single() == 2 / va80 assert (b / 6).get_single() == vb / 681 def test_floordiv_op(self):82 va, vb, a, b = self._get_two_unique_gen()83 c = a // b84 assert c.get_single() == va // vb85 def test_floordiv_op_scalar(self):86 va, vb, a, b = self._get_two_unique_gen()87 assert (2 // a).get_single() == 2 // va88 assert (b // 6).get_single() == vb // 689 def test_mul_op(self):90 va, vb, a, b = self._get_two_unique_gen()91 c = a * b92 assert c.get_single() == va * vb93 def test_mul_op_scalar(self):94 va, vb, a, b = self._get_two_unique_gen()95 assert (2 * a).get_single() == 2 * va96 assert (b * 6).get_single() == vb * 697 def test_pow_op(self):98 va, vb, a, b = self._get_two_unique_gen()99 c = a ** b100 assert c.get_single() == va ** vb101 def test_pow_op_scalar(self):102 va, vb, a, b = self._get_two_unique_gen()103 assert (2 ** b).get_single() == 2 ** vb104 assert (a ** 6).get_single() == va ** 6105 def test_mod_op(self):106 va, vb, a, b = self._get_two_unique_gen()107 c = a % b108 assert c.get_single() == va % vb109 def test_mod_op_scalar(self):110 va, vb, a, b = self._get_two_unique_gen()111 assert (2 % a).get_single() == 2 % va112 assert (b % 6).get_single() == vb % 6113 def _get_two_unique_gen_binary(self):114 va = np.random.randint(0, 2, dtype=np.int32)115 vb = np.random.randint(0, 2, dtype=np.int32)116 a = ConstantValueGenerator(va, dtype=np.int64)117 b = ConstantValueGenerator(vb, dtype=np.int64)118 return va, vb, a, b119 def test_and_op(self):120 va, vb, a, b = self._get_two_unique_gen_binary()121 c = a & b122 assert c.get_single() == va & vb123 def test_and_op_scalar(self):124 va, vb, a, b = self._get_two_unique_gen_binary()125 assert (1 & b).get_single() == 1 & vb126 assert (a & 1).get_single() == va & 1127 assert (0 & b).get_single() == 0 & vb128 assert (a & 0).get_single() == va & 0129 def test_or_op(self):130 va, vb, a, b = self._get_two_unique_gen_binary()131 c = a | b132 assert c.get_single() == va | vb133 def test_or_op_scalar(self):134 va, vb, a, b = self._get_two_unique_gen_binary()135 assert (1 | b).get_single() == 1 | vb136 assert (a | 1).get_single() == va | 1137 assert (0 | b).get_single() == 0 | vb138 assert (a | 0).get_single() == va | 0139 def test_xor_op(self):140 va, vb, a, b = self._get_two_unique_gen_binary()141 c = a ^ b142 assert c.get_single() == va ^ vb143 def test_xor_op_scalar(self):144 va, vb, a, b = self._get_two_unique_gen_binary()145 assert (1 ^ b).get_single() == 1 ^ vb146 assert (a ^ 1).get_single() == va ^ 1147 assert (0 ^ b).get_single() == 0 ^ vb148 assert (a ^ 0).get_single() == va ^ 0149 def test_neg_op(self):150 v = np.random.randint(-1000, +1000, dtype=np.int32)151 c = - ConstantValueGenerator(v, dtype=np.int64)152 assert c.get_single() == - v153class TestBoundingOperator:154 def test_values_single(self):155 n = Sin() * ConstantValueGenerator(50, dtype=np.uint16)156 lb = 0157 ub = 0158 while lb >= ub:159 lb = np.random.randint(-20, 20)160 ub = np.random.randint(-20, 20)161 bg = BoundingOperator(n, lb=lb, ub=ub)162 for i in range(10000):163 assert lb <= bg.get_single() <= ub164 def test_values_batch(self):165 n = Sin() * ConstantValueGenerator(50, dtype=np.uint16)166 lb = 0167 ub = 0168 while lb >= ub:169 lb = np.random.randint(-20, 20)170 ub = np.random.randint(-20, 20)171 bg = BoundingOperator(n, lb=lb, ub=ub)172 for i in range(10):173 nb = np.random.randint(2, 10000)174 values = bg.get_batch(nb)175 for val in values:176 assert lb <= val <= ub177class TestScalingOperator:178 def test_raises(self):179 with pytest.raises(ValueError):180 ScalingOperator(generator=None, lb=10, ub=0, dtype=np.int8)181 with pytest.raises(NotCompatibleGeneratorException):182 ScalingOperator(generator=Generator(), lb=0, ub=10, dtype=np.int8)183 def test_values_single(self):184 triangular_fun = BoundingOperator(185 ApplyFunctionOperator(function=lambda x: abs((x % 4) - 2) - 1, generator=Autoincrement()), lb=-1, ub=1)186 n = ScalingOperator(generator=triangular_fun, lb=-10, ub=10, dtype=np.float32)187 for _ in range(10000):188 assert n.get_single() == 10189 assert n.get_single() == 0190 assert n.get_single() == -10191 assert n.get_single() == 0192 def test_values_batch(self):193 triangular_fun = BoundingOperator(194 ApplyFunctionOperator(function=lambda x: abs((x % 4) - 2) - 1, generator=Autoincrement()), lb=-1, ub=1)195 n = ScalingOperator(generator=triangular_fun, lb=-10, ub=10, dtype=np.float32)196 tmp = [10, 0, -10, 0]197 count = 0198 for i in range(10):199 nb = np.random.randint(2, 1000)200 values = n.get_batch(nb)201 for j, val in enumerate(values):202 assert val == tmp[(count + j) % 4]203 count += nb204class TestApplyFunctionOperator:205 def _get_afo(self, fun):206 generator = Autoincrement()207 afo = ApplyFunctionOperator(function=fun, generator=generator)208 return afo209 def test_type(self):210 afo = self._get_afo(lambda x: 2 * x)211 assert afo.get_single() == 0212 assert isinstance(afo.get_batch(9), np.ndarray)213 def test_shape(self):214 afo = self._get_afo(lambda x: 2 * x)215 nb = np.random.randint(2, 10000)216 assert afo.get_batch(nb).shape == (nb,)217 def test_values_single(self):218 afo = self._get_afo(lambda x: 4 * x + 2)219 for i in range(10000):220 assert afo.get_single() == 4 * i + 2221 def test_values_batch(self):222 afo = self._get_afo(lambda x: 4 * x + 2)223 count = 0224 for i in range(10):225 nb = np.random.randint(2, 10000)226 values = afo.get_batch(nb)227 for j, val in enumerate(values):228 assert val == (count + j) * 4 + 2229 count += nb230class TestAbsoluteOperator:231 def test_values_single(self):232 n = Cos() * ConstantValueGenerator(50, dtype=np.uint16)233 ao = AbsoluteOperator(n)234 for i in range(10000):235 assert ao.get_single() >= 0236 def test_values_batch(self):237 n = Cos() * ConstantValueGenerator(50, dtype=np.uint16)238 ao = AbsoluteOperator(n)239 for i in range(10):240 nb = np.random.randint(2, 10000)241 values = ao.get_batch(nb)242 for val in values:243 assert val >= 0244class TestRepeatPattern:245 def test_type(self):246 rp = RepeatPattern([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])247 assert rp.get_single() == 0248 assert isinstance(rp.get_batch(9), np.ndarray)249 def test_shape(self):250 rp = RepeatPattern([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])251 nb = np.random.randint(2, 10000)252 assert rp.get_batch(nb).shape == (nb,)253 def test_len(self):254 rp = RepeatPattern([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])255 for i in range(10):256 nb = np.random.randint(2, 100000)257 print(nb)258 assert len(rp.get_batch(nb)) == nb259 def test_values_single(self):260 rp = RepeatPattern([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])261 for i in range(10000):262 assert rp.get_single() == i % 10263 def test_values_batch(self):264 rp = RepeatPattern([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])265 idx = 0266 for _ in range(10):267 nb = np.random.randint(2, 10000)268 print(nb)269 for val in rp.get_batch(nb):270 assert val == idx % 10271 idx += 1272class TestAutoincrement:273 def test_type(self):274 ai = Autoincrement(start=-42, step=+42, dtype=np.int32)275 assert ai.get_single() == -42276 assert isinstance(ai.get_batch(42), np.ndarray)277 def test_shape(self):278 ai = Autoincrement(start=-42, step=+42, dtype=np.int32)279 nb = np.random.randint(2, 10000)280 assert ai.get_batch(nb).shape == (nb,)281 def test_values_single(self):282 ai = Autoincrement(start=-42, step=+42, dtype=np.int32)283 for i in range(np.random.randint(10, 10000)):284 val = ai.get_single()285 assert val == -42 + i * 42286 def test_values_batch(self):287 ai = Autoincrement(start=-4.2, step=+4.2, dtype=np.float64)288 count = 0289 for i in range(10):290 nb = np.random.randint(2, 10000)291 values = ai.get_batch(nb)292 for j, val in enumerate(values):293 d = (Decimal(-4.2) + ((Decimal(count + j)) * Decimal(4.2)))294 assert round(Decimal(val), 1) == round(d, 1)295 count += nb296class TestAutoincrementRandom:297 def _get_gen(self):298 start = np.random.randint(-50, +50)299 step = np.random.randint(-10, 10)300 gen = ConstantValueGenerator(step, dtype=np.int32)301 ai = AutoincrementWithGenerator(start=start, generator=gen)302 return start, step, ai303 def test_type(self):304 start, step, ai = self._get_gen()305 assert ai.get_single() == start306 assert isinstance(ai.get_batch(42), np.ndarray)307 def test_shape(self):308 start, step, ai = self._get_gen()309 nb = np.random.randint(2, 100000)310 assert ai.get_batch(nb).shape == (nb,)311 def test_values_single(self):312 start, step, ai = self._get_gen()313 for i in range(np.random.randint(10, 10000)):314 val = ai.get_single()315 assert val == start + i * step316 def test_values_batch(self):317 start, step, ai = self._get_gen()318 count = 0319 for i in range(10):320 nb = np.random.randint(2, 10000)321 values = ai.get_batch(nb)322 for j, val in enumerate(values):323 assert val == start + (count + j) * step324 count += nb325class TestTimeSeries:326 def _get_ts(self):327 time = Autoincrement()328 data = ConstantValueGenerator(value=42, dtype=np.uint16)329 return TimeSeries(time_gen=time, data_gen=data)330 def test_type(self):331 ts = self._get_ts()332 assert ts.get_single() == (0, 42)333 assert isinstance(ts.get_batch(9)[0], np.ndarray)334 assert isinstance(ts.get_batch(9)[1], np.ndarray)335 def test_values_single(self):336 ts = self._get_ts()337 for i in range(1000):338 assert ts.get_single() == (i, 42)339 def test_values_batch(self):340 ts = self._get_ts()341 for i in range(10):342 tt, vv = ts.get_batch(100)343 for j, t in enumerate(tt):344 assert t == 100 * i + j345 for j, v in enumerate(vv):346 assert v == 42347class TestDate:348 def test_values_single(self):349 triangular_fun = BoundingOperator(350 ApplyFunctionOperator(function=lambda x: abs((x % 4) - 2) - 1, generator=Autoincrement()), lb=-1, ub=1)351 rd = RandomDatetime(generator=triangular_fun, start=np.datetime64("1950"), end=np.datetime64("2042"), unit="Y")352 for i in range(10000):353 assert rd.get_single() == np.datetime64('2042')354 assert rd.get_single() == np.datetime64('1996')355 assert rd.get_single() == np.datetime64('1950')356 assert rd.get_single() == np.datetime64('1996')357 def test_values_batch(self):358 triangular_fun = BoundingOperator(359 ApplyFunctionOperator(function=lambda x: abs((x % 4) - 2) - 1, generator=Autoincrement()), lb=-1, ub=1)360 rd = RandomDatetime(generator=triangular_fun, start=np.datetime64("1950"), end=np.datetime64("2042"), unit="Y")361 tmp = [np.datetime64('2042'), np.datetime64('1996'), np.datetime64('1950'), np.datetime64('1996')]362 count = 0363 for i in range(10):364 nb = np.random.randint(2, 1000)365 values = rd.get_batch(nb)366 for j, val in enumerate(values):367 assert val == tmp[(count + j) % 4]368 count += nb369class TestDistribution:370 def test_raise(self):371 with pytest.raises(NotImplementedError):372 d = Distribution()373 d.get_single()374 with pytest.raises(NotImplementedError):375 d = Distribution()376 d.get_batch(10)377class TestDistributions:378 def _get_all_distributions(self):379 distributions = [380 Beta(a=2, b=2),381 Binomial(n=20, p=0.5),382 BinomialNegative(n=20, p=0.5),383 CauchyStandard(),384 Chisquare(k=6),385 ChisquareNonCentral(k=4, nonc=1),386 Dirichlet(alpha=[0.3, 0.3, 0.3]),387 Exponential(),388 F(dfnum=5, dfden=2),389 FNonCentral(dfnum=10, dfden=20, nonc=5),390 Gamma(k=1.0),391 Geometric(p=0.5),392 Gumbel(),393 Hypergeometric(n=50, m=50, N=80),394 Laplace(mu=-5, beta=4),395 Logistic(mu=6, beta=2),396 Lognormal(mu=0, sigma=1.5),397 Lomax(a=2),398 Multinomial(n=10, pvals=[1 / 10 for _ in range(10)]),399 Normal(),400 NormalMultivariate(mu=[0, 0], cov=[[1, 0], [0, 100]]),401 Poisson(lam=4),402 Power(a=2),403 Randint(lb=-10, ub=20),404 RandomSample(),405 Rayleigh(sigma=1),406 Triangular(left=10, mode=22, right=42),407 Uniform(lb=-42, ub=84),408 Vonmises(mu=-1, kappa=1),409 Wald(mu=2, lam=0.2),410 Weibull(a=0.5),411 Zipf(a=2),412 Choice(probabilities=[.05, .15, .05, .20, .25, .10, .20])413 ]414 return distributions415 def test_attributes(self):416 for d in self._get_all_distributions():417 if isinstance(d, DistributionUnbounded):418 assert d.lb is None419 assert d.ub is None420 elif isinstance(d, DistributionNonNegative):421 assert d.lb is not None422 assert d.ub is None423 elif isinstance(d, DistributionBounded):424 assert d.lb is not None425 assert d.ub is not None426 def test_bounds_single(self):427 for d in self._get_all_distributions():428 if isinstance(d, DistributionBounded):429 for _ in range(10000):430 v = d.get_single()431 assert d.lb <= v <= d.ub432 elif isinstance(d, DistributionNonNegative):433 for _ in range(10000):434 v = d.get_single()435 if isinstance(v, np.ndarray):436 for vv in v:437 assert d.lb <= vv438 else:439 assert d.lb <= v440 elif isinstance(d, DistributionUnbounded):441 for _ in range(10000):442 v = d.get_single()443 def test_bounds_batch(self):444 for d in self._get_all_distributions():445 if isinstance(d, DistributionBounded):446 for v in d.get_batch(10000):447 assert d.lb <= v <= d.ub448 elif isinstance(d, DistributionNonNegative):449 for v in d.get_batch(10000):450 if isinstance(v, np.ndarray):451 for vv in v:452 assert d.lb <= vv453 else:454 assert d.lb <= v455 elif isinstance(d, DistributionUnbounded):456 d.get_batch(10000)457class TestTrigo:458 def _get_all(self):459 functions = [460 Sin(),461 Sinh(),462 Cos(),463 Cosh(),464 Tan(),465 Tanh()466 ]467 return functions468 def test_bounds_single(self):469 for f in self._get_all():470 if isinstance(f, BoundedGenerator):471 for _ in range(10000):472 assert f.lb <= f.get_single() <= f.ub473 else:474 for _ in range(10000):475 f.get_single()476 def test_bounds_batch(self):477 for f in self._get_all():478 if isinstance(f, BoundedGenerator):479 for v in f.get_batch(10000):480 assert f.lb <= v <= f.ub481 else:482 f.get_batch(10000)483class TestCastGenerator:484 def test_types(self):485 n = Normal()486 cg = CastOperator(generator=n, dtype=np.int16)487 cg.get_single()488 assert cg.get_batch(1000).dtype == np.int16489class TestTimeDelayedGenerator:490 def test_values_single(self):491 time_delay_sec = 0.005492 tdg = TimeDelayedGenerator(generator=ConstantValueGenerator(21, dtype=np.uint16), time_delay_sec=time_delay_sec)493 start_time = datetime.datetime.now()494 for _ in range(100):495 tdg.get_single()496 end_time = datetime.datetime.now()497 elapsed_timedelta = (end_time - start_time)498 assert datetime.timedelta(seconds=.47) <= elapsed_timedelta <= datetime.timedelta(seconds=.53)499 tdg = TimeDelayedGenerator(generator=ConstantValueGenerator(21, dtype=np.uint16),500 time_delay_generator=ConstantValueGenerator(time_delay_sec, dtype=np.float16))501 start_time = datetime.datetime.now()502 for _ in range(100):503 tdg.get_single()504 end_time = datetime.datetime.now()505 elapsed_timedelta = (end_time - start_time)506 assert datetime.timedelta(seconds=.47) <= elapsed_timedelta <= datetime.timedelta(seconds=.53)507 def test_values_batch(self):508 time_delay_sec = 0.0005509 tdg = TimeDelayedGenerator(generator=ConstantValueGenerator(21, dtype=np.uint16), time_delay_sec=time_delay_sec)510 start_time = datetime.datetime.now()511 for _ in range(10):512 tdg.get_batch(100)513 end_time = datetime.datetime.now()514 elapsed_timedelta = (end_time - start_time)515 assert datetime.timedelta(seconds=.47) <= elapsed_timedelta <= datetime.timedelta(seconds=.53)516 tdg = TimeDelayedGenerator(generator=ConstantValueGenerator(21, dtype=np.uint16),517 time_delay_generator=ConstantValueGenerator(time_delay_sec, dtype=np.float16))518 start_time = datetime.datetime.now()519 for _ in range(10):520 tdg.get_batch(100)521 end_time = datetime.datetime.now()522 elapsed_timedelta = (end_time - start_time)523 assert datetime.timedelta(seconds=.47) <= elapsed_timedelta <= datetime.timedelta(seconds=.53)524class TestHistory:525 def test_values_single(self):526 gen = History(Autoincrement(), 42)527 for i in range(10):528 assert i == gen.get_single()529 for i in range(10):530 assert gen.get_prev(-10+i) == i531 for i in range(10):532 assert i + 10 == gen.get_single()533 for i in range(10):534 assert gen.get_prev(-10+i) == i + 10535 def test_values_batch(self):536 gen = History(Autoincrement(), 42)537 for i, v in enumerate(gen.get_batch(10)):538 assert i == v539 for i in range(10):540 assert gen.get_prev(-10+i) == i541class TestMeanHistory:542 def test_values_single(self):543 gen = MeanHistory(Autoincrement(start=4), 4, initial_values=[0,1,2,3])544 for i in range(42):545 assert gen.get_single() == (i * 4.0 + 6.0) / 4.0546 def test_values_batch(self):547 gen = MeanHistory(Autoincrement(start=4), 4, initial_values=[0,1,2,3])548 for i in range(42):549 for j, v in enumerate(gen.get_batch(10)):550 assert v == ((i * 10 + j) * 4.0 + 6.0) / 4.0551class TestRegex:552 def test_values_single(self):553 patterns = [r'(0|\+33|0033)[1-9][0-9]{8}']554 for pattern in patterns:555 gen = Regex(pattern)556 for i in range(42):557 assert re.fullmatch(pattern, gen.get_single()) is not None558 def test_values_batch(self):559 patterns = [r'(0|\+33|0033)[1-9][0-9]{8}']560 for pattern in patterns:561 gen = Regex(pattern)562 for i in range(42):563 for e in gen.get_batch(10):...
main.py
Source:main.py
...83 for output_file in district_output_files:84 for district_name, district in output_file[1].items():85 for title in ['show_on_uncolonized', 'potential']:86 for other_build_restriction in other_build_restrictions:87 district[0].get_single(title).ensure(other_build_restriction)88 ###################89 # auto patch mods #90 ###################91 for i, other_mod in enumerate(mod_collection):92 print(other_mod)93 # load mod data94 try:95 mod_folder = extract_mod(workshop_folder, other_mod[0])96 except IndexError:97 print('Skipping mod with id: ' + other_mod[0])98 continue99 mod_file_names = os.listdir(mod_folder + '/common/districts')100 mod_districts = {}101 for mod_file_name in mod_file_names:102 mod_file = parse_file(mod_folder + '/common/districts/' + mod_file_name)103 if mod_file_name not in district_files_overwritten:104 for district_name in districts_overwritten:105 if district_name in mod_file:106 district_files_overwritten.append(mod_file_name)107 break108 if mod_file_name in district_files_overwritten:109 for district_name, district in mod_file.items():110 mod_districts[district_name] = district[0]111 districts_overwritten.append(district_name)112 for mod_file_path in glob.glob(mod_folder + '/events/*'):113 mod_file = parse_file(mod_file_path)114 for events in mod_file.values():115 for event in events:116 if type(event) is not str:117 for effect in event.get_list('immediate'):118 if effect.has_single('IF'):119 effect = effect.get_single('IF')120 if 'set_global_flag' in effect and other_mod[2] is None:121 other_mod[2] = effect['set_global_flag'][0].replace('= ', '')122 for other_build_restriction in other_build_restrictions:123 for value in other_build_restriction.get_values():124 if effect['set_global_flag'][0] == value:125 other_mod[3] = True126 shutil.rmtree(mod_folder)127 # save certain added districts128 for district_name, district in mod_districts.items():129 for output_file in district_output_files:130 if district_name in output_file[1]:131 break132 else:133 for output_file in district_output_files:134 if output_file[0] == 'udp_extra_districts.txt':135 for title in ['show_on_uncolonized', 'potential']:136 use_mod_flag(other_mod)137 district.get_single(title).ensure({'has_global_flag': ['= ' + other_mod[2]]})138 output_file[1].ensure({district_name: [district]})139 break140 # disable removed districts141 for output_file in district_output_files:142 if output_file[0] in mod_file_names:143 for district_name, district in output_file[1].items():144 if district_name not in mod_districts:145 for title in ['show_on_uncolonized', 'potential']:146 use_mod_flag(other_mod)147 district[0].get_single(title).ensure({'NOT': [{'has_global_flag': ['= ' + other_mod[2]]}]})148 # merge ai behaviours for ai mods149 if i < ai_mod_count:150 for output_file in district_output_files:151 for district_name, district in output_file[1].items():152 if district_name in mod_districts:153 mod_district = mod_districts[district_name]154 use_mod_flag(other_mod)155 current_ai = district[0].get_single('ai_weight')156 new_ai = mod_district.get_single('ai_weight')157 combined_ai = StellarisDict({'weight': ['= 0'], 'modifier': []})158 if current_ai.get_single('weight') != '= 0':159 combined_ai.ensure({'modifier': [{160 'weight': current_ai['weight'],161 'NOT': [{'has_global_flag': ['= ' + other_mod[2]]}],162 }]})163 if new_ai.get_single('weight') != '= 0':164 combined_ai.ensure({'modifier': [{165 'weight': new_ai['weight'],166 'has_global_flag': ['= ' + other_mod[2]],167 }]})168 for modifier in current_ai.get_list('modifier'):169 mod_flag_set = False170 for item in modifier.get_list('has_global_flag'):171 for mod in mod_collection:172 if item.replace('= ', '') == mod[2]:173 mod_flag_set = True174 if not mod_flag_set:175 modifier.ensure({'NOT': [{'has_global_flag': ['= ' + other_mod[2]]}]})176 combined_ai.ensure({'modifier': [modifier]})177 for modifier in new_ai.get_list('modifier'):178 modifier.ensure({'has_global_flag': ['= ' + other_mod[2]]})179 combined_ai.ensure({'modifier': [modifier]})180 district[0]['ai_weight'] = [combined_ai]181 # merge build restrictions182 for output_file in district_output_files:183 for district_name, district in output_file[1].items():184 if district_name in mod_districts:185 mod_district = mod_districts[district_name]186 for title in ['show_on_uncolonized', 'potential', 'allow']:187 for key, values in mod_district.get_single(title).items():188 for value in values:189 if key == 'has_deposit' or (isinstance(value, StellarisDict) and 'has_deposit' in value.get_keys()) \190 or (key == 'has_planet_flag' and output_file[0] == '04_ringworld_districts.txt'):191 use_mod_flag(other_mod)192 value = {'NOT': [{'has_global_flag': ['= ' + other_mod[2]]}], key: [value]}193 key = 'OR'194 merged = False195 if key == 'OR' and set(value.keys()) == {'is_planet_class'}:196 if 'uncapped' in district_name:197 value['is_planet_class'] = [x for x in value['is_planet_class'] if x != '= pc_ringworld_habitable']198 for block in district[0].get_single(title).get_list(key):199 if set(block.keys()) == {'is_planet_class'}:200 block['is_planet_class'] = block['is_planet_class'] + \201 [x for x in value['is_planet_class'] if x not in block['is_planet_class']]202 merged = True203 if not merged and key != 'does_spawn_housing_districts' and value not in district[0].get_single(title).get_list(key):204 district[0].get_single(title).ensure({key: [value]})205 # merge triggered modifiers and descriptions206 for output_file in district_output_files:207 for district_name, district in output_file[1].items():208 if district_name in mod_districts:209 mod_district = mod_districts[district_name]210 for key in ['triggered_planet_modifier', 'triggered_desc', 'ai_resource_production']:211 trigger_key = 'potential' if key == 'triggered_planet_modifier' else 'trigger'212 for value in mod_district.get_list(key):213 if value not in [item.copy_without('vanilla') for item in district[0].get_list(key)]:214 must_add_flag = trigger_key not in value or \215 value.get_single(trigger_key) in [item.get_single(trigger_key) for item in district[0].get_list(key)]216 if not must_add_flag and \217 set(value.get_single(trigger_key).keys()) == {'exists', 'owner'} and \218 value.get_single(trigger_key).get_single('exists') == '= owner' and \219 len(value.get_single(trigger_key).get_list('owner')) == 1 and \220 set(value.get_single(trigger_key).get_single('owner').keys()).issubset({221 'is_regular_empire',222 'is_gestalt',223 'is_machine_empire',224 'is_hive_empire',225 'is_fallen_empire',226 'is_fallen_empire_spiritualist',227 'is_mechanical_empire',228 }):229 must_add_flag = True230 if must_add_flag:231 if trigger_key not in value:232 value.ensure({trigger_key: [{}]})233 use_mod_flag(other_mod)234 value.get_single(trigger_key).ensure({'has_global_flag': ['= ' + other_mod[2]]})235 district[0].ensure({key: [value]})236 for old_modifier in district[0].get_list(key):237 if 'vanilla' in old_modifier:238 for new_modifier in mod_district.get_list(key):239 if old_modifier.copy_without(trigger_key).copy_without('vanilla') == new_modifier.copy_without(trigger_key) and \240 new_modifier.get_single(trigger_key).get_single('owner').get_single('AND').has_single('NOT') and \241 old_modifier.get_single(trigger_key).get_single('owner') == \242 new_modifier.get_single(trigger_key).get_single('owner').get_single('AND').copy_without('NOT'):243 old_modifier.get_single('vanilla').ensure({'delete': []})244 break245 else:246 if old_modifier.copy_without('vanilla') not in mod_district.get_list(key):247 use_mod_flag(other_mod)248 old_modifier.get_single('vanilla').get_single('NOR').ensure({'has_global_flag': ['= ' + other_mod[2]]})249 # merge normal modifiers250 for output_file in district_output_files:251 for district_name, district in output_file[1].items():252 if district_name in mod_districts:253 mod_district = mod_districts[district_name]254 merge_modifier = True255 if 'planet_modifier' in district[0]:256 if (mod_district.get_single('planet_modifier') == district[0].get_single('planet_modifier')) or \257 (district_name == 'district_nexus' and mod_district.get_single('planet_modifier') == {'planet_housing_add': ['= 5']}):258 merge_modifier = False259 else:260 district[0].ensure({'triggered_planet_modifier': [{261 'default': [{'NOR': [{}]}],262 'modifier': [district[0].get_single('planet_modifier')],263 }]})264 del district[0]['planet_modifier']265 else:266 for modifier in district[0].get_list('triggered_planet_modifier'):267 if 'default' in modifier:268 if (mod_district.get_single('planet_modifier') == modifier.get_single('modifier')) or \269 (district_name == 'district_nexus' and mod_district.get_single('planet_modifier') == {'planet_housing_add': ['= 5']}):270 merge_modifier = False271 break272 else:273 merge_modifier = False274 if merge_modifier:275 if 'planet_modifier' in mod_district:276 use_mod_flag(other_mod)277 district[0].ensure({'triggered_planet_modifier': [{278 'potential': [{'has_global_flag': ['= ' + other_mod[2]]}],279 'modifier': [mod_district.get_single('planet_modifier')],280 }]})281 for modifier in district[0].get_list('triggered_planet_modifier'):282 if 'default' in modifier:283 use_mod_flag(other_mod)284 modifier.get_single('default').get_single('NOR').ensure({'has_global_flag': ['= ' + other_mod[2]]})285 break286 # merge resources287 for output_file in district_output_files:288 for district_name, district in output_file[1].items():289 if district_name in mod_districts:290 mod_district = mod_districts[district_name]291 for title in ['cost', 'upkeep', 'produces']:292 for value in mod_district.get_single('resources').get_list(title):293 if value not in district[0].get_single('resources').get_list(title):294 if 'trigger' not in value:295 use_mod_flag(other_mod)296 value.ensure({'trigger': [{'has_global_flag': ['= ' + other_mod[2]]}]})297 elif value.get_single('trigger').get_single('owner').has_single('NOT'):298 district[0].get_single('resources').get_list(title).remove(value.copy_without('trigger'))299 district[0].get_single('resources').ensure({title: [value]})300 # merge district conversions301 for output_file in district_output_files:302 for district_name, district in output_file[1].items():303 if district_name in mod_districts:304 mod_district = mod_districts[district_name]305 for value in mod_district.get_single('convert_to'):306 if value != district_name and value not in district[0].get_single('convert_to'):307 district[0].get_single('convert_to').ensure({value: []})308 ################################309 # fix OR in build restrictions #310 ################################311 for output_file in district_output_files:312 for district_name, district in output_file[1].items():313 for title in ['show_on_uncolonized', 'potential', 'allow']:314 for block in district[0].get_single(title).get_list('OR'):315 for key, values in block.items():316 for value in values:317 if value in district[0].get_single(title).get_list(key):318 district[0].get_single(title).get_list(key).remove(value)319 if len(district[0].get_single(title).get_list(key)) == 0:320 del district[0].get_single(title)[key]321 ###########################322 # convert temporary flags #323 ###########################324 for output_file in district_output_files:325 for district_name, district in output_file[1].items():326 for key in ['triggered_planet_modifier', 'triggered_desc', 'ai_resource_production']:327 trigger_key = 'potential' if key == 'triggered_planet_modifier' else 'trigger'328 i = 0329 while i < len(district[0].get_list(key)):330 modifier = district[0].get_list(key)[i]331 for flag in ['default', 'vanilla']:332 if flag in modifier:333 if 'delete' in modifier.get_single(flag):334 district[0].get_list(key).remove(modifier)335 i -= 1336 break337 if len(modifier.get_single(flag).get_single('NOR').keys()) > 0:338 if trigger_key not in modifier:339 modifier.ensure({trigger_key: [{}]})340 modifier.get_single(trigger_key).ensure(modifier.get_single(flag))341 del modifier[flag]342 i += 1343 #############################344 # uncap district generation #345 #############################346 for output_file in district_output_files:347 for district_name in output_file[1]:348 if 'min_for_deposits_on_planet' in output_file[1].get_single(district_name):349 output_file[1][district_name][0]['min_for_deposits_on_planet'] = ['= 0']350 if 'max_for_deposits_on_planet' in output_file[1].get_single(district_name):351 output_file[1][district_name][0]['max_for_deposits_on_planet'] = ['= 999']352 #########################353 # save all output files #354 #########################355 shutil.rmtree('../Mod', True)356 os.makedirs('../Mod/!!!!Universal Districts Patch/common/districts')357 for output_file in district_output_files:358 write_data(output_file[1], '../Mod/!!!!Universal Districts Patch/common/districts/' + output_file[0])359 district_files_overwritten.remove(output_file[0])360 for file_overwritten in district_files_overwritten:361 open('../Mod/!!!!Universal Districts Patch/common/districts/' + file_overwritten, 'w').close()362 shutil.rmtree(mods_folder + '/!!!!Universal Districts Patch', True)363 shutil.rmtree(mods_folder + '/!!!!Universal Districts Patch.mod', True)364 for folder in ['../Mod', '../Skeleton']:...
socrata.py
Source:socrata.py
...18 db='metadata', # æ°æ®åºå19 )20 self.cur = self.connection.cursor()21 @staticmethod22 def get_single(dic, key):23 return dic.get(key, None)24 @staticmethod25 def get_array(dic, key):26 l1 = dic.get(key, [])27 s1 = ';'.join([str(x) for x in l1])28 if len(s1): # s1!=''29 return s130 else:31 return None32 @staticmethod33 def get_json_dic(params):34 headers = {35 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}36 proxies = {"http": None, "https": None}37 try:38 r = requests.get(SOCRATA_API_URL, timeout=1000, verify=False, params=params, headers=headers)39 # print(r.url)40 except Exception as e:41 print(e)42 return None43 else:44 return r.json()45 @staticmethod46 def get_results(json_dic):47 return json_dic.get('results', None)48 def save_metadata(self, json_dic):49 results = self.get_results(json_dic)50 if results is None or len(results) == 0:51 print('results is None or len(results)==0')52 return True53 sql = 'INSERT IGNORE INTO `{}`(`name`, `id`, `parent_fxf`, `description`, `attribution`, `attribution_link`, ' \54 '`contact_email`, `type`, `updatedAt`, `createdAt`, `metadata_updated_at`, `data_updated_at`, ' \55 '`page_views_last_week`, `page_views_last_month`, `page_views_total`, `page_views_last_week_log`, ' \56 '`page_views_last_month_log`, `page_views_total_log`, `columns_name`, `columns_field_name`, ' \57 '`columns_datatype`, `columns_description`, `columns_format`, `download_count`, `provenance`, ' \58 '`lens_view_type`, `blob_mime_type`, `hide_from_data_json`, `publication_date`, `categories`, `tags`, ' \59 '`domain_tags`, `domain_metadata`, `domain`, `license`, `permalink`, `link`, `owner_id`, `user_type`, ' \60 '`display_name`, `data_source`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,' \61 ' %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);' \62 .format(self.domain)63 values = []64 for result in results:65 resource = self.get_resource(result)66 classification = self.get_classification(result)67 metadata = self.get_metadata(result)68 permalink = self.get_single(result, 'permalink')69 link = self.get_single(result, 'link')70 owner = self.get_owner(result)71 value = resource + classification + metadata + [permalink] + [link] + owner + [self.url]72 values.append(tuple(value))73 try:74 # æ§è¡sqlè¯å¥75 rows = self.cur.executemany(sql, values)76 # æ交å°æ°æ®åºæ§è¡77 self.connection.commit()78 print('insert {} rows'.format(rows))79 return True80 except Exception as e:81 # å¦æåçé误ååæ»82 print(e)83 self.connection.rollback()84 return False85 def get_resource(self, result_dic):86 dic = result_dic.get('resource', {})87 ret = []88 ret.append(self.get_single(dic, 'name'))89 ret.append(self.get_single(dic, 'id'))90 ret.append(self.get_array(dic, 'parent_fxf'))91 ret.append(self.get_single(dic, 'description'))92 ret.append(self.get_single(dic, 'attribution'))93 ret.append(self.get_single(dic, 'attribution_link'))94 ret.append(self.get_single(dic, 'contact_email'))95 ret.append(self.get_single(dic, 'type'))96 ret.append(self.get_single(dic, 'updatedAt'))97 ret.append(self.get_single(dic, 'createdAt'))98 ret.append(self.get_single(dic, 'metadata_updated_at'))99 ret.append(self.get_single(dic, 'data_updated_at'))100 page_views_dic = dic.get('page_views', {})101 ret.append(self.get_single(page_views_dic, 'page_views_last_week'))102 ret.append(self.get_single(page_views_dic, 'page_views_last_month'))103 ret.append(self.get_single(page_views_dic, 'page_views_total'))104 ret.append(self.get_single(page_views_dic, 'page_views_last_week_log'))105 ret.append(self.get_single(page_views_dic, 'page_views_last_month_log'))106 ret.append(self.get_single(page_views_dic, 'page_views_total_log'))107 ret.append(self.get_array(dic, 'columns_name'))108 ret.append(self.get_array(dic, 'columns_field_name'))109 ret.append(self.get_array(dic, 'columns_datatype'))110 ret.append(self.get_array(dic, 'columns_description'))111 ret.append(self.get_array(dic, 'columns_format'))112 ret.append(self.get_single(dic, 'download_count'))113 ret.append(self.get_single(dic, 'provenance'))114 ret.append(self.get_single(dic, 'lens_view_type'))115 ret.append(self.get_single(dic, 'blob_mime_type'))116 ret.append(self.get_single(dic, 'hide_from_data_json'))117 ret.append(self.get_single(dic, 'publication_date'))118 return ret119 def get_classification(self, result_dic):120 dic = result_dic.get('classification', {})121 ret = []122 ret.append(self.get_array(dic, 'categories'))123 ret.append(self.get_array(dic, 'tags'))124 ret.append(self.get_array(dic, 'domain_tags'))125 ret.append(self.get_array(dic, 'domain_metadata'))126 return ret127 def get_metadata(self, result_dic):128 dic = result_dic.get('metadata', {})129 ret = []130 ret.append(self.get_single(dic, 'domain'))131 ret.append(self.get_single(dic, 'license'))132 return ret133 def get_owner(self, result_dic):134 dic = result_dic.get('owner', {})135 ret = []136 ret.append(self.get_single(dic, 'id'))137 ret.append(self.get_single(dic, 'user_type'))138 ret.append(self.get_single(dic, 'display_name'))139 return ret140 def get_scroll_id(self, json_dic):141 results = self.get_results(json_dic)142 if len(results) == 0:143 return 0144 last_result = results[-1]145 last_resource = last_result.get('resource', {})146 return last_resource.get('id', None)147 def create_table(self):148 sql = 'CREATE TABLE IF NOT EXISTS `{}`'.format(self.domain)149 with open(TABLE_SQL_PATH, 'r', encoding='utf-8') as f:150 sql = sql + f.read()151 try:152 self.cur.execute(sql)...
test_rolling.py
Source:test_rolling.py
...48 return gen_data(*request.param)49@pytest.fixture(scope="module", params=weighted_params, ids=weighted_ids)50def weighted_data(request):51 return gen_data(*request.param)52def get_single(x, idx):53 if isinstance(x, (pd.Series, pd.DataFrame)):54 return x.iloc[idx]55 return x[idx]56def get_sub(x, idx, window):57 if isinstance(x, (pd.Series, pd.DataFrame)):58 out = x.iloc[idx - window : idx]59 return np.asarray(out)60 return x[idx - window : idx]61def test_has_nan(data):62 y, x, w = data63 mod = RollingWLS(y, x, window=100, weights=w)64 has_nan = np.zeros(y.shape[0], dtype=bool)65 for i in range(100, y.shape[0] + 1):66 _y = get_sub(y, i, 100)67 _x = get_sub(x, i, 100)68 has_nan[i - 1] = np.squeeze(69 (np.any(np.isnan(_y)) or np.any(np.isnan(_x)))70 )71 assert_array_equal(mod._has_nan, has_nan)72def test_weighted_against_wls(weighted_data):73 y, x, w = weighted_data74 mod = RollingWLS(y, x, weights=w, window=100)75 res = mod.fit(use_t=True)76 for i in range(100, y.shape[0]):77 _y = get_sub(y, i, 100)78 _x = get_sub(x, i, 100)79 if w is not None:80 _w = get_sub(w, i, 100)81 else:82 _w = np.ones_like(_y)83 wls = WLS(_y, _x, weights=_w, missing="drop").fit()84 rolling_params = get_single(res.params, i - 1)85 rolling_nobs = get_single(res.nobs, i - 1)86 assert_allclose(rolling_params, wls.params)87 assert_allclose(rolling_nobs, wls.nobs)88 assert_allclose(get_single(res.ssr, i - 1), wls.ssr)89 assert_allclose(get_single(res.llf, i - 1), wls.llf)90 assert_allclose(get_single(res.aic, i - 1), wls.aic)91 assert_allclose(get_single(res.bic, i - 1), wls.bic)92 assert_allclose(get_single(res.centered_tss, i - 1), wls.centered_tss)93 assert_allclose(res.df_model, wls.df_model)94 assert_allclose(get_single(res.df_resid, i - 1), wls.df_resid)95 assert_allclose(get_single(res.ess, i - 1), wls.ess, atol=1e-8)96 assert_allclose(res.k_constant, wls.k_constant)97 assert_allclose(get_single(res.mse_model, i - 1), wls.mse_model)98 assert_allclose(get_single(res.mse_resid, i - 1), wls.mse_resid)99 assert_allclose(get_single(res.mse_total, i - 1), wls.mse_total)100 assert_allclose(101 get_single(res.rsquared, i - 1), wls.rsquared, atol=1e-8102 )103 assert_allclose(104 get_single(res.rsquared_adj, i - 1), wls.rsquared_adj, atol=1e-8105 )106 assert_allclose(107 get_single(res.uncentered_tss, i - 1), wls.uncentered_tss108 )109@pytest.mark.parametrize("cov_type", ["nonrobust", "HC0"])110@pytest.mark.parametrize("use_t", [None, True, False])111def test_against_wls_inference(data, use_t, cov_type):112 y, x, w = data113 mod = RollingWLS(y, x, window=100, weights=w)114 res = mod.fit(use_t=use_t, cov_type=cov_type)115 ci_cols = ci = res.conf_int()116 test_cols = x.shape[1] > 3117 # This is a smoke test of cov_params to make sure it works118 res.cov_params()119 if test_cols:120 ci_cols = res.conf_int(cols=[0, 2])121 # Skip to improve performance122 for i in range(100, y.shape[0]):123 _y = get_sub(y, i, 100)124 _x = get_sub(x, i, 100)125 wls = WLS(_y, _x, missing="drop").fit(use_t=use_t, cov_type=cov_type)126 assert_allclose(get_single(res.tvalues, i - 1), wls.tvalues)127 assert_allclose(get_single(res.bse, i - 1), wls.bse)128 assert_allclose(get_single(res.pvalues, i - 1), wls.pvalues, atol=1e-8)129 assert_allclose(get_single(res.fvalue, i - 1), wls.fvalue)130 with np.errstate(invalid="ignore"):131 assert_allclose(132 get_single(res.f_pvalue, i - 1), wls.f_pvalue, atol=1e-8133 )134 assert res.cov_type == wls.cov_type135 assert res.use_t == wls.use_t136 wls_ci = wls.conf_int()137 if isinstance(ci, pd.DataFrame):138 ci_val = ci.iloc[i - 1]139 ci_val = np.asarray(ci_val).reshape((-1, 2))140 else:141 ci_val = ci[i - 1].T142 assert_allclose(ci_val, wls_ci)143 if test_cols:144 wls_ci = wls.conf_int(cols=[0, 2])145 if isinstance(ci_cols, pd.DataFrame):146 ci_val = ci_cols.iloc[i - 1]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!