Re: How to use module in a class

Anthony Tuininga <[email protected]> Fri, 13 Oct 2017 11:20:47 -0600
Newsgroups gmane.comp.python.db.cx-oracle
Message-ID <CAE1XR-5RDvTRusWpRininZrDjBdV17JiXwA8C48YiU196MhSQQ@mail.gmail.com>
--===============1725384850188667658==
Content-Type: multipart/alternative; boundary="94eb2c043aa6c10c04055b70e054"

--94eb2c043aa6c10c04055b70e054
Content-Type: text/plain; charset="UTF-8"

Hi,

For this sort of thing I would definitely suggest subclassing from
cx_Oracle.Connection and cx_Oracle.Cursor. Something like this:

class MyConnection(cx_Oracle.Connection):
    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):
        super(MyConnection, self).__init__(user or self.DEFAULT_DB_USER,
passw or self.DEFAULT_DB_USER_PASSWD, db or self.DEFAULT_DB)
        self.outputtypehandler = self.OutputTypeHandler

    def OutputTypeHandler(cursor, name, defaultType, size, precision,
scale):
        if defaultType == cx_Oralce.NUMBER:
            return cursor.var(decimal.Decimal, arraysize=cursor.arraysize)

    def cursor(self):
        return MyCursor(self)

class MyCursor(cx_Oracle.Cursor):
    pass

A few comments:

(1) I've simplified the output type handler since cx_Oracle knows how to
convert directly to decimal.Decimal

(2) Unless you want to override cursor.execute() or add additional
functions yourself, there probably isn't any need to subclass the cursor

I'm not sure what you mean by your last sentences. If you can explain
further I can try to help.

On Mon, Sep 25, 2017 at 10:10 PM, Andrew Zyman <[email protected]> wrote:

> 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.
>
>
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> cx-oracle-users mailing list
> cx-oracle-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/cx-oracle-users
>
>

--94eb2c043aa6c10c04055b70e054
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<div><br></div><div>For this sort of thing I would defi=
nitely suggest subclassing from cx_Oracle.Connection and cx_Oracle.Cursor. =
Something like this:</div><div><br></div><div>class MyConnection(cx_Oracle.=
<wbr>Connection):</div><div><div>=C2=A0 =C2=A0 DEFAULT_DB =3D &#39;<a href=
=3D"http://127.0.0.1/TT_1122:timesten:direct" target=3D"_blank">127.0.0.1/T=
T_1122:timesten:<wbr>direct</a>&#39;</div><div>=C2=A0 =C2=A0 DEFAULT_DB_USE=
R =3D &quot;username&quot;</div><div>=C2=A0 =C2=A0 DEFAULT_DB_USER_PASSWD =
=3D &quot;password&quot;</div></div><div><br></div><div>=C2=A0 =C2=A0 def _=
_init__(self, db =3D None, user =3D None, passw =3D None):</div><div>=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 super(MyConnection, self).__init__(user or self.DEFAU=
LT_DB_USER, passw or self.DEFAULT_DB_USER_PASSWD, db or self.DEFAULT_DB)</d=
iv><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.outputtypehandler =3D self.OutputT=
ypeHandler</div><div><br></div><div><div>=C2=A0 =C2=A0 def OutputTypeHandle=
r(cursor, name, defaultType, size, precision, scale):</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 if defaultType =3D=3D cx_Oralce.NUMBER:</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return cursor.var(decimal.Decimal, array=
size=3Dcursor.arraysize)</div></div><div><br></div><div>=C2=A0 =C2=A0 def c=
ursor(self):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return MyCursor(self)</d=
iv><div><br></div><div>class MyCursor(cx_Oracle.Cursor):</div><div>=C2=A0 =
=C2=A0 pass</div><div><br></div><div>A few comments:</div><div><br></div><d=
iv>(1) I&#39;ve simplified the output type handler since cx_Oracle knows ho=
w to convert directly to decimal.Decimal</div><div><br></div><div>(2) Unles=
s you want to override cursor.execute() or add additional functions yoursel=
f, there probably isn&#39;t any need to subclass the cursor</div><div><br><=
/div><div>I&#39;m not sure what you mean by your last sentences. If you can=
 explain further I can try to help.</div></div><div class=3D"gmail_extra"><=
