How can I use pandas.read_sql on an async connection? - pandas

I am trying to do the asynchron equivalent of
engine = create_engine('sqlite:///./test.db')
stmt = session.query(MyTable)
data = pd.read_sql(stmt, engine)
but it fails with the error AttributeError: 'AsyncConnection' object has no attribute 'cursor'.
What would be the right way to make this work?
asyn_engine = create_async_engine('sqlite+aiosqlite:///./test.db')
stmt = select(MyTable)
data = pd.read_sql(stmt, async_engine)

This code in principal is working...
# Making pd.read_sql connection the first argument to make it compatible
# with conn.run_syn()
def _read_sql(con, stmt):
return pd.read_sql(stmt, con)
async def get_df(stmt, engine):
async with engine.begin() as conn:
data = await conn.run_sync(_read_sql, stmt)
return data
asyn_engine = create_async_engine('sqlite+aiosqlite:///./test.db')
stmt = select(MyTable)
data = get_df(stmt, asyn_engine )

Related

Getting error in a python script when using QuickSight API calls to retrieve the value of user parameter selection

I am working on a python script which will use QS APIs to retrieve the user parameter selections but keep getting the below error:
parameters = response['Dashboard']['Parameters'] KeyError: 'Parameters'
If I try a different code to retrieve the datasets in my QS account, it works but the Parameters code doesn't. I think I am missing some configuration.
#Code to retrieve the parameters from a QS dashboard (which fails):
import boto3
quicksight = boto3.client('quicksight')
response = quicksight.describe_dashboard(
AwsAccountId='99999999999',
DashboardId='zzz-zzzz-zzzz'
)
parameters = response['Dashboard']['Parameters']
for parameter in parameters:
print(parameter['Name'], ':', parameter['Value'])
#Code to display the datasets in the QS account (which works):
import boto3
import json
account_id = '99999999999'
session = boto3.Session(profile_name='default')
qs_client = session.client('quicksight')
response = qs_client.list_data_sets(AwsAccountId = account_id,MaxResults = 100)
results = response['DataSetSummaries']
while "NextToken" in response.keys():
response = qs_client.list_data_sets(AwsAccountId = account_id,MaxResults = 100,NextToken=response["NextToken"])
results.extend(response["DataSetSummaries"])
for i in results:
x = i['DataSetId']
try:
response = qs_client.describe_data_set(AwsAccountId=account_id,DataSetId=x)
print("succeeded loading: {} for data set {} ".format(x, response['DataSet']['Name']))
except:
print("failed loading: {} ".format(x))

Telethon "DestroySessionRequest" method not work

I try destoy every new session.
I run main script, save all exist session to variable
all_session = await client(functions.account.GetAuthorizationsRequest())
hash_list = [x.hash for x in all_session.authorizations]
after i run and search for new session
if i got new session - i try destroy it
while True:
await asyncio.sleep(15)
all_new = await client(functions.account.GetAuthorizationsRequest())
new_hashes = [x.hash for x in all_new.authorizations]
for h in new_hashes:
if h not in hash_list:
logger.debug(f"have a new hash {h}")
try:
result = await client(functions.DestroySessionRequest(
session_id=-h
))
logger.debug(f"DROP RESULT IS: {result.stringify()}")
except Exception as e:
logger.debug(
f"error - {e}")
Every time then i try i got as response
DestroySessionNone(
session_id=3806172788422661047
)
i also tried use this method without "-", same result
try:
result = await client(functions.DestroySessionRequest(
session_id=-h
))
logger.debug(f"DROP RESULT IS: {result.stringify()}")
except Exception as e:
logger.debug(
f"error - {e}")

How to perform a SQL query with SQLAlchemy to later pass it into a pandas dataframe

