当前位置: 首页>>代码示例>>Python>>正文


Python Course.from_json方法代码示例

本文整理汇总了Python中courses.models.Course.from_json方法的典型用法代码示例。如果您正苦于以下问题:Python Course.from_json方法的具体用法?Python Course.from_json怎么用?Python Course.from_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在courses.models.Course的用法示例。


在下文中一共展示了Course.from_json方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: fetch_courses

# 需要导入模块: from courses.models import Course [as 别名]
# 或者: from courses.models.Course import from_json [as 别名]
    def fetch_courses(self, term, verbose=False):
        """ Get all courses for the given term.

        Parameters:
        -----------
        term : Term
            The term to fetch courses for.

        verbose : bool [default False]
            Whether or not to show messages throughout the process of fetching
            courses.

        """
        if verbose:
            self.stdout.write('Fetching data...')

        response = requests.post(
            url=COURSE_URL,
            data={
                'term': term.to_code(),
            }
        )

        if verbose:
            self.stdout.write('Parsing data...')

        root = ElementTree.fromstring(response.text.encode('utf-8'))
        course_count = len(root)

        bar_count = 40

        old_crns = set([c.crn for c in Course.objects.filter(term=term)])
        crns = set()

        if verbose:
            self.stdout.write('Building courses...')

        for i, course in enumerate(root):
            if verbose:
                count = int(float(i + 1) / course_count * bar_count)
                log = '[' + '=' * count + ' ' * (bar_count - count) + ']'
                self.stdout.write(log, ending='\r')
                self.stdout.flush()

            course_json = {}

            for attr in course:
                name = ATTRIBUTE_MAP.get(attr.tag, attr.tag)
                course_json[name] = attr.text

            try:
                schedules = []

                try:
                    old_course = Course.objects.get(crn=course_json['crn'], term=term)
                    schedules = [(s.schedule, s.shown) for s in old_course.courseshown_set.all()]
                    old_course.delete()

                except Course.DoesNotExist:
                    pass

                course = Course.from_json(course_json, term)
                crns.add(course.crn)

                for schedule, shown in schedules:
                    schedule.set_shown(course, shown)

            except Exception:
                self.stdout.write('Error parsing course:')
                self.stdout.write(str(course_json))
                traceback.print_exc(file=self.stdout)
                self.stdout.write('\n')

        # Remove stale courses
        for crn in old_crns - crns:
            Course.objects.get(crn=crn, term=term).delete()
开发者ID:brett-patterson,项目名称:rice-courses,代码行数:78,代码来源:fetchcourses.py


注:本文中的courses.models.Course.from_json方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。