br><div class=3D"gmail_quote">On Mon, Sep 25, 2017 at 10:10 PM, Andrew Zyma=
n <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_bla=
nk">[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_q=
uote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1e=
x"><div dir=3D"ltr"><div><div><div><div><div><div><div><div><div>Hello,<br>=
</div>=C2=A0i&#39;m trying to use the module within a designated &quot;Data=
base&quot; class and having all sorts of funny situations.<br></div>=C2=A0I=
&#39;d appreciate some guidance on how to architecture the interaction in a=
 better way.<br><br></div>Task:<br></div>=C2=A0I&#39;d like to separate all=
 database related operations into one class ( let&#39;s call it DB).<br><br=
></div>for example:<br></div><pre style=3D"background-color:rgb(248,248,255=
);color:rgb(0,0,0);font-family:&quot;DejaVu Sans Mono&quot;;font-size:9.8pt=
"><span style=3D"font-weight:bold">import </span>cx_Oracle<br><span style=
=3D"font-weight:bold">import </span>decimal<br><br><span style=3D"font-weig=
ht:bold">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"fon=
t-weight:bold">   </span>DEFAULT_DB <span style=3D"font-weight:bold">=3D </=
span><span style=3D"color:rgb(0,128,128);font-weight:bold">&#39;<a href=3D"=
http://127.0.0.1/TT_1122:timesten:direct" target=3D"_blank">127.0.0.1/TT_11=
22:timesten:<wbr>direct</a>&#39;<br></span><span style=3D"color:rgb(0,128,1=
28);font-weight:bold">   </span>DEFAULT_DB_USER <span style=3D"font-weight:=
bold">=3D </span><span style=3D"color:rgb(0,128,128);font-weight:bold">&quo=
t;username&quot;<br></span><span style=3D"color:rgb(0,128,128);font-weight:=
bold">   </span>DEFAULT_DB_USER_PASSWD <span style=3D"font-weight:bold">=3D=
 </span><span style=3D"color:rgb(0,128,128);font-weight:bold">&quot;passwor=
d&quot;<br></span><span style=3D"color:rgb(0,128,128);font-weight:bold"><br=
></span><span style=3D"color:rgb(0,128,128);font-weight:bold">   </span><sp=
an style=3D"font-weight:bold">def </span><span style=3D"color:rgb(178,0,178=
)">__init__</span>(<span style=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-weight: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:italic">db </span><span style=3D"font-weight:bold=
">is None:<br></span><span style=3D"font-weight:bold">         </span>db <s=
pan style=3D"font-weight:bold">=3D </span><span style=3D"color:rgb(148,85,1=
41)">self</span>.DEFAULT_DB<br>      <span style=3D"font-weight:bold">if </=
span><span style=3D"font-style:italic">user </span><span style=3D"font-weig=
ht:bold">is None:<br></span><span style=3D"font-weight:bold">         </spa=
n>user <span style=3D"font-weight:bold">=3D </span><span style=3D"color:rgb=
(148,85,141)">self</span>.DEFAULT_DB_USER<br>      <span style=3D"font-weig=
ht:bold">if </span><span style=3D"font-style:italic">passw </span><span sty=
le=3D"font-weight:bold">is None:<br></span><span style=3D"font-weight:bold"=
>         </span>passw <span style=3D"font-weight:bold">=3D </span><span st=
yle=3D"color:rgb(148,85,141)">self</span>.DEFAULT_DB_USER_PASSWD<br><br>   =
 <span style=3D"color:rgb(153,153,136);font-style:italic">  </span><span st=
yle=3D"color:rgb(148,85,141)">self</span>.connection <span style=3D"font-we=
ight:bold">=3D cx_Oracle.</span>Connection(<span style=3D"font-style:italic=
">user </span><span style=3D"font-weight:bold">+ </span><span style=3D"colo=
r:rgb(0,128,128);font-weight:bold">&quot;/&quot; </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);fo=
nt-weight:bold">&quot;@&quot; </span><span style=3D"font-weight:bold">+ </s=
pan><span style=3D"color:rgb(148,85,141)">self</span>.DEFAULT_DB)<br>      =
<span style=3D"color:rgb(148,85,141)">self</span>.connection.<wbr>outputtyp=
ehandler <span style=3D"font-weight:bold">=3D </span><span style=3D"color:r=
gb(148,85,141)">self</span>.OutputTypeHandler<br><span style=3D"color:rgb(1=
53,153,136);font-style:italic"><br></span><span style=3D"color:rgb(153,153,=
136);font-style:italic">   </span><span style=3D"font-weight:bold">def </sp=
an><span style=3D"color:rgb(153,0,0);font-weight:bold">OutputTypeHandler</s=
pan>(<span style=3D"color:rgb(148,85,141)">cursor</span>, <span style=3D"fo=
nt-style:italic">name</span>, <span style=3D"font-style:italic">defaultType=
</span>, <span style=3D"font-style:italic">size</span>, <span style=3D"font=
-style:italic">precision</span>, <span style=3D"font-style:italic">scale</s=
pan>)<span style=3D"font-weight:bold">:<br></span><span style=3D"font-weigh=
t:bold">      if </span><span style=3D"font-style:italic">defaultType </spa=
n><span style=3D"font-weight:bold">=3D=3D </span>cx_Oralce.NUMBER<span styl=
e=3D"font-weight:bold">:<br></span><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,153)">size</span><span style=3D"font-weight=
:bold">=3D</span><span style=3D"color:rgb(0,153,153)">200</span>, <span sty=
le=3D"color:rgb(102,0,153)">arraysize</span><span style=3D"font-weight:bold=
">=3D</span>Cursor.arraysize,<br>                           <span style=3D"=
color:rgb(102,0,153)">outconverter</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>(<spa=
n style=3D"color:rgb(148,85,141)">self</span>, <span style=3D"font-style:it=
alic">commit</span><span style=3D"font-weight:bold">=3DTrue</span>)<span st=
yle=3D"font-weight:bold">:<br></span><span style=3D"font-weight:bold">     =
 </span><span style=3D"color:rgb(153,153,136);font-style:italic">&quot;&quo=
t;&quot;Close connection&quot;&quot;&quot;<br></span><span style=3D"color:r=
gb(153,153,136);font-style:italic">      </span><span style=3D"font-weight:=
bold">if </span><span style=3D"font-style:italic">commit</span><span style=
=3D"font-weight:bold">:<br></span><span style=3D"font-weight:bold">        =
 </span><span style=3D"color:rgb(148,85,141)">self</span>.connection.commit=
()<br>      <span style=3D"color:rgb(148,85,141)">self</span>.connection.cl=
ose()<br><br></pre><span style=3D"font-weight:bold">def </span><span style=
=3D"color:rgb(153,0,0);font-weight:bold">checkData</span>(<span style=3D"co=
lor:rgb(148,85,141)">self</span>, <span style=3D"font-style:italic">minutes=
</span>)<span style=3D"font-weight:bold">:</span><br><span style=3D"font-we=
ight:bold"></span><pre style=3D"background-color:rgb(248,248,255);color:rgb=
(0,0,0);font-family:&quot;DejaVu Sans Mono&quot;;font-size:9.8pt"><span sty=
le=3D"font-weight:bold"></span><span style=3D"font-weight:bold">   </span>c=
ursor <span style=3D"font-weight:bold">=3D </span><span style=3D"color:rgb(=
148,85,141)">self</span>.connection.cursor()<br><span style=3D"color:rgb(15=
3,153,136);font-style:italic">   </span>cursor.execute(<span style=3D"color=
:rgb(0,128,128);font-weight:bold">&quot;select sum(reqid) from movaver&quot=
;</span>)<br><span style=3D"color:rgb(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 <sp=
an style=3D"font-weight:bold">is None:<br></span><span style=3D"font-weight=
:bold">      raise </span><span style=3D"color:rgb(0,134,179)">Exception</s=
pan>(<span style=3D"color:rgb(0,128,128);font-weight:bold">&quot;No rows re=
turned during checkData({})&quot;</span>.format(<span style=3D"font-style:i=
talic">minutes</span>)<wbr>)<br>   <span style=3D"font-weight:bold">return =
</span>rows[<span style=3D"color:rgb(0,153,153)">0</span>]</pre><br></div><=
div>There are a few issues with this approach. For instance the &quot;var&q=
uot; and &quot;arraysize&quot; in cursor ( function OutputTypeHandler&quot;=
 are not been resolved as attributes of &quot;cursor&quot;<br><br></div><di=
v>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 ge=
ts resolved as expected.<br><br></div><div><br></div><div>My other attempt =
was to import Connection, Cursor etc from cx_Oracle and define DB as a chil=
d of many parents... This is how i learned that python doesn&#39;t support =
such arrangements.<br><br></div><div>Appreciate your advice.<br></div><div>=
<br><br></div></div></div></div>
<br>------------------------------<wbr>------------------------------<wbr>-=
-----------------<br>
Check out the vibrant tech community on one of the world&#39;s most<br>
engaging tech sites, Slashdot.org! <a href=3D"http://sdm.link/slashdot" rel=
=3D"noreferrer" target=3D"_blank">http://sdm.link/slashdot</a><br>_________=
_____________________<wbr>_________________<br>
cx-oracle-users mailing list<br>
<a href=3D"mailto:cx-oracle-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org">cx-oracle-users@li=
sts.<wbr>sourceforge.net</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/cx-oracle-users" re=
l=3D"noreferrer" target=3D"_blank">https://lists.sourceforge.net/<wbr>lists=
/listinfo/cx-oracle-users</a><br>
<br></blockquote></div><br></div>

--94eb2c043aa6c10c04055b70e054--


--===============1725384850188667658==
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
--===============1725384850188667658==
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

--===============1725384850188667658==--