<commit_before>import boto3
import logging

class Stopper:
  TAG_NAME = 'LambderStop'

  def __init__(self):
    self.ec2 = boto3.resource('ec2')
    logging.basicConfig()
    self.logger = logging.getLogger()

  def get_instances_to_stop(self):
    filters = [{'Name':'tag-key', 'Values': [self.TAG_NAME]}]
    instances = self.ec2.instances.filter(Filters=filters)
    return instances

  # Return the value of a specific tag on a resource
  # If the tag does not exist, return the value of 'default'
  def get_tag_value(self, resource, tag_name, default=None):
    tags = filter(lambda x: x['Key'] == tag_name, resource.tags)

    if not tags:
      return default

    return tags[0]['Value']

  def run(self):
    instances = self.get_instances_to_stop()

    for instance in instances:
      name     = self.get_tag_value(instance, 'Name', 'Unknown')
      name_str = name + " [{}]".format(instance.id)
      state    = instance.state['Name']

      if state == 'running':
        self.logger.info('Stopping ' + name_str)
        instance.stop()
      else:
        self.logger.info('Will not stop ' + name_str + ', state: ' + state)<commit_msg>added 1sec sleep to every stop to avoid API throttle<commit_after>import boto3
import logging
import time

class Stopper:
  TAG_NAME = 'LambderStop'

  def __init__(self):
    self.ec2 = boto3.resource('ec2')
    logging.basicConfig()
    self.logger = logging.getLogger()

  def get_instances_to_stop(self):
    filters = [{'Name':'tag-key', 'Values': [self.TAG_NAME]}]
    instances = self.ec2.instances.filter(Filters=filters)
    return instances

  # Return the value of a specific tag on a resource
  # If the tag does not exist, return the value of 'default'
  def get_tag_value(self, resource, tag_name, default=None):
    tags = filter(lambda x: x['Key'] == tag_name, resource.tags)

    if not tags:
      return default

    return tags[0]['Value']

  def run(self):
    instances = self.get_instances_to_stop()

    for instance in instances:
      name     = self.get_tag_value(instance, 'Name', 'Unknown')
      name_str = name + " [{}]".format(instance.id)
      state    = instance.state['Name']

      if state == 'running':
        self.logger.info('Stopping ' + name_str)
        instance.stop()
        time.sleep(5)
      else:
        self.logger.info('Will not stop ' + name_str + ', state: ' + state)
{'fraction_non_alphanumeric': 0.12673684210526315, 'fraction_numerical': 0.005894736842105263, 'mean_word_length': 3.197879858657244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5373701', 'n_tokens_mistral': 773, 'n_tokens_neox': 752, 'n_words': 199}