本文整理汇总了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
示例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
示例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