Skip to content

Instantly share code, notes, and snippets.

@douglasmakey
Created November 27, 2015 03:21
Show Gist options
  • Save douglasmakey/46ff81df66223a618250 to your computer and use it in GitHub Desktop.
Save douglasmakey/46ff81df66223a618250 to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
class ConnectPSQL(object):
_instance = None
_session = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(ConnectPSQL, cls).__new__(cls, *args, **kwargs)
Session = None
try:
#engine = create_engine(options['postgres'], client_encoding='utf8', echo=True)
engine = create_engine('postgresql+psycopg2://dondeviene:pass@localhost/dondeviene')
Session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine))
cls._session = Session
except:
print "Error Connect to database"
exit()
return cls._instance
def get_session(self):
print self._session
class SingletonMetaClass(type):
def __init__(cls,name,bases,dict):
super(SingletonMetaClass,cls)\
.__init__(name,bases,dict)
original_new = cls.__new__
def my_new(cls,*args,**kwds):
if cls.instance == None:
cls.instance = \
original_new(cls,*args,**kwds)
return cls.instance
cls.instance = None
cls.__new__ = staticmethod(my_new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment