Skip to content

Commit 4c9b4aa

Browse files
kiblik1yaacov
andauthored
Add black lint (#483)
* Fix #482 * apply black codestyle * modify flake8 settings to be compatible with Black * change job name to be less ambiguous * integrate black into ci as a step reather than a standalone job and add it to the dev deps * Update .flake8 Use better formatting of necessary changes to comply with Black codestyle Co-authored-by: Yaacov Zamir <[email protected]> * add W508 to ignored rules * fix typo * fix yet another typo... --------- Co-authored-by: Yaacov Zamir <[email protected]>
1 parent 5b86d4e commit 4c9b4aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+827
-655
lines changed

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
[flake8]
22
show_source = True
33
statistics = True
4+
5+
# E501: line to long.
6+
# E203: whitespace before ':' to accept black code style
7+
# W503: line break before binary operator
8+
ignore = E501,E203,W503

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ jobs:
1717
pip install -r requirements-dev.txt
1818
- name: Lint
1919
run: flake8 --show-source --statistics
20+
- name: Blint
21+
run: black --check --diff .
2022
- name: Run tests
2123
run: pytest

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ install:
99

1010
script:
1111
- flake8
12+
- black
1213
- pytest --check-links --ignore=docs/course_materials/reveal

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ name = "pypi"
77

88
[dev-packages]
99

10+
black = "*"
1011
flake8 = "*"
1112
pytest = "*"
1213
pytest-coverage = "*"

classroom/connect_service.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77

88
# If modifying these scopes, delete the file token.pickle.
9-
SCOPES = ['https://www.googleapis.com/auth/classroom.rosters',
10-
'https://www.googleapis.com/auth/classroom.courses',
11-
'https://www.googleapis.com/auth/classroom.topics']
9+
SCOPES = [
10+
"https://www.googleapis.com/auth/classroom.rosters",
11+
"https://www.googleapis.com/auth/classroom.courses",
12+
"https://www.googleapis.com/auth/classroom.topics",
13+
]
1214

1315

1416
def create_service():
@@ -20,21 +22,19 @@ def create_service():
2022
# The file token.pickle stores the user's access and refresh tokens, and is
2123
# created automatically when the authorization flow completes for the first
2224
# time.
23-
if os.path.exists('token.pickle'):
24-
with open('token.pickle', 'rb') as token:
25+
if os.path.exists("token.pickle"):
26+
with open("token.pickle", "rb") as token:
2527
creds = pickle.load(token)
2628
# If there are no (valid) credentials available, let the user log in.
2729
if not creds or not creds.valid:
2830
if creds and creds.expired and creds.refresh_token:
2931
creds.refresh(Request())
3032
else:
31-
flow = InstalledAppFlow.from_client_secrets_file(
32-
'credentials.json', SCOPES)
33+
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
3334
creds = flow.run_local_server(port=0)
3435
# Save the credentials for the next run
35-
with open('token.pickle', 'wb') as token:
36+
with open("token.pickle", "wb") as token:
3637
pickle.dump(creds, token)
3738

38-
service = build('classroom', 'v1', credentials=creds,
39-
cache_discovery=False)
39+
service = build("classroom", "v1", credentials=creds, cache_discovery=False)
4040
return service

classroom/content_edit.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
def print_topics(service, course_id):
77
# Call the Classroom API
88
results = service.courses().topics().list(courseId=course_id).execute()
9-
topics = results.get('topic', [])
9+
topics = results.get("topic", [])
1010

1111
if not topics:
12-
print('No topics found.')
12+
print("No topics found.")
1313
else:
14-
print('Topics:')
14+
print("Topics:")
1515
for topic in topics:
1616
print(f"Title: {topic['name']}, ID: {topic['topicId']}")
1717

1818

1919
def create_topic(service, title, course_id):
20-
topic = {'name': title}
21-
new_topic = service.courses().topics().create(
22-
courseId=course_id,
23-
body=topic).execute()
20+
topic = {"name": title}
21+
new_topic = (
22+
service.courses().topics().create(courseId=course_id, body=topic).execute()
23+
)
2424
print(f"Topic created: {new_topic['name']} {new_topic['topicId']}")
2525

2626

classroom/course_creator.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,43 @@
66
def print_courses(service):
77
# Call the Classroom API
88
results = service.courses().list(pageSize=10).execute()
9-
courses = results.get('courses', [])
9+
courses = results.get("courses", [])
1010

1111
if not courses:
12-
print('No courses found.')
12+
print("No courses found.")
1313
else:
14-
print('Courses:')
14+
print("Courses:")
1515
for course in courses:
1616
print(f"Title: {course['name']}, ID: {course['id']}")
1717

1818

1919
def create_course(service, course_name):
20-
course_data = {'name': course_name,
21-
'descriptionHeading': 'Welcome to ' + course_name,
22-
'description': ("We'll be learning about coding from a " +
23-
"combination of lectures, course work, " +
24-
"and home projects. Let's start!"),
25-
'ownerId': 'me',
26-
'courseState': 'PROVISIONED'}
20+
course_data = {
21+
"name": course_name,
22+
"descriptionHeading": "Welcome to " + course_name,
23+
"description": (
24+
"We'll be learning about coding from a "
25+
+ "combination of lectures, course work, "
26+
+ "and home projects. Let's start!"
27+
),
28+
"ownerId": "me",
29+
"courseState": "PROVISIONED",
30+
}
2731
course = service.courses().create(body=course_data).execute()
2832
print(f"Course created: {course.get('name')} {course.get('id')}")
29-
return course.get('id')
33+
return course.get("id")
3034

3135

3236
def load_list(list_path):
33-
'''
37+
"""
3438
Load student/teacher list from csv file.
35-
'''
39+
"""
3640
if not os.path.exists(list_path):
3741
print(f"This path does not exist: {list_path}.")
3842
return None
39-
with open(list_path, mode='r') as csv_file:
43+
with open(list_path, mode="r") as csv_file:
4044
mail_list = []
41-
csv_reader = csv.reader(csv_file, delimiter=',')
45+
csv_reader = csv.reader(csv_file, delimiter=",")
4246
if csv_reader is None:
4347
print("The list is empty.")
4448
return mail_list
@@ -59,25 +63,25 @@ def create_invitation(service, args, invite_type):
5963
Function:
6064
- Creates invitations using the mail list.
6165
"""
62-
if invite_type == 'STUDENT':
66+
if invite_type == "STUDENT":
6367
list_path = args.student_list
6468
else:
6569
list_path = args.teacher_list
6670
mail_list = load_list(list_path)
6771
if len(mail_list) > 0:
6872
for mail in mail_list:
6973
invitation = {
70-
'courseId': args.id,
71-
'role': invite_type,
72-
'userId': mail,
74+
"courseId": args.id,
75+
"role": invite_type,
76+
"userId": mail,
7377
}
7478
try:
7579
service.invitations().create(body=invitation).execute()
76-
print(f'Invitation was sent to {mail}')
80+
print(f"Invitation was sent to {mail}")
7781
except errors.HttpError as e:
78-
if '409' in str(e):
79-
print(f'Not added, {mail} has a pending invitation.')
80-
elif '400' in str(e):
81-
print(f'Not added, {mail} already listed in course.')
82+
if "409" in str(e):
83+
print(f"Not added, {mail} has a pending invitation.")
84+
elif "400" in str(e):
85+
print(f"Not added, {mail} already listed in course.")
8286
else:
83-
print('No permissions or wrong course ID.')
87+
print("No permissions or wrong course ID.")

classroom/rose_class.py

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,97 @@
66

77

88
def main():
9-
'''
9+
"""
1010
Getting user input and preforming corresponding actions.
1111
Available functions:
1212
- Create course/topic in classroom.
1313
- Print existing courses/topics.
1414
- Update teacher list.
1515
- Update student list.
16-
'''
16+
"""
1717

1818
logging.basicConfig(level=logging.INFO)
19-
parser = argparse.ArgumentParser(description='ROSE Classroom')
20-
parser.add_argument('--course', action='store_true',
21-
help='A flag for course actions, stores True. '
22-
'Has to be followed by an action as --create. '
23-
'If not specified, will be False.')
24-
parser.add_argument('--topic', action='store_true',
25-
help='A flag for topic actions, stores True. '
26-
'Has to be followed by an action as --create. '
27-
'If not specified, will be False.')
28-
parser.add_argument('--create', '-c', dest='name',
29-
help='Creating a new instance using given name. '
30-
'If not specified, cannot be created. '
31-
'Follows an instance type as --course. '
32-
'For creating Topics, Assignments and more '
33-
'please specify the course/topic id using -i.')
34-
parser.add_argument('--print', '-p', action="store_true",
35-
help='Printing existing instances.')
36-
parser.add_argument('--teacher_list', '-t', dest='teacher_list',
37-
help='Adding teachers using a list, '
38-
'expects a csv file. '
39-
'If course exists, '
40-
'please provide course ID using -i.')
41-
parser.add_argument('--student_list', '-s', dest='student_list',
42-
help='Adding students using a list, '
43-
'expects csv file. '
44-
'If course exists, '
45-
'please provide course ID using -i.')
46-
parser.add_argument('--id', '-i',
47-
help='Specifies an instance id. Can be used for '
48-
'adding student lists or teacher lists, adding '
49-
'Topics, Homework and more. '
50-
'Please specify the needed action. '
51-
'Use combined with instance type as --course.')
19+
parser = argparse.ArgumentParser(description="ROSE Classroom")
20+
parser.add_argument(
21+
"--course",
22+
action="store_true",
23+
help="A flag for course actions, stores True. "
24+
"Has to be followed by an action as --create. "
25+
"If not specified, will be False.",
26+
)
27+
parser.add_argument(
28+
"--topic",
29+
action="store_true",
30+
help="A flag for topic actions, stores True. "
31+
"Has to be followed by an action as --create. "
32+
"If not specified, will be False.",
33+
)
34+
parser.add_argument(
35+
"--create",
36+
"-c",
37+
dest="name",
38+
help="Creating a new instance using given name. "
39+
"If not specified, cannot be created. "
40+
"Follows an instance type as --course. "
41+
"For creating Topics, Assignments and more "
42+
"please specify the course/topic id using -i.",
43+
)
44+
parser.add_argument(
45+
"--print", "-p", action="store_true", help="Printing existing instances."
46+
)
47+
parser.add_argument(
48+
"--teacher_list",
49+
"-t",
50+
dest="teacher_list",
51+
help="Adding teachers using a list, "
52+
"expects a csv file. "
53+
"If course exists, "
54+
"please provide course ID using -i.",
55+
)
56+
parser.add_argument(
57+
"--student_list",
58+
"-s",
59+
dest="student_list",
60+
help="Adding students using a list, "
61+
"expects csv file. "
62+
"If course exists, "
63+
"please provide course ID using -i.",
64+
)
65+
parser.add_argument(
66+
"--id",
67+
"-i",
68+
help="Specifies an instance id. Can be used for "
69+
"adding student lists or teacher lists, adding "
70+
"Topics, Homework and more. "
71+
"Please specify the needed action. "
72+
"Use combined with instance type as --course.",
73+
)
5274

5375
args = parser.parse_args()
5476

55-
'''Set up the service to google classroom'''
77+
"""Set up the service to google classroom"""
5678
service = connect_service.create_service()
5779

5880
if args.id and len(args.id) < 12:
59-
print('Please check the ID specified and try again.')
81+
print("Please check the ID specified and try again.")
6082
elif args.course and not args.topic:
6183
if args.name:
6284
args.id = course_creator.create_course(service, args.name)
63-
print(f'The id returned {args.id}')
85+
print(f"The id returned {args.id}")
6486
elif args.print:
6587
course_creator.print_courses(service)
6688
elif not args.id:
67-
print('Please use --help to inspect the possible actions.')
89+
print("Please use --help to inspect the possible actions.")
6890
else:
6991
if args.teacher_list:
70-
course_creator.create_invitation(service, args, 'TEACHER')
92+
course_creator.create_invitation(service, args, "TEACHER")
7193

7294
if args.student_list:
73-
course_creator.create_invitation(service, args, 'STUDENT')
95+
course_creator.create_invitation(service, args, "STUDENT")
7496

7597
no_list = args.student_list is None and args.teacher_list is None
76-
if (no_list):
77-
print('Please use -h to check the available actions.')
98+
if no_list:
99+
print("Please use -h to check the available actions.")
78100
elif args.topic:
79101
if args.course and args.id:
80102
if args.print:
@@ -83,8 +105,8 @@ def main():
83105
if args.name:
84106
content_edit.create_topic(service, args.name, args.id)
85107
else:
86-
print('Wrong action')
108+
print("Wrong action")
87109

88110

89-
if __name__ == '__main__':
111+
if __name__ == "__main__":
90112
main()

docs/course_materials/exercises/09_Extra/class_exercise_1_solution.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
21
def joing_files(file_name1, file_name2, out_file_name):
32
"""
43
Reads the first two files, joins them and writes the data to the third one.
54
"""
65
try:
7-
file1 = open(file_name1, 'r').read()
8-
file2 = open(file_name2, 'r').read()
9-
open(out_file_name, 'w').write(file1 + file2)
10-
print('The files were joined successfully!')
6+
file1 = open(file_name1, "r").read()
7+
file2 = open(file_name2, "r").read()
8+
open(out_file_name, "w").write(file1 + file2)
9+
print("The files were joined successfully!")
1110
except Exception as e:
1211
print(e)
13-
print('Reading or writing error with one of the files!')
12+
print("Reading or writing error with one of the files!")
1413

1514

1615
file_name_1 = input("Enter the first file name: ")

docs/course_materials/exercises/09_Extra/class_exercise_2_solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
end_of_the_year = datetime(2020, 12, 31)
44
today = datetime.now()
55
time_to_the_end_of_the_year = end_of_the_year - today
6-
print('There are {} days left…'.format(time_to_the_end_of_the_year.days))
6+
print("There are {} days left…".format(time_to_the_end_of_the_year.days))

0 commit comments

Comments
 (0)