本文整理汇总了Python中opus_core.database_management.database_server.DatabaseServer.get_database方法的典型用法代码示例。如果您正苦于以下问题:Python DatabaseServer.get_database方法的具体用法?Python DatabaseServer.get_database怎么用?Python DatabaseServer.get_database使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opus_core.database_management.database_server.DatabaseServer
的用法示例。
在下文中一共展示了DatabaseServer.get_database方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AbstractServiceTests
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
class AbstractServiceTests(opus_unittest.OpusTestCase):
def setUp(self):
self.database_name = 'test_services_database'
self.config = TestDatabaseConfiguration(database_name = self.database_name)
self.db_server = DatabaseServer(self.config)
def tearDown(self):
self.db_server.drop_database(self.database_name)
self.db_server.close()
def test_create_when_already_exists(self):
"""Shouldn't do anything if the database already exists."""
self.db_server.create_database(self.database_name)
db = self.db_server.get_database(self.database_name)
self.assertFalse(db.table_exists('run_activity'))
self.assertFalse(db.table_exists('computed_indicators'))
services = AbstractService(self.config)
services.services_db.close()
self.assertTrue(db.table_exists('run_activity'))
self.assertTrue(db.table_exists('computed_indicators'))
def test_create(self):
"""Should create services tables if the database doesn't exist."""
services = AbstractService(self.config)
services.services_db.close()
self.assertTrue(self.db_server.has_database(self.database_name))
db = self.db_server.get_database(self.database_name)
self.assertTrue(db.table_exists('run_activity'))
self.assertTrue(db.table_exists('computed_indicators'))
示例2: create_building_types_table
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def create_building_types_table(self, db_config, db_name):
table_name = 'job_building_types'
dbconfig = DatabaseServerConfiguration(
host_name = db_config.host_name,
user_name = db_config.user_name,
protocol = 'mysql',
password = db_config.password
)
db_server = DatabaseServer(dbconfig)
try:
db = db_server.get_database(db_name)
except:
raise NameError, "Unknown database '%s'!" % db_name
logger.log_status('Creating table %s.' % table_name)
try:
db.DoQuery('DROP TABLE IF EXISTS %s;' % table_name)
db.DoQuery('CREATE TABLE %s '
'(id INT, name varchar(50), home_based INT);'
% table_name)
except:
raise NameError, "Invalid table name specified! (%s)" % table_name
db.DoQuery('INSERT INTO %s (id, name, home_based) VALUES'
'(1, "commercial", 0),'
'(3, "industrial", 0),'
'(2, "governmental", 0),'
'(4, "home_based", 1);'
% table_name)
示例3: run
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def run (self):
time = -1
latest = ""
directoryname = 'data/vibe_gridcell/runs/'
if self.isParcel is True:
directoryname = 'data/vibe_parcel/'
for filename in os.listdir(os.path.join(os.environ['OPUS_HOME'], directoryname)):
print filename
if time == -1:
time = os.path.getmtime(os.path.join(os.environ['OPUS_HOME'], directoryname, filename))
latest = filename
if os.path.getmtime(os.path.join(os.environ['OPUS_HOME'], directoryname, filename)) > time:
time = os.path.getmtime(os.path.join(os.environ['OPUS_HOME'], directoryname, filename))
latest = filename
config = DatabaseServerConfiguration(host_name = 'localhost',
user_name = 'urbansim',
password = 'urbansim',
protocol = 'mysql')
db_server = DatabaseServer(config)
for i in range(1981, 1980+int(self.YearsToRun)):
newdir = latest + '/' + str(i)
flt_directory_in = os.path.join(os.environ['OPUS_HOME'], directoryname ,newdir)
input_storage = flt_storage(storage_location = flt_directory_in)
db = db_server.get_database('ress_'+str(i))
output_storage = StorageFactory().get_storage('sql_storage', storage_location = db)
ExportStorage().export(in_storage=input_storage, out_storage=output_storage)
示例4: opusRun
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def opusRun(progressCB,logCB,params):
param_dict = {}
for key, val in params.iteritems():
param_dict[str(key)] = str(val)
# get parameter values
database_name = param_dict['database_name']
database_server_connection = param_dict['database_server_connection']
households_table_name = 'raw_pums_hh_data'
query = "DELETE h.* FROM %s AS h WHERE h.persons = '00' IS NULL" % (households_table_name)
# create engine and connection
logCB("Openeing database connection\n")
dbs_config = DatabaseServerConfiguration(database_configuration=database_server_connection)
server = DatabaseServer(database_server_configuration = dbs_config)
opus_db = server.get_database(database_name=database_name)
# Do Query
logCB("Deleting empty household records...\n")
opus_db.execute(query)
# Finish up
logCB("Closing database connection\n")
opus_db.close()
logCB('Finished running query\n')
开发者ID:christianurich,项目名称:VIBe2UrbanSim,代码行数:28,代码来源:synthesizer_delete_empty_households_from_raw_pums_data.py
示例5: _create_db_from_chain_via_python
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def _create_db_from_chain_via_python(self,
from_database_configuration,
to_database_configuration,
tables_to_copy):
db_server_from = DatabaseServer(from_database_configuration)
db_server_to = DatabaseServer(to_database_configuration)
db_server_to.drop_database(to_database_configuration.database_name)
db_server_to.create_database(to_database_configuration.database_name)
database_out = db_server_to.get_database(to_database_configuration.database_name)
scenario_db_manager = ScenarioDatabaseManager(
server_configuration = from_database_configuration,
base_scenario_database_name = from_database_configuration.database_name)
table_mapping = scenario_db_manager.get_database_to_table_mapping()
cross_db_operations = CrossDatabaseOperations()
#by default, copy all tables
if tables_to_copy == []:
tables_to_copy = sum(table_mapping.values(), []) # flat a list of lists
elif 'scenario_information' not in tables_to_copy:
tables_to_copy.append('scenario_information')
for database_name, tables in table_mapping.items():
database_in = db_server_from.get_database(database_name)
for table in tables:
if table not in tables_to_copy:
continue
logger.start_block("Copying table '%s' from database '%s'"
% (table, database_name))
try:
cross_db_operations.copy_table(table_to_copy = table,
database_in = database_in,
database_out = database_out,
use_chunking = True)
finally:
logger.end_block()
database_in.close()
self._fix_scenario_information_table(database_out)
database_out.close()
db_server_from.close()
db_server_to.close()
示例6: __init__
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def __init__(self, config):
if 'estimation_database_configuration' in config:
db_server = DatabaseServer(config['estimation_database_configuration'])
db = db_server.get_database(config['estimation_database_configuration'].database_name)
out_storage = StorageFactory().build_storage_for_dataset(
type='sql_storage', storage_location=db)
else:
out_storage = StorageFactory().get_storage(type='flt_storage',
storage_location=os.path.join(config['cache_directory'], str(config['base_year']+1)))
simulation_state = SimulationState()
simulation_state.set_cache_directory(config['cache_directory'])
simulation_state.set_current_time(config['base_year'])
attribute_cache = AttributeCache()
SessionConfiguration(new_instance=True,
package_order=config['dataset_pool_configuration'].package_order,
in_storage=attribute_cache)
if not os.path.exists(os.path.join(config['cache_directory'], str(config['base_year']))):
#raise RuntimeError, "datasets uncached; run prepare_estimation_data.py first"
CacheScenarioDatabase().run(config, unroll_gridcells=False)
for dataset_name in config['datasets_to_preload']:
SessionConfiguration().get_dataset_from_pool(dataset_name)
households = SessionConfiguration().get_dataset_from_pool("household")
household_ids = households.get_id_attribute()
workers = households.get_attribute("workers")
hh_ids = []
member_ids = []
is_worker = []
job_ids = []
for i in range(households.size()):
if workers[i] > 0:
hh_ids += [household_ids[i]] * workers[i]
member_ids += range(1, workers[i]+1)
is_worker += [1] * workers[i]
job_ids += [-1] * workers[i]
in_storage = StorageFactory().get_storage('dict_storage')
persons_table_name = 'persons'
in_storage.write_table(
table_name=persons_table_name,
table_data={
'person_id':arange(len(hh_ids))+1,
'household_id':array(hh_ids),
'member_id':array(member_ids),
'is_worker':array(is_worker),
'job_id':array(job_ids),
},
)
persons = PersonDataset(in_storage=in_storage, in_table_name=persons_table_name)
persons.write_dataset(out_storage=out_storage, out_table_name=persons_table_name)
示例7: get
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def get(self, database):
db_config = ScenarioDatabaseConfiguration()
db_server = DatabaseServer(db_config)
db = db_server.get_database(database)
storage = StorageFactory().get_storage(
'sql_storage',
storage_location = db)
return storage
开发者ID:christianurich,项目名称:VIBe2UrbanSim,代码行数:11,代码来源:assign_bldgs_to_jobs_when_multiple_bldgs_in_parcel.py
示例8: drop_table
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def drop_table(table_name, dbname, schema):
dbserverconfig = IndicatorsDatabaseConfiguration(protocol='postgres')
server = DatabaseServer(database_server_configuration = dbserverconfig)
db = server.get_database(database_name=dbname)
query = 'DROP TABLE %s.%s' % (schema, table_name)
try:
db.execute(query)
logCB('DROPPED TABLE %s \n' % table_name)
except:
return
示例9: TestCreateJobBuildingTypesTable
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
class TestCreateJobBuildingTypesTable(opus_unittest.OpusTestCase):
def setUp(self):
self.db_name = 'test_create_table'
self.db_server = DatabaseServer(TestDatabaseConfiguration(protocol = 'mysql',))
self.db_server.drop_database(self.db_name)
self.db_server.create_database(self.db_name)
self.db = self.db_server.get_database(self.db_name)
def tearDown(self):
self.db.close()
self.db_server.drop_database(self.db_name)
self.db_server.close()
def test_setUp(self):
try:
self.db.DoQuery('select * from job_building_types;')
self.fail('Output table job_building_tpes already exists. (Check setUp)')
except: pass
def test_create_table(self):
CreateJobBuildingTypesTable().create_building_types_table(
TestDatabaseConfiguration(protocol = 'mysql'), self.db_name)
try:
self.db.DoQuery('select * from job_building_types;')
except:
self.fail('Expected output table job_building_types does not exist.')
def test_values(self):
CreateJobBuildingTypesTable().create_building_types_table(
TestDatabaseConfiguration(protocol = 'mysql'), self.db_name)
expected_results = [
['id', 'name', 'home_based'],
[1, "commercial", 0],
[3, "industrial", 0],
[2, "governmental", 0],
[4, "home_based", 1]
]
try:
results = self.db.GetResultsFromQuery(
'select * from job_building_types;')
except:
self.fail('Expected output table job_building_types does not exist.')
self.assert_(expected_results == results,
"Table job_building_types has incorrect values! "
"Expected: %s. Received: %s" % (expected_results, results))
示例10: prepare_for_run
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def prepare_for_run(self, database_configuration, database_name):
## sql protocol, hostname, username and password are set in
## $OPUS_HOME/settings/database_server_setting.xml
db_config = DatabaseConfiguration(database_name=database_name, database_configuration=database_configuration)
db_server = DatabaseServer(db_config)
if not db_server.has_database(database_name):
db_server.create_database(database_name)
db = db_server.get_database(database_name)
self.out_storage = sql_storage(storage_location=db)
return self.out_storage
示例11: save_results
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def save_results(self, out_storage=None, model_name=None):
if self.specification is None or self.coefficients is None:
raise ValueError, "model specification or coefficient is None"
#invalid = self.coefficients.is_invalid()
if False:
logger.log_warning('Invalid coefficients. Not saving results!')
return
if model_name is None:
model_name = self.config.get('model_name_for_coefficients', None)
if model_name is None:
if self.model_name is not None:
model_name = self.model_name
else:
raise ValueError, "model_name unspecified"
out_storage_available = True
if out_storage:
pass
elif 'estimation_database_configuration' in self.config:
try:
db_server = DatabaseServer(self.config['estimation_database_configuration'])
database_name = self.config["estimation_database_configuration"].database_name
if not db_server.has_database(database_name):
db_server.create_database(database_name)
output_db = db_server.get_database(database_name)
out_storage = StorageFactory().get_storage(
type='sql_storage',
storage_location=output_db)
except:
logger.log_warning("Problem with connecting database given by 'estimation_database_configuration'.")
out_storage_available = False
else:
logger.log_warning("No estimation_database_configuration given.")
out_storage_available = False
# the original model name of development_project_lcm is too long as a mysql db table name, truncate it
if model_name.rfind("_development_project_location_choice_model") >=0:
model_name = model_name.replace('_project', '')
specification_table = '%s_specification' % model_name
coefficients_table = '%s_coefficients' % model_name
if out_storage_available:
logger.start_block("Writing specification and coefficients into storage given by 'estimation_database_configuration'")
self.specification.write(out_storage=out_storage, out_table_name=specification_table)
self.coefficients.write(out_storage=out_storage, out_table_name=coefficients_table)
logger.end_block()
logger.start_block("Writing specification and coefficients into %s" % AttributeCache().get_storage_location())
self.specification.write(out_storage=AttributeCache(), out_table_name=specification_table)
self.coefficients.write(out_storage=AttributeCache(), out_table_name=coefficients_table)
logger.end_block()
示例12: RunManagerTests
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
class RunManagerTests(opus_unittest.OpusTestCase):
def setUp(self):
self.database_name = 'test_services_database'
self.config = TestDatabaseConfiguration(database_name = self.database_name)
self.db_server = DatabaseServer(self.config)
def tearDown(self):
self.db_server.drop_database(self.database_name)
self.db_server.close()
def test_setup_run(self):
base_directory = tempfile.mkdtemp(prefix='opus_tmp')
run_name = 'test_scenario_name'
run_manager = RunManager(self.config)
run_manager.setup_new_run(cache_directory = os.path.join(base_directory, run_name),
configuration = {})
resulting_cache_directory = run_manager.get_current_cache_directory()
self.assertTrue(resulting_cache_directory.find(run_name)>-1)
self.assertEquals(os.path.dirname(resulting_cache_directory), base_directory)
self.assertTrue(run_manager.ready_to_run)
self.assertTrue(not os.path.exists(resulting_cache_directory))
run_manager.services_db.close()
os.rmdir(base_directory)
def test_add_row_to_history(self):
run_manager = RunManager(self.config)
cache_directory = tempfile.mkdtemp(prefix='opus_tmp')
resources = {'cache_directory':cache_directory,
'description':'test_run',
'base_year':2000,
'project_name': 'test'}
run_manager.add_row_to_history(run_id = 1,
resources = resources,
status = 'done')
db = self.db_server.get_database(self.database_name)
run_activity_table = db.get_table('run_activity')
s = select([run_activity_table.c.run_description,
run_activity_table.c.status],
whereclause = run_activity_table.c.run_id == 1)
results = db.execute(s).fetchall()
self.assertEqual(len(results), 1)
run_name, status = results[0]
self.assertEqual(status, 'done')
self.assertEqual(run_name, 'test_run')
run_manager.services_db.close()
os.rmdir(cache_directory)
示例13: opusRun
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def opusRun(progressCB, logCB, params):
params_dict = {}
for key, val in params.iteritems():
params_dict[str(key)] = str(val)
database_name = params_dict["database_name"]
opus_data_directory = params_dict["opus_data_directory"]
opus_data_year = params_dict["opus_data_year"]
opus_table_name = params_dict["opus_table_name"]
database_server_connection = params_dict["database_server_connection"]
dbs_config = DatabaseServerConfiguration(database_configuration=database_server_connection)
server = DatabaseServer(database_server_configuration=dbs_config)
opusdb = server.get_database(database_name=database_name)
attribute_cache = AttributeCache(cache_directory=opus_data_directory)
attribute_cache_years = [int(year) for year in os.listdir(opus_data_directory) if year.isdigit() and len(year) == 4]
if opus_data_year != "ALL":
attribute_cache_years = [opus_data_year]
for year in attribute_cache_years:
# input_storage = sql_storage(storage_location = opusdb)
input_storage = attribute_cache.get_flt_storage_for_year(year)
# output_storage = attribute_cache.get_flt_storage_for_year(opus_data_year)
if opus_data_year == "ALL":
opusdb = server.get_database(database_name=database_name + "_" + str(year))
output_storage = sql_storage(storage_location=opusdb)
SimulationState().set_current_time(year)
SessionConfiguration(new_instance=True, package_order=[], in_storage=AttributeCache())
if opus_table_name != "ALL":
opus_table_name_list = re.split(" +", opus_table_name.strip())
else:
opus_table_name_list = input_storage.get_table_names()
for i in opus_table_name_list:
logCB("Exporting %s, %s, %s\n" % (i, year, opus_data_directory))
ExportStorage().export_dataset(dataset_name=i, in_storage=input_storage, out_storage=output_storage)
示例14: __init__
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def __init__(self,
indicator_directory,
name = None,
output_type = None,
storage_location = None,
output_style = ALL,
fixed_field_format = None # Only used with the 'fixed_field' output type
):
if output_type == 'sql' and not isinstance(storage_location, DatabaseConfiguration):
raise Exception("If Table output_type is 'sql', a Database object must be passed as storage_location.")
elif output_type in ['dbf', 'csv', 'tab', 'esri', 'fixed_field'] and \
storage_location is not None and \
not isinstance(storage_location,str):
raise Exception("If Table output_type is %s, storage_location must be a path to the output directory"%output_type)
elif output_type not in ['dbf', 'csv', 'tab', 'sql', 'esri', 'fixed_field']:
raise Exception("Table output_type must be either dbf, csv, tab, sql, esri, fixed_field, not %s"%output_type)
if output_type == "fixed_field" and not fixed_field_format:
raise ValueError("If Table output_type is 'fixed_field', an XML format string must be passed as fixed_field_format.")
self.fixed_field_format = fixed_field_format
if output_style not in [Table.ALL,
Table.PER_YEAR,
Table.PER_ATTRIBUTE]:
raise Exception(('%s output_style is not appropriate.'%output_style,
'Choose from Table.ALL, Table.PER_YEAR, ',
'and Table.PER_ATTRIBUTE'))
self.output_type = output_type
self.output_style = output_style
if storage_location is None:
storage_location = indicator_directory
elif output_type == 'sql':
server = DatabaseServer(database_server_configuration = storage_location)
if not server.has_database(database_name = storage_location.database_name):
server.create_database(database_name = storage_location.database_name)
storage_location = server.get_database(
database_name = storage_location.database_name)
self.storage_location = storage_location
self.output_storage = StorageFactory().get_storage(
type = '%s_storage'%(self.output_type),
storage_location = storage_location
)
self.name = name
self.indicator_directory = indicator_directory
示例15: convert_databases
# 需要导入模块: from opus_core.database_management.database_server import DatabaseServer [as 别名]
# 或者: from opus_core.database_management.database_server.DatabaseServer import get_database [as 别名]
def convert_databases(self, db_config, databases, tables, patterns, backup=True, backup_postfix='_old'):
dbconfig = DatabaseServerConfiguration(
host_name = db_config.host_name,
protocol = 'mysql',
user_name = db_config.user_name,
password = db_config.password
)
db_server = DatabaseServer(dbconfig)
for db_name in databases:
db = db_server.get_database(db_name)
self.convert_database(db, tables[db_name], patterns, backup, backup_postfix)
db.close()
db_server.close()