code: def try_with_session(commit=False):
      """
      Decorator for instance methods on Database that need a sqlalchemy session.
  
      This wrapping function checks if the Database has an active session yet. If
      not, it wraps the function call in a ``with DBSession():`` block.
      """
  
      def wrap(func):
          def call(db, *args, **kwargs):
              # if the Database has an active session, don't create a new one
              if db.session is not None:
                  result = func(db, *args, **kwargs)
                  if commit:
                      db.session.commit()
              else:
                  # otherwise, use the session generator
                  with DBSession(db, commit=commit):
                      result = func(db, *args, **kwargs)
  
              return result
  
          return call
  
      return wrap
  
imports: from __future__ import absolute_import, unicode_literals
  import hashlib
  import json
  import os
  import pickle
  from builtins import object
  from datetime import datetime
  from io import BytesIO
  from operator import attrgetter
  import boto3
  import numpy
  import pandas
  import pymysql
  from sklearn.model_selection import train_test_split
  from sqlalchemy import Column, DateTime, Enum, ForeignKey, Integer, MetaData, Numeric, String, Text, and_, create_engine, func, inspect
  from sqlalchemy.engine.url import URL
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.orm import relationship, sessionmaker
  from sqlalchemy.orm.properties import ColumnProperty
  from atm.constants import BUDGET_TYPES, CLASSIFIER_STATUS, DATARUN_STATUS, METRICS, PARTITION_STATUS, SCORE_TARGETS, ClassifierStatus, PartitionStatus, RunStatus
  from atm.data import load_data
  from atm.utilities import base_64_to_object, object_to_base_64
label_desc: Use image node_id 1 for calling a global function with example usage: try_with_session(commit) and returns: wrap, call, result