Skip to content

Instantly share code, notes, and snippets.

@saumalya75
Last active April 6, 2022 05:58
Show Gist options
  • Save saumalya75/bb0fe43063c91c165e50452b395da597 to your computer and use it in GitHub Desktop.
Save saumalya75/bb0fe43063c91c165e50452b395da597 to your computer and use it in GitHub Desktop.
This lambda is able to lint any python file
import boto3
import datetime
from pylint import epylint as lint
'''
{
"py_file_key": "input-scripts/python-scripts/input_python_script.py",
"py_file_bucket": "linter-testing-bucket"
}
'''
s3_client = boto3.client("s3")
s3_resource = boto3.resource("s3")
S3_BUCKET = 'linter-testing-bucket'
def handler(event, context):
py_file_key = event['py_file_key']
py_file_bucket = event.get('py_file_bucket', None) or S3_BUCKET
print(f"Reading code file - s3://{py_file_bucket}/{py_file_key}")
file_content = s3_client.get_object(Bucket=py_file_bucket, Key=py_file_key)["Body"].read()
tmp_file_name = '/tmp/code_file_f{0}.py'.format(context.aws_request_id.replace('-', ''))
print(f"Writing code content to local temp disk with '{tmp_file_name}' name.")
with open(tmp_file_name, 'wb') as tw:
tw.write(file_content)
print(f"Running linter on the code...")
# lint_command = f'{tmp_file_name} --output-format=json'
lint_command = tmp_file_name
pylint_stdout, _ = lint.py_run(lint_command, return_std=True)
result_file_key = "linter-output/py-lint-output/{0}".format(
py_file_key.split('/')[-1].split('.')[0]
+ '_lint_output_'
+ datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S')
+ '.txt'
)
print(f"Writing lint result to s3://{py_file_bucket}/{result_file_key} file.")
s3_resource.Bucket(py_file_bucket).put_object(Key=result_file_key, Body=pylint_stdout.read().encode('utf-8'))
print(f"Linting for s3://{py_file_bucket}/{py_file_key} code file is done.")
import datetime
class BikeRental:
def __init__(self,stock=0):
"""
Our constructor class that instantiates bike rental shop.
"""
self.stock = stock
def displaystock(self):
"""
Displays the bikes currently available for rent in the shop.
"""
print("We have currently {} bikes available to rent.".format(self.stock))
return self.stock
def rentBikeOnHourlyBasis(self, n):
"""
Rents a bike on hourly basis to a customer.
"""
if n <= 0:
print("Number of bikes should be positive!")
return None
elif n > self.stock:
print("Sorry! We have currently {} bikes available to rent.".format(self.stock))
return None
else:
now = datetime.datetime.now()
print("You have rented a {} bike(s) on hourly basis today at {} hours.".format(n,now.hour))
print("You will be charged $5 for each hour per bike.")
print("We hope that you enjoy our service.")
self.stock -= n
return now
def rentBikeOnDailyBasis(self, n):
"""
Rents a bike on daily basis to a customer.
"""
if n <= 0:
print("Number of bikes should be positive!")
return None
elif n > self.stock:
print("Sorry! We have currently {} bikes available to rent.".format(self.stock))
return None
else:
now = datetime.datetime.now()
print("You have rented {} bike(s) on daily basis today at {} hours.".format(n, now.hour))
print("You will be charged $20 for each day per bike.")
print("We hope that you enjoy our service.")
self.stock -= n
return now
def rentBikeOnWeeklyBasis(self, n):
"""
Rents a bike on weekly basis to a customer.
"""
if n <= 0:
print("Number of bikes should be positive!")
return None
elif n > self.stock:
print("Sorry! We have currently {} bikes available to rent.".format(self.stock))
return None
else:
now = datetime.datetime.now()
print("You have rented {} bike(s) on weekly basis today at {} hours.".format(n, now.hour))
print("You will be charged $60 for each week per bike.")
print("We hope that you enjoy our service.")
self.stock -= n
return now
def returnBike(self, request):
"""
1. Accept a rented bike from a customer
2. Replensihes the inventory
3. Return a bill
"""
rentalTime, rentalBasis, numOfBikes = request
bill = 0
if rentalTime and rentalBasis and numOfBikes:
self.stock += numOfBikes
now = datetime.datetime.now()
rentalPeriod = now - rentalTime
# hourly bill calculation
if rentalBasis == 1:
bill = round(rentalPeriod.seconds / 3600) * 5 * numOfBikes
# daily bill calculation
elif rentalBasis == 2:
bill = round(rentalPeriod.days) * 20 * numOfBikes
# weekly bill calculation
elif rentalBasis == 3:
bill = round(rentalPeriod.days / 7) * 60 * numOfBikes
if (3 <= numOfBikes <= 5):
print("You are eligible for Family rental promotion of 30% discount")
bill = bill * 0.7
print("Thanks for returning your bike. Hope you enjoyed our service!")
print("That would be ${}".format(bill))
return bill
else:
print("Are you sure you rented a bike with us?")
return None
class Customer:
def __init__(self):
"""
Our constructor method which instantiates various customer objects.
"""
self.bikes = 0
self.rentalBasis = 0
self.rentalTime = 0
self.bill = 0
def requestBike(self):
"""
Takes a request from the customer for the number of bikes.
"""
bikes = input("How many bikes would you like to rent?")
try:
bikes = int(bikes)
except ValueError:
print("That's not a positive integer!")
return -1
if bikes < 1:
print("Invalid input. Number of bikes should be greater than zero!")
return -1
else:
self.bikes = bikes
return self.bikes
def returnBike(self):
"""
Allows customers to return their bikes to the rental shop.
"""
if self.rentalBasis and self.rentalTime and self.bikes:
return self.rentalTime, self.rentalBasis, self.bikes
else:
return 0,0,0
@saumalya75
Copy link
Author

