python排列*号
SimpleHttpServer出现unicodeDecodeError

with让数据库操作代码变得清爽

妙音 posted @ 2013年12月17日 21:03 in python , 2275 阅读

对数据操通常都需要打开与关闭连接,而且用try-finally保证连接被关闭.

这些常规的操作会让代码变得难看.

db = web.database(dbn='oracle', db='......', user='hy', pw='1234')
try:
    db.query("select * from t_c_ms_interface")
finally:
    db.ctx.db.close()

用上下文协议隐藏这些操作

class IPCCCDB:
    def __init__(self):
        self.dns = '....'

    def __enter__(self):
        import web
        self.db = web.database(dbn='oracle', db=self.dns, user='hy', pw='1234')
        return self.db

    def __exit__(self, type, value, traceback):
        self.db.ctx.db.close()

然后,变得如此清爽

with IPCCCDB() as db:
    db.query("select * from t_c_ms_interface")
Avatar_small
LEGEND SEO 说:
2020年12月23日 16:47

Thanks for providing recent updates regarding the concern, I look forward to read more. Casino Online Canada

Avatar_small
LEGEND SEO 说:
2020年12月24日 20:15

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. kaszinó online

Avatar_small
buddah bear 说:
2020年12月29日 02:09

Our products are of the highest quality and are packed in discrete odor less vacuum sealed packages. Discreet delivery is our recommended best practice and packages are tracked all the way to your home.

<a href="https://exoticbudshop.org/" rel="dofollow">Buddha bear carts</a>
<a href="https://exoticbudshop.org/product/buy-buddha-bear-carts/" rel="dofollow">Buddha bear carts</a> at prices that just can not be beaten.
Following the footsteps of plenty of other countries and states that have realized the health benefits of medicating with cannabis. We believe patients should have access to the best quality.
<a href="https://exoticbudshop.org/product/buy-buddha-bear-carts/" rel="dofollow">buddha bear carts for sale</a> Free Shipping on orders above $200
<a href="https://exoticbudshop.org/product/buy-buddha-bear-carts/" rel="dofollow">Buddah bear carts</a>
<a href="https://exoticbudshop.org/product/buy-buddha-bear-carts/" rel="dofollow">buddha bear carts</a>
<a href="https://exoticbudshop.org/product/buy-krt-carts-online/" rel="dofollow">KRT carts</a>
<a href="https://exoticbudshop.org/product/buy-krt-carts-online/" rel="dofollow">Buy KRT carts</a> Buy quality THC VAPE AT EXOTICBUDSHOP and have it deliver to you discretely
<a href="https://exoticbudshop.org/product/buy-biscotti-boyz-tins-weed/" rel="dofollow">biscottiboyz</a> > Discreet delivery is our recommended best practice and packages are tracked all the way to your home.
<a href="https://exoticbudshop.org/product/buy-lucky-carts-online/" rel="dofollow">lucky carts</a> Our products are of great quality
<a href="https://exoticbudshop.org/product/buy-lucky-carts-online/" rel="dofollow">buy lucky carts</a>
<a href="https://exoticbudshop.org/product/muha-meds-carts-for-sale/" rel="dofollow">muhameds carts</a> Our products are of great quality
<a href="https://exoticbudshop.org/product/muha-meds-carts-for-sale/" rel="dofollow">buy muhameds carts</a>
<a href="https://exoticbudshop.org/product/muha-meds-carts-for-sale/" rel="dofollow">buy muhameds carts online</a> Our products are of great quality

Avatar_small
jonny 说:
2021年1月08日 20:56

I am glad to locate your recognized method for composing the post. Presently you make it simple for me to comprehend and execute the idea. Much obliged to you for the post. James River Capital

Avatar_small
corteiz tracksuit 说:
2023年8月15日 14:11

Absolutely loved reading this blog post! The insights you've shared are truly eye-opening and thought-provoking. Your writing style is engaging and easy to follow, making complex concepts feel accessible to readers like me.

Avatar_small
corteiz tracksuit 说:
2023年8月15日 14:11

Absolutely loved reading this blog post! The insights you've shared are truly eye-opening and thought-provoking. Your writing style is engaging and easy to follow, making complex concepts feel accessible to readers like me.

Avatar_small
corteiz tracksuit 说:
2023年8月15日 14:12

Absolutely loved reading this blog post! The insights you've shared are truly eye-opening and thought-provoking. Your writing style is engaging and easy to follow, making complex concepts feel accessible to readers like me.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter