Airflow: Using MySqlHook to get connection - python-3.x

I'm trying to get a connection object while using the MySqlHook. Assume I saved a mysql connection in the webserver admin called test_connection. What I've done:
mysql_hook = MySqlHook(conn_name_attr = 'test_connection')
conn = mysql_hook.get_conn()
Gives me an error: tuple' object has no attribute 'get_conn'
Any help would be very appreciated!

I am not sure where that code example comes from, especially the parameter conn_name_attr. It seems that the parameter is wrong.
After looking into the models and the hook itself, it seems to be
MySqlHook(mysql_conn_id='test_connection')
Also, if you get back a tuple try printing it since there might be an error message or other helpful information inside it.

Related

How to use pystemd to control systemd timedated ntp service?

I'm working on a python app that needs to get the NTPSynchronized parameter from system-timedated. I'd also like to be able to start and stop the NTP service by using the SetNTP method.
To communicate with timedated over d-bus I have been using this as reference: https://www.freedesktop.org/wiki/Software/systemd/timedated/
I previously got this working with dbus-python, but have since learned that this library has been deprecated. I tried the dbus_next package, but that does not have support for Python 3.5, which I need.
I came across the pystemd package, but I am unsure if this can be used to do what I want. The only documentation I have been able to find is this example (https://github.com/facebookincubator/pystemd), but I can not figure out how to use this to work with system-timedated.
Here is the code I have that works with dbus-python:
import dbus
BUS_NAME = 'org.freedesktop.timedate1`
IFACE = 'org.freedesktop.timedate1`
bus = dbus.SystemBus()
timedate_obj = bus.get_object(BUS_NAME, '/org/freedesktop/timedate1')
# Get synchronization value
is_sync = timedate_obj.Get(BUS_NAME, 'NTPSynchronized', dbus_interface=dbus.PROPERTIES_IFACE)
# Turn off NTP
timedate_obj.SetNTP(False,False, dbus_interface=IFACE)
Here's what I have so far with pystemd, but I don't think I'm accessing it in the right way:
from pystemd.systemd1 import Unit
unit = Unit(b'systemd-timesyncd.service')
unit.load()
# Try to access properties
prop = unit.Properties
prop.NTPSynchronized
Running that I get:
Attribute Error: 'SDInterface' object has no attribute 'NTPSynchronized'
I have a feeling that either the service I entered is wrong, or the way I'm accessing properties is wrong, or even both are wrong.
Any help or advice is appreciated.
Looking at the source code, it appears that using the pystemd.systemd1 Unit object has a default destination of "org.freedesktop.systemd1" + the service name (https://github.com/facebookincubator/pystemd/blob/master/pystemd/systemd1/unit.py)
This is not what I want because I am trying to access "org.freedesktop.timedate1"
So instead I instantiated it's base class SDObject from pystemd.base (https://github.com/facebookincubator/pystemd/blob/master/pystemd/base.py)
The following code allowed me to get the sync status of NTP
from pystemd.base import SDObject
obj = SDObject(
destination=b'org.freedesktop.timedate1',
path=b'/org/freedesktop/timedate1',
bus=None,
_autoload=False
)
obj.load()
is_sync = obj.Properties.Get('org.freedesktop.timedate1','NTPSynchronized')
print(is_sync)
Not sure if this is what the library author intended, but hey it works!

Saving socket connection to JSOn

I am using socket.io websockets in nodejs. I am trying to stringify the socket object to be able to save it into my database. Here is what I am doing:
socket.on('open-room', function(arg, callback) {
var socketStr = JSON.stringify(socket);
}
But I am getting the following error:
TypeError: Converting circular structure to JSON
If you're looking for a general solution on how to convert an object into a JSON string without encountering a circular structure error (usually, you would do this for logging or debugging), check out the S.O. answer Converting Circular Structure to JSON. If it's not important exactly what the output format is, you can use the built in util.inspect(socket).
If you're doing this for any reason other than logging, be aware that a socket.io websocket can't be serialized/deserialized (you won't be able to recreate a working websocket using the database record).
You might have better luck crafting a more specific JSON object containing only the keys you actually care about, and storing that in the database, rather than attempting to stringify the entire object.

Sheets doesn't exist on my googleapis object

I'm trying to use the google sheets api. I've followed this tutorial
https://developers.google.com/sheets/api/quickstart/nodejs
But every time i execute this line
var sheets = google.sheets('v4');
I get this error
TypeError: Object #<GoogleApis> has no method 'sheets'
Any ideas where i'm going wrong? I followed the guide pretty precisely.
Thanks
You may refer with this post. You might encounter this error if you are trying to call a method of an object which does not exist. However, if the property does exist, but is not a function, you'll get an error like: TypeError: Property 'bar' of object #<Object> is not a function. It is simply the default error thrown when a property doesn't exist on an object.
It turned out I was using an old enough verison of node that it the googleapi doesn't support sheets yet.
Updating my node version fixes the issue.

MongoDB does not seem to behave how it should as a whole

When I first used MongoDB I managed to connect successfully, however then I wanted to carry out the most basic query such as:
db.users.find()
I got an error saying TypeError: Cannot read property 'find' of undefined
Basically meaning I cannot use a collection as a property to the object db.
So i tried this:
var user_col = db.collection('users');
user.col.find();
which works absolutely fine.
Over the last few days I have kept having to look up other ways of doing things as the standard documented way doesn't seem to work. Just now I wanted to get the total users on the app, so like it says in the documentation I should do this:
var count = db.runCommand( { count: 'users' } );
console.log(count);
however this gave the error:
TypeError: undefined is not a function
Is there a problem with MongoDB you have seen like this before or am I just being stupid? I do not want to have to keep finding other, less efficient ways of doing things so finally I ask here what is going on.
Thank you.
It appears you are confusing the Mongo shell API with the node.js native driver API. While both are JavaScript, the shell is sync while node.js is async so they're totally different.

Quicklfix related question(FIX::Application)

I am trying to use FIX::Application along with SessionSettings.
The Fix server I am trying to connect to does not see any incoming connection. From my side I see a Logon Message being formulated in toAdmin() callback(which I print out and add certain fields to.
The Question is
1. Do I need to call some form of sendTarget in toAdmin?(I tried that but get a Session not found error)
2. Is there anyway I can increase logging(start logging whats going on under the hood).
Thanks
Firstly the sendTOTarget need not be called in the toAdmin.
As far as logging goes, what i hear is passing th in FIX::LogFactory should be enough.

Resources