Best Python code snippet using localstack_python
tf_models.py
Source:tf_models.py
...17 """18 return datetime.strftime(datetime.now(), '-%Y%m%d-%H%M')19class Terraform:20 @staticmethod21 def set_provider(region):22 """23 :param region: str24 """25 return 'aws.' + region26 @staticmethod27 def set_id(resource, name):28 """29 :param resource: str30 :param name: str31 """32 return '${' + resource + '.' + name + '.id}'33 @classmethod34 def aws_instance(35 cls,36 name=None,37 region=None,38 ami=None,39 instance_type='m3.medium',40 subnet_id=None,41 key_name=None, # the keypair name42 tags=None,43 vpc_security_group_ids=None,44 associate_public_ip_address=None,45 source_dest_check=None):46 """47 :param name: str48 :param region: str49 :param ami: str50 :param instance_type: str51 :param subnet_id: str52 :param key_name: str53 :param tags: dict54 :param vpc_security_group_ids: list55 :param associate_public_ip_address: bool56 :param source_dest_check: bool57 :return: Terraform58 """59 tags.update({'Name': name}) if tags else tags60 resource = 'aws_instance'61 id = Terraform.set_id(resource, name)62 provider = Terraform.set_provider(region)63 return cls(id, name, provider, resource, remove_none({64 name: dict(provider=provider, ami=ami, subnet_id=subnet_id,65 key_name=key_name, instance_type=instance_type,66 vpc_security_group_ids=vpc_security_group_ids, tags=tags,67 associate_public_ip_address=associate_public_ip_address,68 source_dest_check=source_dest_check)}))69 @classmethod70 def aws_ami_from_instance(71 cls,72 name,73 region,74 source_instance_id,75 tags=None):76 """77 :param name: str78 :param region: str79 :param source_instance_id: str80 :param tags: dict81 :return: Terraform82 """83 name += time_now()84 tags.update({'Name': name}) if tags else None85 resource = 'aws_ami_from_instance'86 id = Terraform.set_id(resource, name)87 provider = Terraform.set_provider(region)88 return cls(id, name, provider, resource, remove_none({89 name: dict(provider=provider, name=name,90 source_instance_id=source_instance_id, tags=tags)}))91 @classmethod92 def aws_elb(93 cls,94 name,95 region,96 listeners,97 subnets,98 health_check=None,99 internal=None,100 security_groups=None,101 instances=None):102 """103 :param name: str104 :param region: str105 :param listeners: list106 :param subnets: list107 :param health_check: dict108 :param internal: bool109 :param security_groups: list110 :param instances: list111 :return: Terraform112 """113 resource = 'aws_elb'114 id = Terraform.set_id(resource, name)115 provider = Terraform.set_provider(region)116 return cls(id, name, provider, resource, remove_none({117 name: dict(provider=provider, name=name, listener=listeners,118 subnets=subnets, health_check=health_check,119 internal=internal, security_groups=security_groups,120 instances=instances)}))121 @classmethod122 def aws_elasticache_cluster(123 cls,124 name,125 region,126 node_type,127 num_cache_nodes,128 parameter_group_name,129 port,130 engine,131 subnet_group_name=None,132 security_group_ids=None):133 """134 :param name: str135 :param region: str136 :param node_type: str137 :param num_cache_nodes: str138 :param parameter_group_name: str139 :param port: int140 :param engine: str141 :param subnet_group_name: str142 :param security_group_ids: list143 :return: Terraform144 """145 resource = 'aws_elasticache_cluster'146 id = Terraform.set_id(resource, name)147 provider = Terraform.set_provider(region)148 return cls(id, name, provider, resource, remove_none({149 name: dict(provider=provider, cluster_id=name, node_type=node_type,150 num_cache_nodes=num_cache_nodes,151 parameter_group_name=parameter_group_name, port=port,152 subnet_group_name=subnet_group_name,153 security_group_ids=security_group_ids, engine=engine)}))154 @classmethod155 def aws_rds(156 cls,157 name,158 region,159 instance_class,160 snapshot_id,161 allocated_storage=None,162 engine=None,163 engine_version=None,164 db_subnet_group_name=None,165 vpc_security_group_ids=None,166 parameter_group_name=None,167 publicly_accessible=False,168 username=None,169 password=''):170 """171 :param name: str172 :param region: str173 :param instance_class: str174 :param snapshot_id: str175 :param allocated_storage: str176 :param engine: str177 :param engine_version: str178 :param db_subnet_group_name: str179 :param vpc_security_group_ids: list180 :param parameter_group_name: str181 :param publicly_accessible: bool182 :param username:183 :param password:184 :return: Terraform185 """186 resource = 'aws_db_instance'187 id = Terraform.set_id(resource, name)188 provider = Terraform.set_provider(region)189 return cls(id, name, provider, resource, remove_none({190 name: dict(provider=provider, identifier=name,191 snapshot_identifier=snapshot_id,192 allocated_storage=allocated_storage, engine=engine,193 engine_version=engine_version,194 instance_class=instance_class,195 db_subnet_group_name=db_subnet_group_name,196 vpc_security_group_ids=vpc_security_group_ids,197 parameter_group_name=parameter_group_name,198 publicly_accessible=publicly_accessible,199 username=username, password=password)}))200 @classmethod201 def aws_security_group(202 cls,203 name,204 region,205 name_prefix=None,206 description=None,207 ingress=None,208 egress=None,209 vpc_id=None,210 tags=None):211 """212 :param name:213 :param region:214 :param name_prefix:215 :param description:216 :param ingress:217 :param egress:218 :param vpc_id:219 :param tags:220 :return:221 """222 resource = 'aws_security_group'223 id = Terraform.set_id(resource, name)224 provider = Terraform.set_provider(region)225 return cls(id, name, provider, resource, remove_none({226 name: dict(provider=provider, name=name, name_prefix=name_prefix,227 description=description, ingress=ingress, egress=egress,228 tags=tags, vpc_id=vpc_id)}))229 # *** VPC (Networking) ***230 @classmethod231 def aws_vpc(232 cls,233 name,234 region,235 cidr_block,236 instance_tenancy=None,237 enable_dns_support=None,238 enable_dns_hostnames=None,239 enable_classiclink=None,240 tags=None):241 """242 :param name: str243 :param region: str244 :param cidr_block: str245 :param instance_tenancy: str246 :param enable_dns_support: str247 :param enable_dns_hostnames: str248 :param enable_classiclink: str249 :param tags: dict250 :return: Terraform251 """252 resource = 'aws_vpc'253 id = Terraform.set_id(resource, name)254 provider = Terraform.set_provider(region)255 return cls(id, name, provider, resource, remove_none({256 name: dict(provider=provider, cidr_block=cidr_block,257 instance_tenancy=instance_tenancy,258 enable_dns_support=enable_dns_support,259 enable_dns_hostnames=enable_dns_hostnames,260 enable_classiclink=enable_classiclink, tags=tags)}))261 @classmethod262 def aws_internet_gateway(263 cls,264 name,265 region,266 vpc_id,267 tags=None):268 """269 :param name: str270 :param region: str271 :param vpc_id: str272 :param tags: dict273 :return: Terraform274 """275 resource = 'aws_internet_gateway'276 id = Terraform.set_id(resource, name)277 provider = Terraform.set_provider(region)278 return cls(id, name, provider, resource, remove_none({279 name: dict(provider=provider, vpc_id=vpc_id, tags=tags)}))280 @classmethod281 def aws_subnet(282 cls,283 name,284 region,285 vpc_id,286 cidr_block,287 map_public_ip_on_launch=None,288 availability_zone=None,289 tags=None):290 """291 :param name: str292 :param region: str293 :param vpc_id: str294 :param cidr_block: str295 :param map_public_ip_on_launch: bool296 :param availability_zone: str297 :param tags: dict298 :return: Terraform299 """300 resource = 'aws_subnet'301 id = Terraform.set_id(resource, name)302 provider = Terraform.set_provider(region)303 return cls(id, name, provider, resource, remove_none({304 name: dict(provider=provider, vpc_id=vpc_id, cidr_block=cidr_block,305 map_public_ip_on_launch=map_public_ip_on_launch,306 availability_zone=availability_zone, tags=tags)}))307 @classmethod308 def aws_eip(309 cls,310 name,311 region,312 vpc=None,313 instance=None,314 network_interface=None,315 associate_with_private_ip=None):316 """317 :param name: str318 :param region: str319 :param vpc: str320 :param instance: str321 :param network_interface: str322 :param associate_with_private_ip: bool323 :return: Terraform324 """325 resource = 'aws_eip'326 id = Terraform.set_id(resource, name)327 provider = Terraform.set_provider(region)328 return cls(id, name, provider, resource, remove_none({329 name: dict(provider=provider, vpc=vpc, instance=instance,330 network_interface=network_interface,331 associate_with_private_ip=associate_with_private_ip)}))332 @classmethod333 def aws_nat_gateway(334 cls,335 name,336 region,337 allocation_id,338 subnet_id):339 """340 :param name: str341 :param region: str342 :param allocation_id: str343 :param subnet_id: str344 :return: Terraform345 """346 resource = 'aws_nat_gateway'347 id = Terraform.set_id(resource, name)348 provider = Terraform.set_provider(region)349 return cls(id, name, provider, resource, remove_none({350 name: dict(provider=provider, allocation_id=allocation_id,351 subnet_id=subnet_id)}))352 @classmethod353 def aws_route_table(354 cls,355 name,356 region,357 vpc_id,358 route=None,359 propagating_vgws=None,360 tags=None):361 """362 :param name: str363 :param region: str364 :param vpc_id: str365 :param route: dict366 :param propagating_vgws: bool367 :param tags: dict368 :return:369 """370 resource = 'aws_route_table'371 id = Terraform.set_id(resource, name)372 provider = Terraform.set_provider(region)373 return cls(id, name, provider, resource, remove_none({374 name: dict(provider=provider, vpc_id=vpc_id, route=route,375 propagating_vgws=propagating_vgws, tags=tags)}))376 @classmethod377 def aws_route(378 cls,379 name,380 region,381 route_table_id,382 destination_cidr_block,383 vpc_peering_connection_id=None,384 gateway_id=None,385 nat_gateway_id=None,386 instance_id=None,387 network_interface_id=None):388 """389 :param name: str390 :param region: str391 :param route_table_id: str392 :param destination_cidr_block: str393 :param vpc_peering_connection_id: str394 :param gateway_id: str395 :param nat_gateway_id: str396 :param instance_id: str397 :param network_interface_id: str398 :return: Terraform399 """400 resource = 'aws_route'401 id = Terraform.set_id(resource, name)402 provider = Terraform.set_provider(region)403 return cls(id, name, provider, resource, remove_none({404 name: dict(provider=provider, route_table_id=route_table_id,405 destination_cidr_block=destination_cidr_block,406 vpc_peering_connection_id=vpc_peering_connection_id,407 gateway_id=gateway_id, nat_gateway_id=nat_gateway_id,408 instance_id=instance_id,409 network_interface_id=network_interface_id)}))410 @classmethod411 def aws_route_table_association(412 cls,413 name,414 region,415 subnet_id,416 route_table_id):417 """418 :param name: str419 :param region: str420 :param subnet_id: str421 :param route_table_id: str422 :return: Terraform423 """424 resource = 'aws_route_table_association'425 id = Terraform.set_id(resource, name)426 provider = Terraform.set_provider(region)427 return cls(id, name, provider, resource, remove_none({428 name: dict(provider=provider, subnet_id=subnet_id,429 route_table_id=route_table_id)}))430 @classmethod431 def aws_main_route_table_association(432 cls,433 name,434 region,435 vpc_id,436 route_table_id):437 """438 :param name: str439 :param region: str440 :param vpc_id: str441 :param route_table_id: str442 :return: Terraform443 """444 resource = 'aws_main_route_table_association'445 id = Terraform.set_id(resource, name)446 provider = Terraform.set_provider(region)447 return cls(id, name, provider, resource, remove_none({448 name: dict(provider=provider, vpc_id=vpc_id,449 route_table_id=route_table_id)}))450 def __init__(self, id, name, provider, resource, terraform):451 """452 :param id: str453 :param name: str454 :param provider: str455 :param resource: str456 :param terraform: dict457 """458 self.id = id459 self.terraform = terraform460 self.name = name...
FLEX: A Platform for Scalable Service Placement in Multi-Fog and Multi-Cloud Environments
Source:FLEX: A Platform for Scalable Service Placement in Multi-Fog and Multi-Cloud Environments
1#!/usr/bin/env python2# coding: utf-83#FLEX: A Platform for Scalable Service Placement in Multi-Fog and Multi-Cloud Environments4# # MCD1 Algorithm5# In[1]:6import random7import numpy as np8# # Input:9# In[19]:10number_provider=1011number_service=3012alpha=random.uniform(0,1)13service=[]14for i in range(number_service):15 service.append(i)16# # Process17# In[16]:18def select_Provider(s,number_provider):19 alpha=random.uniform(0,1)20 #P:List of Nodes21 empty_provider = {22 #id: number of provider23 'id': None, 24 #C:Cost vector for Si25 'C': None, 26 #D:delay27 'D': None,28 #F 29 'F': None,30 } 31 provider_List=[]32 for i in range(number_provider):33 provider_List.append(empty_provider.copy())34 provider_List[i]['id'] = i35 #Cij cost of provider Pj for Si36 provider_List[i]['C'] = random.randint(10, 20)37 #dij delay of provider Pj for Si38 provider_List[i]['D'] =random.randint(10, 500)39 C=[]40 for i in range(number_provider):41 C.append(provider_List[i]['C'])42 Cmax=np.max(C) 43 D=[]44 for i in range(number_provider):45 D.append(provider_List[i]['D'])46 Dmax=np.max(D) 47 for i in range(number_provider):48 provider_List[i]['id'] = i49 #Cij norm50 provider_List[i]['C'] = provider_List[i]['C']/Cmax51 #dij norm52 provider_List[i]['D'] =provider_List[i]['D']/Dmax53 #Fij54 provider_List[i]['F'] =alpha*provider_List[i]['C']+(1-alpha)*provider_List[i]['D']55 print(provider_List)56 F=[]57 for i in range(number_provider):58 F.append(provider_List[i]['F'])59 Fmin=np.min(F)60 idex=np.argmin(F)61 print("Let P%d is the provider with the value of Fmin=%f" %(idex,Fmin))62 return idex,Fmin63# In[29]:64set_provider=[]65set_Fmin=[]66for i in range(number_service):67 idex,Fmin=select_Provider(1,number_provider)68 set_provider.append(idex)69 set_Fmin.append(Fmin)70# # Output71# In[31]:72import matplotlib.pyplot as plt73plt.plot(service,set_provider,color='k', linestyle='dashed', marker='o',74 markerfacecolor='blue', markersize=12)75plt.xlabel('service')76plt.ylabel('set provider')77plt.title('MCD1 Algorithm')78plt.show()79# In[30]:80import matplotlib.pyplot as plt81plt.plot(service,set_Fmin,color='k', linestyle='dashed', marker='o',82 markerfacecolor='red', markersize=12)83plt.xlabel('service')84plt.ylabel('Fmin')85plt.title('MCD1 Algorithm')86plt.show()...
patches.py
Source:patches.py
1"""Patch library bad behavior."""2import libmorse3from kivy import utils as kivy_utils4from kivy.clock import Clock5from kivy.core import camera as core_camera, core_select_lib6from kivy.uix import camera as uix_camera7from morseus import settings8CAM = settings.CAMERA_PROVIDERS9LOG = settings.LOGGING10PLATFORM = kivy_utils.platform11def patch_camera(set_provider=CAM.EXPLICIT):12 """Choose a predefined camera provider for some platforms."""13 CoreCamera = core_camera.Camera14 set_provider = set_provider or CAM.IMPLICIT.get(PLATFORM)15 if CAM.ENABLE and set_provider:16 for provider in core_camera.providers:17 if set_provider == provider[0]:18 core_camera.providers = (provider,)19 _CoreCamera = core_select_lib("camera", core_camera.providers)20 CoreCamera = _CoreCamera or CoreCamera21 break22 class PatchedCoreCamera(CoreCamera):23 def start(self, *args, **kwargs):24 ret = super(PatchedCoreCamera, self).start(*args, **kwargs)25 attrs = map(lambda attr: getattr(self, attr, None),26 ["_update_ev", "fps"])27 if all(attrs) and self.fps > 1.0:28 self._update_ev.cancel()29 self._update_ev = Clock.schedule_interval(30 self._update, 1.0 / self.fps31 )32 return ret33 core_camera.Camera = PatchedCoreCamera34 uix_camera.CoreCamera = core_camera.Camera35def patch_libmorse(logfile=LOG.FILE.EXPLICIT):36 if logfile is None:37 logfile = LOG.FILE.IMPLICIT.get(PLATFORM)38 # Do not take into account `True` or `None`.39 if logfile is False:40 # We deliberately do not want to log into files.41 libmorse.settings.LOGFILE = None42def patch_all(camera=True, libmorse=True):43 if camera:44 patch_camera()45 if libmorse:...
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!!