************* Module code_file_fffa4180ec9c041b09bfb01026b2749b2
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:4: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:27: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:31: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:33: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:34: convention (C0301, line-too-long, ) Line too long (103/100)
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:39: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:40: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:52: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:54: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:55: convention (C0301, line-too-long, ) Line too long (101/100)
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:61: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:72: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:73: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:76: convention (C0301, line-too-long, ) Line too long (102/100)
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:82: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:84: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:98: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:102: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:106: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:110: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:111: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:112: convention (C0325, superfluous-parens, ) Unnecessary parens after 'if' keyword
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:131: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:137: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:142: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:156: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:162: convention (C0303, trailing-whitespace, ) Trailing whitespace
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:1: convention (C0114, missing-module-docstring, ) Missing module docstring
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:3: convention (C0115, missing-class-docstring, BikeRental) Missing class docstring
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:17: convention (C0209, consider-using-f-string, BikeRental.displaystock) Formatting a regular string which could be a f-string
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:20: convention (C0103, invalid-name, BikeRental.rentBikeOnHourlyBasis) Method name "rentBikeOnHourlyBasis" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:20: convention (C0103, invalid-name, BikeRental.rentBikeOnHourlyBasis) Argument name "n" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:24: refactor (R1705, no-else-return, BikeRental.rentBikeOnHourlyBasis) Unnecessary "elif" after "return", remove the leading "el" from "elif"
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:29: convention (C0209, consider-using-f-string, BikeRental.rentBikeOnHourlyBasis) Formatting a regular string which could be a f-string
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:34: convention (C0209, consider-using-f-string, BikeRental.rentBikeOnHourlyBasis) Formatting a regular string which could be a f-string
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:41: convention (C0103, invalid-name, BikeRental.rentBikeOnDailyBasis) Method name "rentBikeOnDailyBasis" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:41: convention (C0103, invalid-name, BikeRental.rentBikeOnDailyBasis) Argument name "n" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:45: refactor (R1705, no-else-return, BikeRental.rentBikeOnDailyBasis) Unnecessary "elif" after "return", remove the leading "el" from "elif"
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:50: convention (C0209, consider-using-f-string, BikeRental.rentBikeOnDailyBasis) Formatting a regular string which could be a f-string
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:55: convention (C0209, consider-using-f-string, BikeRental.rentBikeOnDailyBasis) Formatting a regular string which could be a f-string
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:62: convention (C0103, invalid-name, BikeRental.rentBikeOnWeeklyBasis) Method name "rentBikeOnWeeklyBasis" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:62: convention (C0103, invalid-name, BikeRental.rentBikeOnWeeklyBasis) Argument name "n" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:66: refactor (R1705, no-else-return, BikeRental.rentBikeOnWeeklyBasis) Unnecessary "elif" after "return", remove the leading "el" from "elif"
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:71: convention (C0209, consider-using-f-string, BikeRental.rentBikeOnWeeklyBasis) Formatting a regular string which could be a f-string
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:76: convention (C0209, consider-using-f-string, BikeRental.rentBikeOnWeeklyBasis) Formatting a regular string which could be a f-string
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:85: convention (C0103, invalid-name, BikeRental.returnBike) Method name "returnBike" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:91: convention (C0103, invalid-name, BikeRental.returnBike) Variable name "rentalTime" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:91: convention (C0103, invalid-name, BikeRental.returnBike) Variable name "rentalBasis" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:91: convention (C0103, invalid-name, BikeRental.returnBike) Variable name "numOfBikes" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:94: refactor (R1705, no-else-return, BikeRental.returnBike) Unnecessary "else" after "return", remove the "else" and de-indent the code inside it
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:97: convention (C0103, invalid-name, BikeRental.returnBike) Variable name "rentalPeriod" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:117: convention (C0209, consider-using-f-string, BikeRental.returnBike) Formatting a regular string which could be a f-string
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:133: convention (C0103, invalid-name, Customer.init) Attribute name "rentalBasis" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:134: convention (C0103, invalid-name, Customer.init) Attribute name "rentalTime" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:125: convention (C0115, missing-class-docstring, Customer) Missing class docstring
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:138: convention (C0103, invalid-name, Customer.requestBike) Method name "requestBike" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:150: refactor (R1705, no-else-return, Customer.requestBike) Unnecessary "else" after "return", remove the "else" and de-indent the code inside it
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:157: convention (C0103, invalid-name, Customer.returnBike) Method name "returnBike" doesn't conform to snake_case naming style
/tmp/code_file_fffa4180ec9c041b09bfb01026b2749b2.py:161: refactor (R1705, no-else-return, Customer.returnBike) Unnecessary "else" after "return", remove the "else" and de-indent the code inside it


Your code has been rated at 3.37/10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment