Uni 학습 Python에서 지난 학기를 방금 보냈습니다. 나는 그것을 정말로 즐겼으며 더 많은 'pythonic'코드를 작성하는 방법에 대한 몇 가지 팁을 기대했다.Python 방식 코딩하기
이것은 최근 과제에서 작성한 __init__
수업입니다. 필자가 쓴 시간에, 나는 람다 (lambdas)를 사용하여, 또는 더 깔끔하고 효율적인 방법으로 이것을 다시 쓸 수있는 방법을 찾지 만, 시간이 없어졌다.
def __init__(self, dir):
def _read_files(_, dir, files):
for file in files:
if file == "classes.txt":
class_list = readtable(dir+"/"+file)
for item in class_list:
Enrol.class_info_dict[item[0]] = item[1:]
if item[1] in Enrol.classes_dict:
Enrol.classes_dict[item[1]].append(item[0])
else:
Enrol.classes_dict[item[1]] = [item[0]]
elif file == "subjects.txt":
subject_list = readtable(dir+"/"+file)
for item in subject_list:
Enrol.subjects_dict[item[0]] = item[1]
elif file == "venues.txt":
venue_list = readtable(dir+"/"+file)
for item in venue_list:
Enrol.venues_dict[item[0]] = item[1:]
elif file.endswith('.roll'):
roll_list = readlines(dir+"/"+file)
file = os.path.splitext(file)[0]
Enrol.class_roll_dict[file] = roll_list
for item in roll_list:
if item in Enrol.enrolled_dict:
Enrol.enrolled_dict[item].append(file)
else:
Enrol.enrolled_dict[item] = [file]
try:
os.path.walk(dir, _read_files, None)
except:
print "There was a problem reading the directory"
보시다시피 다소 부피가 커집니다. 누구나 시간이나 성향이 있다면, 파이썬 모범 사례에 대한 몇 가지 팁을 정말 고맙게 생각합니다.
감사합니다. 코드 비트를 정리 할 수
참고 :이 모듈은'enrol'이라는 모듈의 일부로 클래스 클래스 인 것처럼'__init__' 인'Enroll' 클래스를 포함합니다 –
: http://stackoverflow.com/questions/2943396/python -need-some-help –
이 표정 놀라 울 정도 * http://stackoverflow.com/questions/2943396/python-need-some-help – Johnsyweb