Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F83165351
base.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Sep 15, 13:19
Size
5 KB
Mime Type
text/x-python
Expires
Tue, Sep 17, 13:19 (1 d, 21 h)
Engine
blob
Format
Raw Data
Handle
20799614
Attached To
R3127 blackdynamite
base.py
View Options
#!/usr/bin/env python
from
__future__
import
print_function
import
os
import
job
import
psycopg2
import
re
import
copy
class
BaseError
(
Exception
):
pass
class
Base
(
object
):
"""
"""
def
getObject
(
self
,
sqlobject
):
curs
=
self
.
connection
.
cursor
()
curs
.
execute
(
"SELECT * FROM {0}.{1} WHERE id = {2}"
.
format
(
self
.
schema
,
sqlobject
.
table_name
,
sqlobject
.
id
))
col_info
=
self
.
getColumnProperties
(
sqlobject
)
line
=
curs
.
fetchone
()
for
i
in
range
(
0
,
len
(
col_info
)):
col_name
=
col_info
[
i
][
0
]
sqlobject
[
col_name
]
=
line
[
i
]
def
getObjectList
(
self
,
sqlobject
):
list_objects
=
[]
col_info
=
self
.
getColumnProperties
(
sqlobject
)
curs
=
self
.
connection
.
cursor
()
curs
.
execute
(
"SELECT * FROM {0}.{1}"
.
format
(
self
.
schema
,
sqlobject
.
table_name
))
for
entries
in
curs
:
# print (col_info)
obj
=
copy
.
deepcopy
(
sqlobject
)
for
i
in
range
(
0
,
len
(
col_info
)):
col_name
=
col_info
[
i
][
0
]
obj
[
col_name
]
=
entries
[
i
]
obj
.
id
=
obj
[
"id"
]
# print (col_name + " " + str(entries[i]))
list_objects
.
append
(
obj
)
# print (list_objects[0].entries)
return
list_objects
def
createSchema
(
self
,):
# create the schema of the simulation
curs
=
self
.
connection
.
cursor
()
curs
.
execute
(
"DROP SCHEMA {0} cascade"
.
format
(
self
.
schema
))
curs
.
execute
(
"CREATE SCHEMA {0}"
.
format
(
self
.
schema
))
def
createTypeCodes
(
self
):
curs
=
self
.
connection
.
cursor
()
curs
.
execute
(
"SELECT typname,oid from pg_type;"
)
self
.
type_code
=
{}
for
i
in
curs
:
# print (i[0])
if
(
i
[
0
]
==
'float8'
):
self
.
type_code
[
i
[
1
]]
=
float
if
(
i
[
0
]
==
'text'
):
self
.
type_code
[
i
[
1
]]
=
str
if
(
i
[
0
]
==
'int8'
):
self
.
type_code
[
i
[
1
]]
=
int
if
(
i
[
0
]
==
'int4'
):
self
.
type_code
[
i
[
1
]]
=
int
if
(
i
[
0
]
==
'bool'
):
self
.
type_code
[
i
[
1
]]
=
bool
self
.
type_code
[
1114
]
=
"TIMESTAMP"
def
createTable
(
self
,
object
):
request
=
object
.
createTableRequest
(
self
.
schema
)
curs
=
self
.
connection
.
cursor
()
print
(
request
)
curs
.
execute
(
request
)
def
update
(
self
,
object
):
request
,
params
=
object
.
update
(
self
.
schema
)
curs
=
self
.
connection
.
cursor
()
print
(
request
)
print
(
params
)
curs
.
execute
(
request
,
params
)
def
createGenericTables
(
self
,):
sql_script_name
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"build_tables.sql"
)
curs
=
self
.
connection
.
cursor
()
# create generic tables
query_list
=
list
()
with
open
(
sql_script_name
,
"r"
)
as
fh
:
for
line
in
fh
:
query_list
.
append
(
re
.
sub
(
"SCHEMAS_IDENTIFIER"
,
self
.
schema
,
line
))
curs
.
execute
(
"
\n
"
.
join
(
query_list
))
def
getColumnProperties
(
self
,
sqlobject
):
curs
=
self
.
connection
.
cursor
()
curs
.
execute
(
"SELECT * FROM {0}.{1} LIMIT 0"
.
format
(
self
.
schema
,
sqlobject
.
table_name
))
column_names
=
[
desc
[
0
]
for
desc
in
curs
.
description
]
column_type
=
[
desc
[
1
]
for
desc
in
curs
.
description
]
return
zip
(
column_names
,
column_type
)
def
setObjectItemTypes
(
self
,
sqlobject
):
col_info
=
self
.
getColumnProperties
(
sqlobject
)
for
i
,
j
in
col_info
:
sqlobject
.
types
[
i
]
=
self
.
type_code
[
j
]
# print (str(i) + " " + str(self.type_code[j]))
def
insert
(
self
,
sqlobject
):
curs
=
self
.
connection
.
cursor
()
request
,
params
=
sqlobject
.
insert
(
self
.
schema
)
curs
.
execute
(
request
,
params
)
sqlobject
.
id
=
curs
.
fetchone
()[
0
]
def
pushQuantity
(
self
,
name
,
type_code
,
description
=
None
):
""" implemented type_codes: "int" "float" "int.vector" "float.vector"
"""
curs
=
self
.
connection
.
cursor
()
if
(
type_code
==
"int"
):
is_integer
=
True
is_vector
=
False
if
(
type_code
==
"int.vector"
):
is_integer
=
True
is_vector
=
True
if
(
type_code
==
"float"
):
is_integer
=
False
is_vector
=
False
if
(
type_code
==
"float.vector"
):
is_integer
=
False
is_vector
=
True
curs
.
execute
(
"INSERT INTO {0}.quantities (name,is_integer,is_vector) VALUES (
%s
,
%s
,
%s
)"
.
format
(
self
.
schema
),(
name
,
is_integer
,
is_vector
)
)
def
__init__
(
self
,
schema
,
connection
):
assert
(
isinstance
(
connection
,
psycopg2
.
_psycopg
.
connection
))
self
.
connection
=
connection
self
.
schema
=
schema
self
.
createTypeCodes
()
if
__name__
==
"__main__"
:
connection
=
psycopg2
.
connect
(
host
=
"localhost"
)
job_description
=
job
.
Job
(
dict
(
hono
=
int
,
lulu
=
float
,
toto
=
str
))
base
=
Base
(
"honoluluSchema"
,
connection
,
job_description
)
base
.
create
()
connection
.
commit
()
base
.
pushJob
(
dict
(
hono
=
12
,
lulu
=
24.2
,
toto
=
"toto"
))
base
.
pushQuantity
(
"ekin"
,
"float"
)
connection
.
commit
()
Event Timeline
Log In to Comment