How to use module in a class
Andrew Zyman <[email protected]> Tue, 26 Sep 2017 00:10:14 -0400
| Newsgroups | gmane.comp.python.db.cx-oracle |
|---|---|
| Message-ID | <CAPrcKX+0UVNEHDzfgm=33Wzg=XXDB_hF-T_1nRb7BTVECky9hA@mail.gmail.com> |
--===============4271366889384271346==
Content-Type: multipart/alternative; boundary="f403043e9de0956711055a0fdce4"
--f403043e9de0956711055a0fdce4
Content-Type: text/plain; charset="UTF-8"
Hello,
i'm trying to use the module within a designated "Database" class and
having all sorts of funny situations.
I'd appreciate some guidance on how to architecture the interaction in a
better way.
Task:
I'd like to separate all database related operations into one class (
let's call it DB).
for example:
import cx_Oracle
import decimal
class DB():
DEFAULT_DB = '127.0.0.1/TT_1122:timesten:direct'
DEFAULT_DB_USER = "username"
DEFAULT_DB_USER_PASSWD = "password"
def __init__(self, db = None, user = None, passw = None):
if db is None:
db = self.DEFAULT_DB
if user is None:
user = self.DEFAULT_DB_USER
if passw is None:
passw = self.DEFAULT_DB_USER_PASSWD
self.connection = cx_Oracle.Connection(user + "/" + passw + "@"
+ self.DEFAULT_DB)
self.connection.outputtypehandler = self.OutputTypeHandler
def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
if defaultType == cx_Oralce.NUMBER:
return cursor.var(str, size=200, arraysize=Cursor.arraysize,
outconverter=decimal.Decimal)
def close(self, commit=True):
"""Close connection"""
if commit:
self.connection.commit()
self.connection.close()
def checkData(self, minutes):
cursor = self.connection.cursor()
cursor.execute("select sum(reqid) from movaver")
rows = cursor.fetchone()
cursor.close()
if rows is None:
raise Exception("No rows returned during checkData({})".format(minutes))
return rows[0]
There are a few issues with this approach. For instance the "var" and
"arraysize" in cursor ( function OutputTypeHandler" are not been resolved
as attributes of "cursor"
though, if i do not try to wrap this functionality in the class and just
simply import the cx_Oracle and start writing the functions - everything
gets resolved as expected.
My other attempt was to import Connection, Cursor etc from cx_Oracle and
define DB as a child of many parents... This is how i learned that python
doesn't support such arrangements.
Appreciate your advice.
--f403043e9de0956711055a0fdce4
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><div><div><div><div><div><div><div><div>Hello,<br></d=
iv>=C2=A0i'm trying to use the module within a designated "Databas=
e" class and having all sorts of funny situations.<br></div>=C2=A0I=
9;d appreciate some guidance on how to architecture the interaction in a be=
tter way.<br><br></div>Task:<br></div>=C2=A0I'd like to separate all da=
tabase related operations into one class ( let's call it DB).<br><br></=
div>for example:<br></div><pre style=3D"background-color:rgb(248,248,255);c=
olor:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:9.8pt"><=
span style=3D"font-weight:bold">import </span>cx_Oracle<br><span style=3D"f=
ont-weight:bold">import </span>decimal<br><br><span style=3D"font-weight:bo=
ld">class </span><span style=3D"color:rgb(68,85,136);font-weight:bold">DB</=
span>()<span style=3D"font-weight:bold">:<br></span><span style=3D"font-wei=
ght:bold"> </span>DEFAULT_DB <span style=3D"font-weight:bold">=3D </span>=
<span style=3D"color:rgb(0,128,128);font-weight:bold">'<a href=3D"http:=
//127.0.0.1/TT_1122:timesten:direct">127.0.0.1/TT_1122:timesten:direct</a>&=
#39;<br></span><span style=3D"color:rgb(0,128,128);font-weight:bold"> </s=
pan>DEFAULT_DB_USER <span style=3D"font-weight:bold">=3D </span><span style=
=3D"color:rgb(0,128,128);font-weight:bold">"username"<br></span><=
span style=3D"color:rgb(0,128,128);font-weight:bold"> </span>DEFAULT_DB_U=
SER_PASSWD <span style=3D"font-weight:bold">=3D </span><span style=3D"color=
:rgb(0,128,128);font-weight:bold">"password"<br></span><span styl=
e=3D"color:rgb(0,128,128);font-weight:bold"><br></span><span style=3D"color=
:rgb(0,128,128);font-weight:bold"> </span><span style=3D"font-weight:bold=
">def </span><span style=3D"color:rgb(178,0,178)">__init__</span>(<span sty=
le=3D"color:rgb(148,85,141)">self</span>, <span style=3D"font-style:italic"=
>db </span><span style=3D"font-weight:bold">=3D None</span>, <span style=3D=
"font-style:italic">user </span><span style=3D"font-weight:bold">=3D None</=
span>, <span style=3D"font-style:italic">passw </span><span style=3D"font-w=
eight:bold">=3D None</span>)<span style=3D"font-weight:bold">:<br></span> =
<span style=3D"font-weight:bold">if </span><span style=3D"font-style:it=
alic">db </span><span style=3D"font-weight:bold">is None:<br></span><span s=
tyle=3D"font-weight:bold"> </span>db <span style=3D"font-weight:bol=
d">=3D </span><span style=3D"color:rgb(148,85,141)">self</span>.DEFAULT_DB<=
br> <span style=3D"font-weight:bold">if </span><span style=3D"font-sty=
le:italic">user </span><span style=3D"font-weight:bold">is None:<br></span>=
<span style=3D"font-weight:bold"> </span>user <span style=3D"font-w=
eight:bold">=3D </span><span style=3D"color:rgb(148,85,141)">self</span>.DE=
FAULT_DB_USER<br> <span style=3D"font-weight:bold">if </span><span sty=
le=3D"font-style:italic">passw </span><span style=3D"font-weight:bold">is N=
one:<br></span><span style=3D"font-weight:bold"> </span>passw <span=
style=3D"font-weight:bold">=3D </span><span style=3D"color:rgb(148,85,141)=
">self</span>.DEFAULT_DB_USER_PASSWD<br><br> <span style=3D"color:rgb(15=
3,153,136);font-style:italic"> </span><span style=3D"color:rgb(148,85,141)=
">self</span>.connection <span style=3D"font-weight:bold">=3D cx_Oracle.</s=
pan>Connection(<span style=3D"font-style:italic">user </span><span style=3D=
"font-weight:bold">+ </span><span style=3D"color:rgb(0,128,128);font-weight=
:bold">"/" </span><span style=3D"font-weight:bold">+ </span><span=
style=3D"font-style:italic">passw </span><span style=3D"font-weight:bold">=
+ </span><span style=3D"color:rgb(0,128,128);font-weight:bold">"@"=
; </span><span style=3D"font-weight:bold">+ </span><span style=3D"color:rgb=
(148,85,141)">self</span>.DEFAULT_DB)<br> <span style=3D"color:rgb(148=
,85,141)">self</span>.connection.outputtypehandler <span style=3D"font-weig=
ht:bold">=3D </span><span style=3D"color:rgb(148,85,141)">self</span>.Outpu=
tTypeHandler<br><span style=3D"color:rgb(153,153,136);font-style:italic"><b=
r></span><span style=3D"color:rgb(153,153,136);font-style:italic"> </span=
><span style=3D"font-weight:bold">def </span><span style=3D"color:rgb(153,0=
,0);font-weight:bold">OutputTypeHandler</span>(<span style=3D"color:rgb(148=
,85,141)">cursor</span>, <span style=3D"font-style:italic">name</span>, <sp=
an style=3D"font-style:italic">defaultType</span>, <span style=3D"font-styl=
e:italic">size</span>, <span style=3D"font-style:italic">precision</span>, =
<span style=3D"font-style:italic">scale</span>)<span style=3D"font-weight:b=
old">:<br></span><span style=3D"font-weight:bold"> if </span><span sty=
le=3D"font-style:italic">defaultType </span><span style=3D"font-weight:bold=
">=3D=3D </span>cx_Oralce.NUMBER<span style=3D"font-weight:bold">:<br></spa=
n><span style=3D"font-weight:bold"> return c</span>ursor.var(<span =
style=3D"color:rgb(0,134,179)">str</span>, <span style=3D"color:rgb(102,0,1=
53)">size</span><span style=3D"font-weight:bold">=3D</span><span style=3D"c=
olor:rgb(0,153,153)">200</span>, <span style=3D"color:rgb(102,0,153)">array=
size</span><span style=3D"font-weight:bold">=3D</span>Cursor.arraysize,<br>=
<span style=3D"color:rgb(102,0,153)">outconverte=
r</span><span style=3D"font-weight:bold">=3D</span>decimal.Decimal)<br><br>=
<span style=3D"font-weight:bold">def </span><span style=3D"color:rgb(153=
,0,0);font-weight:bold">close</span>(<span style=3D"color:rgb(148,85,141)">=
self</span>, <span style=3D"font-style:italic">commit</span><span style=3D"=
font-weight:bold">=3DTrue</span>)<span style=3D"font-weight:bold">:<br></sp=
an><span style=3D"font-weight:bold"> </span><span style=3D"color:rgb(1=
53,153,136);font-style:italic">"""Close connection"&quo=
t;"<br></span><span style=3D"color:rgb(153,153,136);font-style:italic"=
> </span><span style=3D"font-weight:bold">if </span><span style=3D"fon=
t-style:italic">commit</span><span style=3D"font-weight:bold">:<br></span><=
span style=3D"font-weight:bold"> </span><span style=3D"color:rgb(14=
8,85,141)">self</span>.connection.commit()<br> <span style=3D"color:rg=
b(148,85,141)">self</span>.connection.close()<br><br></pre><span style=3D"f=
ont-weight:bold">def </span><span style=3D"color:rgb(153,0,0);font-weight:b=
old">checkData</span>(<span style=3D"color:rgb(148,85,141)">self</span>, <s=
pan style=3D"font-style:italic">minutes</span>)<span style=3D"font-weight:b=
old">:</span><br><span style=3D"font-weight:bold"></span><pre style=3D"back=
ground-color:rgb(248,248,255);color:rgb(0,0,0);font-family:"DejaVu San=
s Mono";font-size:9.8pt"><span style=3D"font-weight:bold"></span><span=
style=3D"font-weight:bold"> </span>cursor <span style=3D"font-weight:bol=
d">=3D </span><span style=3D"color:rgb(148,85,141)">self</span>.connection.=
cursor()<br><span style=3D"color:rgb(153,153,136);font-style:italic"> </s=
pan>cursor.execute(<span style=3D"color:rgb(0,128,128);font-weight:bold">&q=
uot;select sum(reqid) from movaver"</span>)<br><span style=3D"color:rg=
b(153,153,136);font-style:italic"> </span>rows <span style=3D"font-weight=
:bold">=3D </span>cursor.fetchone()<br> cursor.close()<br> <span style=
=3D"font-weight:bold">if </span>rows <span style=3D"font-weight:bold">is No=
ne:<br></span><span style=3D"font-weight:bold"> raise </span><span sty=
le=3D"color:rgb(0,134,179)">Exception</span>(<span style=3D"color:rgb(0,128=
,128);font-weight:bold">"No rows returned during checkData({})"</=
span>.format(<span style=3D"font-style:italic">minutes</span>))<br> <span=
style=3D"font-weight:bold">return </span>rows[<span style=3D"color:rgb(0,1=
53,153)">0</span>]</pre><br></div><div>There are a few issues with this app=
roach. For instance the "var" and "arraysize" in cursor=
( function OutputTypeHandler" are not been resolved as attributes of =
"cursor"<br><br></div><div>though, if i do not try to wrap this f=
unctionality in the class and just simply import the cx_Oracle and start wr=
iting the functions - everything gets resolved as expected.<br><br></div><d=
iv><br></div><div>My other attempt was to import Connection, Cursor etc fro=
m cx_Oracle and define DB as a child of many parents... This is how i learn=
ed that python doesn't support such arrangements.<br><br></div><div>App=
reciate your advice.<br></div><div><br><br></div></div></div></div>
--f403043e9de0956711055a0fdce4--
--===============4271366889384271346==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
--===============4271366889384271346==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
cx-oracle-users mailing list
cx-oracle-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/cx-oracle-users
--===============4271366889384271346==--