I have followed this article set up, it all seems to work properly, but I would like now to perform a SQL query and pass the result into a pandas data frame, how could I proceed?
This is what I have now;
host_server = os.environ.get('host_server', 'localhost')
db_server_port = urllib.parse.quote_plus(str(os.environ.get('db_server_port', '5432')))
database_name = os.environ.get('database_name', 'my_data_base123')
db_username = urllib.parse.quote_plus(str(os.environ.get('db_username', 'my_user_name123')))
db_password = urllib.parse.quote_plus(str(os.environ.get('db_password', 'my_password123')))
ssl_mode = urllib.parse.quote_plus(str(os.environ.get('ssl_mode','prefer')))
DATABASE_URL = 'postgresql://{}:{}#{}:{}/{}?sslmode={}'.format(db_username, db_password, host_server, db_server_port, database_name, ssl_mode)
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
engine = sqlalchemy.create_engine(
DATABASE_URL, pool_size=3, max_overflow=0
)
metadata.create_all(database)
app = FastAPI(title="REST API using FastAPI PostgreSQL Async EndPoints")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
#app.on_event("startup")
async def startup():
await database.connect()
#app.on_event("shutdown")
async def shutdown():
await database.disconnect()
Trying to access the database with a SQL query:
with engine.connect() as conn:
result = conn.execute(text("select 'hello world'))
print(result.all())
as it says in the sqlalchemy documentation , but i get some errors, like:
print(result.all())
AttributeError: 'ResultProxy' object has no attribute 'all'
even if i try to access the tables of my database
with engine.connect() as conn:
result = conn.execute(text("select * FROM users"))
print(result.all())
i get the same error
It is solved, I had to upgrade SQLAlchemy
sudo pip install sqlalchemy --upgrade

how to receive information in kotlin from a server in python (socketserver)

server
I have tried almost everything to receive text from python
I don't know where the problem comes from from the client or from the server
try:
llamadacod = self.request.recv(1024)
llamada = self.decode(llamadacod)
print(f"{color.A}{llamada}")
time.sleep(0.1)
if llamada == "conectado":
msg = "Hello"
msgcod = self.encode(msg)
print(f"{color.G}{msg}")
self.request.send(msgcod)
client
val thread = Thread(Runnable {
try{
val client = Socket("localHost",25565)
client.setReceiveBufferSize(1024)
client.outputStream.write("conectado".toByteArray())
val text = InputStreamReader(client.getInputStream())
recibir = text.toString()
client.outputStream.write("Client_desconect".toByteArray())
client.close()
I already solved it, the solution was very simple, you just had to ensure that both the server and the client would occupy the same way of communicating
client :
val input = DataInputStream(client.getInputStream())
id = input.readUTF()
server:
self.request.send(len(msg).to_bytes(2, byteorder='big'))
self.request.send(msg)

in Grails can you use both sql.newInstance and Domain class calls in the same method?

Can you have code that looks like this?
def methodname ()
{
proc = "sql to call a stored proc"
def conn = Sql.newInstance(DB_CONN, DB_USERNAME, DB_PASSWORD, "org.postgresql.Driver")
def result1 = conn.rows(proc)
def result2 = MyClass.Find("from MyClass where foo='bar'")
return [result1, result2]
}
If so are they using different connections? Is there any way to get the call to the stored proc to use the same connection as the MyClass call?
They will use different connections. Sql will grab a new one and the GORM call will use one from the connection pool managed by the DataSource. But you can get Sql to use the same connection as the GORM call by using sessionFactory.currentSession.connection():
class MyControllerOrMyService {
def sessionFactory
def methodname() {
proc = "sql to call a stored proc"
def conn = new Sql(sessionFactory.currentSession.connection())
def result1 = conn.rows(proc)
def result2 = MyClass.Find("from MyClass where foo='bar'")
return [result1, result2]
}
}
They will be different connections. This is a standard issue with Grails in that you can only have one DataSource.groovy and so only one linked to GORM. There are ways around this but it is mostly a matter of what you need at the time.
If you wanted to connect to a entirely different DataSource you could set up something like this in your Config.groovy (not perfect but effective enough).
environments {
production {
grails.serverURL = "http://localhost:8080/${appName}"
grails.databaseDriverClassName = "oracle.jdbc.driver.OracleDriver"
grails.databaseURL = "jdbc:oracle:thin:#<servername>:1521:<sid>"
grails.databaseUsername = "<username>"
grails.databasePassword = "<password>"
}
development {
grails.serverURL = "http://localhost:8080/${appName}"
grails.databaseDriverClassName = "oracle.jdbc.driver.OracleDriver"
grails.databaseURL = "jdbc:oracle:thin:#<servername>:1521:<sid>"
grails.databaseUsername = "<username>"
grails.databasePassword = "<password>"
}
test {
grails.serverURL = "http://localhost:8080/${appName}"
grails.databaseDriverClassName = "oracle.jdbc.driver.OracleDriver"
grails.databaseURL = "jdbc:oracle:thin:#<servername>:1521:<sid>"
grails.databaseUsername = "<username>"
grails.databasePassword = "<password>"
}
}
Then in your service simply call it like so:
import org.codehaus.groovy.grails.commons.*
class SomeService {
boolean transactional = true
def config = ConfigurationHolder.config
// set up the Sql object
def sql = groovy.sql.Sql.newInstance(
config.grails.databaseURL,
config.grails.databaseUsername,
config.grails.databasePassword,
config.grails.databaseDriverClassName)
See http://jira.codehaus.org/browse/GRAILS-5454 .