當前位置: 首頁>>代碼示例>>Python>>正文


Python Schedule.create_tables方法代碼示例

本文整理匯總了Python中schedule.Schedule.create_tables方法的典型用法代碼示例。如果您正苦於以下問題:Python Schedule.create_tables方法的具體用法?Python Schedule.create_tables怎麽用?Python Schedule.create_tables使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在schedule.Schedule的用法示例。


在下文中一共展示了Schedule.create_tables方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: load

# 需要導入模塊: from schedule import Schedule [as 別名]
# 或者: from schedule.Schedule import create_tables [as 別名]
def load(feed_filename, db_filename=":memory:"):
    schedule = Schedule(db_filename)
    schedule.create_tables()

    fd = feed.Feed(feed_filename)

    for gtfs_class in (Agency,
                       Route,
                       Stop,
                       Trip,
                       StopTime,
                       ServicePeriod,
                       ServiceException,
                       Fare,
                       FareRule,
                       ShapePoint,
                       Frequency,
                       Transfer,
    ):

        try:
            for i, record in enumerate(fd.get_table(gtfs_class.TABLENAME + ".txt")):
                if i % 500 == 0:
                    schedule.session.commit()

                instance = gtfs_class(**record.to_dict())
                schedule.session.add(instance)
        except KeyError:
            # TODO: check if the table is required
            continue

    schedule.session.commit()

    return schedule
開發者ID:alexcarruthers,項目名稱:gtfs,代碼行數:36,代碼來源:loader.py

示例2: load

# 需要導入模塊: from schedule import Schedule [as 別名]
# 或者: from schedule.Schedule import create_tables [as 別名]
def load(feed_filename, db_connection=":memory:", strip_fields=True,
         commit_chunk=500, drop_agency=False, agency_id_override=None, **kwargs):
    if 'db_filename' in kwargs:
        db_connection = kwargs['db_filename']
    schedule = Schedule(db_connection)
    schedule.create_tables(Entity.metadata)
    fd = feed.Feed(feed_filename)
    
    gtfs_classes = [Agency,
                    Stop,
                    Route,
                    Service,
                    ServiceException,
                    Trip, 
                    StopTime,
                    Fare,
                    FareRule,
                    ShapePoint,
                    Frequency,
                    Transfer,
                    FeedInfo,
                    ]
    gtfs_tables = {}

    no_calendar = False
    for gtfs_class in gtfs_classes:
        print('Loading GTFS data for %s:' % gtfs_class)
        gtfs_filename = gtfs_class.table_name + '.txt'
     
        try:
            gtfs_tables[gtfs_class] = fd.read_table(gtfs_filename)
        except (KeyError, IOError) as e:
            if gtfs_class.gtfs_required:
                raise IOError('Error: could not find %s' % gtfs_filename)
            elif no_calendar is True and gtfs_class == ServiceException:
                raise IOError('Error: could not find either %s or %s' % \
                              (gtfs_filename, Service.table_name + '.txt'))
            else:
                if gtfs_class == Service:
                    # OK if there's no calendar file, but then later ensure 
                    # that there is a calendar_dates file
                    no_calendar = True
                print('File %s not present but not mandatory, continuing.' % \
                      gtfs_filename)
                continue

    # peek at the Agency table
    record = gtfs_tables[Agency].peek()
    if 'agency_id' not in record or not record['agency_id'].strip():
        feed_agency_id = record['agency_name'].lower().strip()
    else:
        feed_agency_id = record['agency_id'].strip()
    agency_id = agency_id_override or feed_agency_id

    if drop_agency:
        # reversed so we don't trip-up on foreign key constraints
        for gtfs_class in reversed(gtfs_classes):
            schedule.session.query(gtfs_class).\
                filter(gtfs_class.agency_id==agency_id).\
                delete()
    for gtfs_class in gtfs_classes:
        if gtfs_class not in gtfs_tables:
            continue
        gtfs_table = gtfs_tables[gtfs_class]
        for i, record in enumerate(gtfs_table):
            if len(record) > 0:
                if strip_fields is True:
                    record_stripped = {}
                    for key in record:
                        record_stripped[key.strip()] = record[key].strip()
                    record = record_stripped
                if getattr(record, 'agency_id', feed_agency_id).strip() != feed_agency_id:
                    raise Exception('Loading multiple agencies from the same feed is not supported')
                record['agency_id'] = agency_id
                instance = gtfs_class(**record)
                schedule.session.add(instance)
                if i % commit_chunk == 0 and i > 0:
                    if not drop_agency:
                        schedule.session.commit()
                    sys.stdout.write('.')
                    sys.stdout.flush()
        print('%d record%s committed.' % ((i+1), '' if i == 0 else 's'))
        if not drop_agency:
            schedule.session.commit()
    schedule.session.commit()

    print('Complete.')
    return schedule
開發者ID:eoghanmurray,項目名稱:gtfs-sql,代碼行數:90,代碼來源:loader.py

示例3: load

# 需要導入模塊: from schedule import Schedule [as 別名]
# 或者: from schedule.Schedule import create_tables [as 別名]
def load(feed_filename, db_filename=":memory:", strip_fields=True, 
         commit_chunk=500):
    
    schedule = Schedule(db_filename) 
    schedule.create_tables(Entity.metadata)
    fd = feed.Feed(feed_filename)
    
    gtfs_classes = (Agency,
                    Stop,
                    Route,
                    Service,
                    ServiceException,
                    Trip, 
                    StopTime,
                    Fare,
                    FareRule,
                    ShapePoint,
                    Frequency,
                    Transfer,
                    FeedInfo,
                    )

    no_calendar = False
    for gtfs_class in gtfs_classes:

        print('Loading GTFS data for %s:' % gtfs_class)
        gtfs_filename = gtfs_class.table_name + '.txt'
     
        try:
            gtfs_table = fd.read_table(gtfs_filename)
        except (KeyError, IOError) as e:
            if gtfs_class.gtfs_required:
                raise IOError('Error: could not find %s' % gtfs_filename)
            elif no_calendar is True and gtfs_class == ServiceException:
                raise IOError('Error: could not find either %s or %s' % \
                              (gtfs_filename, Service.table_name + '.txt'))
            else:
                if gtfs_class == Service:
                    # OK if there's no calendar file, but then later ensure 
                    # that there is a calendar_dates file
                    no_calendar = True
                print('File %s not present but not mandatory, continuing.' % \
                      gtfs_filename)
                continue
        
        for i, record in enumerate(gtfs_table):
            if len(record) > 0:
                if strip_fields is True:
                    for key in record:
                        record[key] = record[key].strip()
                instance = gtfs_class(**record)
                schedule.session.add(instance)
                if i % commit_chunk == 0 and i > 0:
                    schedule.session.commit()
                    sys.stdout.write('.')
                    sys.stdout.flush()
        print('%d record%s committed.' % ((i+1), '' if i == 0 else 's'))
        schedule.session.commit()

    print('Complete.')
    return schedule
開發者ID:andrewblim,項目名稱:gtfs-sql,代碼行數:63,代碼來源:loader.py


注:本文中的schedule.Schedule.create_tables方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。