0

I am instantiating this object below every time I call csv in my function. Was just wondering if there's anyway I could just instantiate the object just once? I tried to split the return csv from def csv() to another function but failed.

Code instantiating the object

def csv():

    proj = Project.Project(db_name='test', json_file="/home/qingyong/workspace/Project/src/json_files/sys_setup.json")#, _id='poc_1'
    csv = CSVDatasource(proj, "/home/qingyong/workspace/Project/src/json_files/data_setup.json")
    return csv

Test function

def test_df(csv,df)

..............

4
  • 1
    csvObj = csv() # since you're returning csv object in the function csv() Commented Jun 8, 2015 at 9:12
  • How should i change my code? Advise? Commented Jun 8, 2015 at 9:15
  • Your "code to call function" is not calling the csv function, it is passing the csv function into test_df, you don't show it being called. Maybe part of your confusion is that you have a name csv used inside the function of the same name - a bad idea. Commented Jun 8, 2015 at 9:26
  • I am missing another function to return the objects. What i want is like instantiate the objects once, have a function to return the objects and other test functions can get from it Commented Jun 8, 2015 at 9:33

4 Answers 4

1

Is your csv function actually a pytest.fixture? If so, you can change its scope to session so it will only be called once per py.test session.

@pytest.fixture(scope="session")
def csv():
    # rest of code

Of course, the returned data should be immutable so tests can't affect each other.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a global variable to cache the object:

_csv = None

def csv():
    global _csv
    if _csv is None:
        proj = Project.Project(db_name='test', json_file="/home/qingyong/workspace/Project/src/json_files/sys_setup.json")#, _id='poc_1'
        _csv = CSVDatasource(proj, "/home/qingyong/workspace/Project/src/json_files/data_setup.json")
    return _csv

Another option is to change the caller to cache the result of csv() in a manner similar to the snippet above.

Note that your "code to call the function" doesn't call the function, it only declares another function that apparently receives the csv function's return value. You didn't show the call that actually calls the function.

3 Comments

it's a bad idea to use global right? Maybe I am missing another function to return the objects. What i want is like instantiate the objects once, have a function to return the objects and other test functions can get from it
@QingYong It's a bad idea to use a global as part of the public interface, or to pass data between module boundaries. Neither is the case here. Caching the return value of a function without exposing it in the interface is the correct (and necessary) use of global variables. This implementation "instantiates the object once", as requested.
@downvoter It is not obvious what is wrong with the response, please elaborate.
0

You can use a decorator for this if CSVDatasource doesn't have side effects like reading the input line by line.

See Efficient way of having a function only execute once in a loop

Comments

0

You can store the object in the function's local dictionary. And return that object if it exists, create a new one if it doesn't.

def csv():
    if not hasattr(csv, 'obj'):
        proj = Project.Project(db_name='test', json_file="/home/qingyong/workspace/Project/src/json_files/sys_setup.json")#, _id='poc_1'
        csv.obj = CSVDatasource(proj, "/home/qingyong/workspace/Project/src/json_files/data_setup.json")
    return csv.obj

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.