Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7911467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:15:41+00:00 2026-06-03T13:15:41+00:00

One functionality of python that I found very handy when working with databases (or

  • 0

One functionality of python that I found very handy when working with databases (or files) are the __enter__ and __exit__ functions you can give to a class. Now by using the with statement you can make sure that in this block __enter__ is first called (and you can open the database or file) and after it’s done __exit__ is called (and you can close a database or file.

I want to open and close a sqlite transaction every time a function from my Database class is called. I can do it at the start and end of every function, but since it has to be done for every function is that class, I was wondering, are there methods that get called before and after each function call? Like SetUp and TearDown in unittesting.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:15:44+00:00Added an answer on June 3, 2026 at 1:15 pm

    You can decorate every member function with a pie decorator, something like

    @transaction
    def insertData(self):
        # code
    

    and transaction is a decorator you define to wrap the function with a pre and post.
    Yes, you have to do it for every function. Here is an example

    def transaction(f):
        def pre():
            print "pre transaction"
        def post():
            print "post transaction"
    
        def wrapped(*args):
            pre()
            f(*args)
            post()
    
        return wrapped
    
    
    class Foo(object):
        def __init__(self):
            print "instantiating"
    
        def doFoo(self):
            print "doing foo"
    
        @transaction
        def doBar(self, value):
            print "doing bar "+str(value)
    
    @transaction
    def foofunc():
        print "hello"
    
    foofunc()
    
    f=Foo()
    f.doFoo()
    f.doBar(5)
    

    .

    stefanos-imac:python borini$ python decorator.py 
    pre transaction
    hello
    post transaction
    instantiating
    doing foo
    pre transaction
    doing bar 5
    post transaction
    

    The alternative is that you use a metaclass, like this:

    import types
    
    
    class DecoratedMetaClass(type):
        def __new__(meta, classname, bases, classDict):
            def pre():
                print "pre transaction"
            def post():
                print "post transaction"
            newClassDict={}
            for attributeName, attribute in classDict.items():
                if type(attribute) == types.FunctionType:
                    def wrapFunc(f):
                        def wrapper(*args):
                            pre()
                            f(*args)
                            post()
                        return wrapper
                    newAttribute = wrapFunc(attribute)
                else:
                    newAttribute = attribute
                newClassDict[attributeName] = newAttribute
            return type.__new__(meta, classname, bases, newClassDict)
    
    
    
    class MyClass(object):
    
        __metaclass__ = DecoratedMetaClass
    
        def __init__(self):
            print "init"
        def doBar(self, value):
            print "doing bar "+str(value)
        def doFoo(self):
            print "doing foo"
    
    
    
    c = MyClass()
    c.doFoo()
    c.doBar(4)
    

    This is pure black magic, but it works

    stefanos-imac:python borini$ python metaclass.py
    pre transaction
    init
    post transaction
    pre transaction
    doing foo
    post transaction
    pre transaction
    doing bar 4
    post transaction
    

    You normally don’t want to decorate the __init__, and you may want to decorate only those methods with a special name, so you may want to replace

            for attributeName, attribute in classDict.items():
                if type(attribute) == types.FunctionType:
    

    with something like

            for attributeName, attribute in classDict.items():
                if type(attribute) == types.FunctionType and "trans_" in attributeName[0:6]:
    

    This way, only methods called trans_whatever will be transactioned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm somewhat new to C++, coming from python. One piece of functionality I really
How to use such functionality of windows-7 with WPF (one that uses Itunes -
I am working on one project where there is a functionality need to implement
I am planning to implement C++-like constructor/destructor functionality to one of my Python classes
I'm working on a project that involves uploading an image to tumblr from Python.
The Zen of Python states that there should only be one way to do
Is there a library for C# that allows similar functionality to python's struct from
I am used to that Python allows some neat tricks to delegate functionality to
I'm working on a Python library that interfaces with a web service API. Like
A number of scripting languages (Python/PHP/etc...) include functionality (sometimes through extensions) that allows you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.