예제 #1
0
def process_command_output(queue):
    # TODO: execute the command and put its data in the db

    q = queue

    # Initialise list that will hold the database entry objects
    put_results_database = []
    while not q.empty():
        work = q.get(True)
        try:
            # execute is the command string to be executed
            execute = work
            # Timing the process of executing each command
            tic = time.clock()
            process = subprocess.run(execute,shell=True, timeout=60, stdout=subprocess.PIPE)
            toc = time.clock()
            # Storing the meta-data in each column of the defind database
            output = process.stdout
            duration = toc-tic
            length = len(execute)
            command_string = execute
        # Specifying the exception condition when the commands takes greater than 1 minute to process
        except subprocess.TimeoutExpired as e:
            print('long running or not finished scenario')
            duration = 0
            output = e.stdout

        # Appending the meta-data object in list
        result_entry = Command(command_string, length, duration, output)
        put_results_database.append(result_entry)

    # Putting results in database
    session.add_all(put_results_database)
    session.commit()
예제 #2
0
def put_to_db(data):
    """
    This functions takes the data and puts to the database
        @param data containing the dictionary
    """
    insertion_list = list()
    for value in data.values():
        insertion_list.append(Command(value[1], value[2], value[4], value[3]))
    session.add_all(insertion_list)
    session.commit()
예제 #3
0
def bootstrap_data():

    # Make some data
    u = User(first_name='Brian',
             last_name='Peterson',
             email='*****@*****.**')
    p = Promotion(
        flt_percent_off=.5,
        start_dt=datetime.utcnow(),
        end_dt=datetime.utcnow() + timedelta(days=5),
    )
    session.add_all([u, p])
    session.commit()
예제 #4
0
def bootstrap_data():

    # Make some data
    u = User(
        first_name='Brian',
        last_name='Peterson',
        email='*****@*****.**'
    )
    p = Promotion(
        flt_percent_off=.5,
        start_dt=datetime.utcnow(),
        end_dt=datetime.utcnow() + timedelta(days=5),
    )
    session.add_all([u, p])
    session.commit()
예제 #5
0
def bulk_db_inserter():
    obj = []
    url = 'https://www.prnewswire.com/search/news/?keyword=huobi&page=1&pagesize=100'
    soup = linker(url)
    urls = soup.find('div', class_="col-sm-12 card-list")
    rows = urls.findAll('div', class_="row")

    for r in rows:
        link = 'prnewswire.com' + r.find('a', class_='news-release')['href']
        title = r.find('a', class_='news-release').text
        date = r.find('small').text
        ins = PRTable(link=link, title=title, date=date)
        obj.append(ins)

    session.add_all(obj)
    session.commit()
예제 #6
0
from db import session, Base
from models.users import User

session.query(User).delete()

session.add_all([
    User(phoneNumber="071654564", name="Sala"),
    User(phoneNumber="0714697757", name="Dinuka")
])

session.commit()
예제 #7
0
def generate_test_user():
    generated_cpf = "".join([str(randint(0, 9)) for _ in range(11)])
    user = User.where(User.document == generated_cpf).first()

    while user is not None:
        generated_cpf = "".join([str(randint(0, 9)) for _ in range(11)])
        user = User.where(User.document == generated_cpf).first()

    user = User(
        document=generated_cpf,
        first_name="Test User",
        last_name=generated_cpf,
        email=f"test_{generated_cpf}@email.com",
        phone_number=f"55119{generated_cpf[0:8]}")

    generated_cep = "".join([str(randint(0, 9)) for _ in range(8)])
    address = Address.where(Address.postal_code == generated_cep).first()

    while address is not None:
        generated_cep = "".join([str(randint(0, 9)) for _ in range(8)])
        address = Address.where(Address.postal_code == generated_cep).first()

    address = Address(
        postal_code=generated_cep,
        country="BR",
        state="SP",
        city="São Paulo",
        neighbourhood=f"Bairro {generated_cep}",
        street=f"Rua {generated_cep}"
    )

    power_supply = PowerSupply(
        status="up",
        description=f"Power Supply {generated_cep}")

    occurrence = Occurrence(power_supply=power_supply,
                            category=("power_outage" if random()
                                      < 0.5 else "maintenance"),
                            description=f"Occurance 1",
                            status=("done" if power_supply.is_up(
                            ) else "in_progress" if random() < 0.5 else "cancelled"),
                            start_time=datetime.now()-timedelta(weeks=2),
                            end_time=(datetime.now()-timedelta(weeks=1)
                                      if power_supply.is_up() else None),
                            estimated_end_time=(datetime.now()-timedelta(hours=1) if random() < 0.5 else None))

    today = date.today()
    day = randint(1, 15)
    month = today.month
    year = today.year
    due_dates = []

    for _ in range(24):
        due_dates.append(datetime.strptime(f"{day}-{month}-{year}", "%d-%m-%Y").date())
        month = month-1 if month > 1 else 12
        year = year-1 if month == 12 else year

    bills = [Bill(user=user,
                  value=round(uniform(0.0, 500.0), 2),
                  paid=random() < 0.5,
                  due_date=due_date)
             for due_date in due_dates]

    power_supply.address = address
    user.address = address

    session.add(address)
    session.add(user)
    session.add(power_supply)
    session.add(occurrence)
    session.add_all(bills)

    session.commit()

    return {
        "cpf": generated_cpf,
        "cep": generated_cep,
        "oldest_bill": due_dates[-1],
        "most_recent_bill": due_dates[0]
    }
예제 #8
0
from db import session, Result, Entry_Requirement, Entry

# add possible results
session.add_all(
    [Result(result='win'),
     Result(result='lose'),
     Result(result='deferred')])

# add possible entry conditions
session.add_all([
    Entry_Requirement(
        giveaway_type='Watch a short video',
        query_selector="('.continue_button_inner', '.boxClickTarget')"),
    Entry_Requirement(giveaway_type='No entry requirement',
                      query_selector="('.boxClickTarget', '.a-button-input')"),
    Entry_Requirement(
        giveaway_type='Follow * on *',
        query_selector=
        "('.a-button-input','.a-button-input', '.boxClickTarget')"),
    Entry_Requirement(giveaway_type='Subscribe to a newsletter',
                      query_selector="('.a-button-input')"),
])

session.commit()

#result: a-size-base-plus a-color-secondary qa-giveaway-result-text a-text-bold
#deferred result: a-size-base-plus a-color-secondary qa-giveaway-result-text a-text-bold
#time: a-size-small a-color-secondary qa-giveaway-winner-declare-time-text
#Aug 17, 2018 11:59 PM PDT

# prize_image in #prize-image
예제 #9
0
                                   if random.random() < 0.5
                                   else None))
    for i, power_supply in enumerate(power_supplies)]

user_addresses = [build_address(postal_code)
                  for postal_code in postal_code_samples]

users = [User(document=f"0123456789{i}",
              first_name="User",
              last_name=str(i),
              email=f"user{i}@example.com",
              phone_number=f"551191234567{i}"[0:13],
              address=address)
         for i, address in enumerate(user_addresses)]

bills = [Bill(user=user,
              value=round(random.uniform(0.0, 500.0), 2),
              paid=random.random() < 0.5,
              due_date=date.today()-timedelta(weeks=4*i))
         for i in range(20)
         for user in users]

session.add_all(ps_addresses)
session.add_all(power_supplies)
session.add_all(occurrences)
session.add_all(user_addresses)
session.add_all(users)
session.add_all(bills)

session.commit()
def process_image():
    '''
    takes user input, create thumbnail, and store in DB
  '''

    uploadsDirExists = pathlib.Path('./uploads').exists()
    thumbnailsDirExists = pathlib.Path('./thumbnails').exists()

    if not uploadsDirExists:
        pathlib.Path('./uploads').mkdir()
        print("Uploads folder did not exist but was created.")
        print("Add images to the Uploads folder and try again.")
        return
    if not thumbnailsDirExists:
        pathlib.Path('./thumbnails').mkdir()
        print("Thumbnails folder was created")

    pics = list(pathlib.Path('./uploads').glob('*.*'))

    size = (128, 128)
    userFileName = input("Input image name:  \n")
    imgToProcess = 'noFile'
    stem = 'noStem'
    suffix = 'noSuffix'

    for pic in pics:
        if userFileName == pic.name:
            imgToProcess = str(pic.name)
            stem = str(pic.stem)
            suffix = str(pic.suffix)
        elif userFileName == pic.stem and pic.suffix:
            imgToProcess = str(pic.name)
            stem = str(pic.stem)
            suffix = str(pic.suffix)

    pathToImg = str(pathlib.Path('./uploads/{0}'.format(imgToProcess)))

    thumbnailImg = '{0}.thumbnail{1}'.format(stem, suffix)
    pathToOutput = str(pathlib.Path('./thumbnails/{0}'.format(thumbnailImg)))

    try:
        inThumbnails = pathlib.Path(pathToOutput).exists()

        if inThumbnails:
            print('Thumbnail already exists')
            return

        img = PIL.Image.open(pathToImg)
        img.thumbnail(size)
        img.save(pathToOutput, "JPEG")
        print('Created: {0} of size{1}'.format(thumbnailImg, str(size)))

    except FileNotFoundError:
        print('Oops there was an error.')
        print("File not found.")
        return

    except OSError:
        print('Oops there was an error.')
        print('Not an image file. Try again.')
        return

    except:
        print('Unexpected error:', sys.exc_info()[0])
        print('Check your configuration and try again.')
        return

    originalImage = Images(filename=imgToProcess,
                           thumbnailImg=thumbnailImg,
                           thumbnailPath=pathToOutput,
                           isThumbnail=False)

    processedImage = Thumbnails(filename=thumbnailImg,
                                original=imgToProcess,
                                originalPath=pathToImg,
                                isThumbnail=True)

    session.add_all([originalImage, processedImage])
    session.commit()