本文整理汇总了Python中opus_core.database_management.database_server.DatabaseServer类的典型用法代码示例。如果您正苦于以下问题:Python DatabaseServer类的具体用法?Python DatabaseServer怎么用?Python DatabaseServer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DatabaseServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: opusRun
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,代码行数:26,代码来源:synthesizer_delete_empty_households_from_raw_pums_data.py
示例2: prepare_for_simulation
def prepare_for_simulation(self, run_configuration, cache_directory=None):
self.config = Resources(run_configuration)
self.simulation_state = SimulationState(
new_instance=True, base_cache_dir=cache_directory, start_time=self.config.get("base_year", 0)
)
### TODO: Get rid of this! There is no good reason to be changing the
### Configuration.
if self.config["cache_directory"] is None:
self.config["cache_directory"] = self.simulation_state.get_cache_directory()
SessionConfiguration(
new_instance=True,
package_order=self.config["dataset_pool_configuration"].package_order,
in_storage=AttributeCache(),
)
ForkProcess().fork_new_process(
self.config["creating_baseyear_cache_configuration"].cache_scenario_database, self.config
)
# Create output database (normally done by run manager)
if "estimation_database_configuration" in self.config:
db_server = DatabaseServer(self.config["estimation_database_configuration"])
if not db_server.has_database(self.config["estimation_database_configuration"].database_name):
db_server.create_database(self.config["estimation_database_configuration"].database_name)
示例3: create_building_types_table
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)
示例4: run
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)
示例5: __init__
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)
示例6: run_run
def run_run(self, run_resources, run_name = None, run_as_multiprocess=True, run_in_background=False):
"""check run hasn't already been marked running
log it in to run_activity
run simulation
mark run as done/failed
"""
if not self.ready_to_run:
raise 'RunManager.setup_new_run must be execute before RunManager.run_run'
if run_resources['cache_directory'] != self.current_cache_directory:
raise 'The configuration and the RunManager conflict on the proper cache_directory'
self.add_row_to_history(self.run_id, run_resources, "started", run_name = run_name)
try:
# Test pre-conditions
model_system_class_path = run_resources.get('model_system', None)
if model_system_class_path is None:
raise TypeError, ("The configuration must specify model_system, the"
" full Opus path to the model system to be used.")
# Create baseyear cache
self.create_baseyear_cache(run_resources)
# Create brand-new output database (deletes any prior contents)
if 'estimation_database_configuration' in run_resources:
db_server = DatabaseServer(run_resources['estimation_database_configuration'])
if not db_server.has_database(run_resources['estimation_database_configuration'].database_name):
db_server.create_database(run_resources['estimation_database_configuration'].database_name)
# Run simulation
exec('from %s import ModelSystem' % model_system_class_path)
model_system = ModelSystem()
self.model_system = model_system
if 'base_year' not in run_resources:
run_resources['base_year'] = run_resources['years'][0] - 1
self._create_seed_dictionary(run_resources)
# model_system.run_in_same_process(run_resources)
if run_as_multiprocess:
model_system.run_multiprocess(run_resources)
else:
model_system.run_in_one_process(run_resources, run_in_background=run_in_background, class_path=model_system_class_path)
self.model_system = None
except:
self.add_row_to_history(self.run_id, run_resources, "failed", run_name = run_name)
self.ready_to_run = False
raise # This re-raises the last exception
else:
self.add_row_to_history(self.run_id, run_resources, "done", run_name = run_name)
self.ready_to_run = False
return self.run_id
示例7: get
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,代码行数:9,代码来源:assign_bldgs_to_jobs_when_multiple_bldgs_in_parcel.py
示例8: drop_table
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: AbstractServiceTests
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'))
示例10: save_results
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()
示例11: prepare_for_run
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
示例12: cleanup
def cleanup(self, remove_cache, remove_output_database):
"""Remove all outputs of this simulation."""
self.simulation_state.remove_singleton(delete_cache=remove_cache)
# Remove SessionConfiguration singleton, if it exists
Singleton().remove_singleton_for_class(SessionConfiguration)
cache_dir = self.config['cache_directory']
if os.path.exists(cache_dir):
rmtree(cache_dir)
if remove_output_database and ('estimation_database_configuration' in self.config):
db_server = DatabaseServer(self.config['estimation_database_configuration'])
db_server.drop_database(self.config['estimation_database_configuration'].database_name)
示例13: RunManagerTests
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)
示例14: TestCreateJobBuildingTypesTable
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))
示例15: __init__
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