Chain Functions, I'm getting an error from PyCharm - function

Chain Functions...the how to is alluding me.
if monthly_investment (float(input("Enter monthly investment:\t ")) = > 1) and (monthly_investment < = 1000):
Getting "expected ',' or ')', I don't see the need for either...

Related

jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

I'm trying to use an xcom_pull inside an SQL phrase executed by a Snowflake operator in Airflow.
I need the task_id name to use a variable since I want to support different tasks.
I tried this syntax but seems it is not being rendered ok.
Anyone has an idea how to do it?
This is the Python code:
for product, val in PRODUCTS_TO_EXTRACT_INC.items():
product_indicator, prefix = val
params['product_prefix'] = prefix
calculate_to_date = SnowflakeOperator(
dag=dag,
task_id=f'calculate_to_date_{prefix}',
snowflake_conn_id = SF_CONNECTION_ID,
warehouse=SF_WAREHOUSE,
database=BI_DB,
schema=STG_SCHEMA,
role=SF_ROLE,
sql= [ """
{SQL_FILE}
""".format(SQL_FILE="{% include '" + QUERIES_DIR + ETL + "/calculate_to_date.sql'" + " %}")
],
params=params
)
This is the SQL code for calculate_to_date.sql:
select '{{{{ (ti.xcom_pull(key="return_value", task_ids=["calculate_from_date_{}"])[0][0]).get("FROM_DATE") }}}}'.format(params.product_prefix) AS TO_DATE
This is the error message:
File "/home/airflow/gcs/dags/Test/queries/fact_subscriptions_events/calculate_to_date.sql", line 11, in template
select '{{{{ (ti.xcom_pull(key="return_value", task_ids=["calculate_from_date_{}"])[0][0]).get("FROM_DATE") }}}}'.format(params.product_prefix)
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'
the correct syntax is
select '{{ (ti.xcom_pull(key="return_value", task_ids="calculate_from_date_{}".format(params.product_prefix))[0]).get("FROM_DATE") }}' AS TO_DATE
it works like a charm

1 time of 10 get error json.decoder.JSONDecodeError: Expecting ','

i have this ugly noob func:
def run_prog(compile_container, user_input, container_file_path):
# bash doesnt support single quotes or quotes inside double quotes so ## = '
time_check_command = '/usr/bin/time -f "## , ##memory##:##%M## , ##time##:##%e##"'
result = compile_container.exec_run(
f"/bin/bash -c 'echo {user_input} | {time_check_command} ./a.out'",
workdir=container_file_path)[1].decode('utf-8')
result = '{"result":"' + result + ',}'
result = result.replace('##', '"')
result_dict = json.loads(result)
if result_dict['time'] == '0.00':
result_dict['time'] = '<0.01'
return result_dict
Most of time it retrun json like this:
{
"result": "888",
"memory": "1792",
"time": "<0.01"
}
But one time of 10 or mb 20 it raise an error "error json.decoder.JSONDecodeError: Expecting ','" and i dont know why. Same input always. Can u please tell what is wrong?
So, i dont really sure whats wrong, but switch from json.loads to ast.literal_eval helped.
result_dict = ast.literal_eval(result)

redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: malformed number near

I'm writing a lua script in redis, and execute it in spring, the content is as simple as
local store = redis.call('hget',KEYS[1],'capacity')
print(store)
if store <= 0
then return 0
end
store = store - 1
redis.call('hset',KEYS[1],'capacity',store)
redis.call('sadd',KEYS[2],ARGV[1])
return 1
but when I run this script, an exception throws
redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: malformed number near '262b4ca69c1805485d135aa6298c2b00bc7c8c09'
And I tried the following script in redis-cli
eval "local s = tonumber(redis.call('hget',KEYS[1],'capacity')) return s" 1 001
It returns
(integer) 100
And the Java code is showing as follows:
String script ="local store = redis.call('hget',KEYS[1],'capacity')\n" +
"print(store)\n" +
"if store <= 0\n" +
"then return 0\n" +
"end\n" +
"store = store - 1\n" +
"redis.call('hset',KEYS[1],'capacity',store)\n" +
"redis.call('sadd',KEYS[2],ARGV[1])\n" +
"return 1\n" +
"\n";
if(sha==null){
sha = jedis.scriptLoad(script) ;
System.out.println("sha:"+sha);
}
Object ojb = jedis.eval(sha,2,id,userName,id) ;
Now I'm so confused and any help will be grateful
You want to use jedis.evalsha instead of jedis.eval.
The error you are getting is Redis server trying to interpret 262b4ca69c1805485d135aa6298c2b00bc7c8c09 as an actual script. To invoke a previously loaded script you use EVALSHA command.

Python MySQL INSERT unicode

I'm trying to insert JSON data into an MySQL database:
def mapClients():
for d in devices:
clientMap = d['dot11.device']['dot11.device.associated_client_map'].keys()
for item in clientMap:
clientList = kr.device_by_mac(item)
times = kr.device_summary_since()
for c in clientList:
sqlMac = c['kismet.device.base.macaddr'],
sqlType = c['kismet.device.base.type'],
sqlManuf = c['kismet.device.base.manuf'],
ktime = c['kismet.device.base.last_time'],
for t in ktime:
sqlTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t))
cur.execute("INSERT INTO devices(apID,mac,type,manuf,last_seen) VALUES(1,'" + str(sqlMac) + "','" + str(sqlType) + "','" + str(sqlManuf) + "','" + sqlTime + "');")
conn.commit()
mapClients()
This returns the following error:
pymysql.err.ProgrammingError: (1064, u"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES(1,'(u'58:E2:8F:CF:20:B3',)','(u'Wi-Fi Client',)','(u'Apple',)','20-10-201' at line 1")
I can see from the error that the various values are being suffixed with a 'u'. I understand through a lot of searching and learning that (I think) this means the data is unicode.
What I want to do is find a way of converting/decoding the data so the INSERT statements work. Some of the variables are tuples, some strings. Any help much appreciated.
You are inserting tuples, not strings; remove the trailing commas:
sqlMac = c['kismet.device.base.macaddr']
sqlType = c['kismet.device.base.type']
sqlManuf = c['kismet.device.base.manuf']
ktime = c['kismet.device.base.last_time']
sqlTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ktime))
It’s the trailing comma that turns those expressions into tuples, and str() on a tuple gives you the container Unicode string as the u'....' representation that then clashes with the ' quoting you are adding.
Note that removes the need to loop over ktime!
Next, you really want to use SQL parameters, not string concatenation. Use placeholders instead of '" + str(...) + "', and leave handling of quoting to the database adapter:
cur.execute("""
INSERT INTO devices (apID, mac, type, manuf, last_seen)
VALUES (1, %s, %s, %s, %s)
""", (sqlMac, sqlType, sqlManuf, sqlTime))
The %s are the placeholders; depending on your exact MySQL Python library, you may need to use ? questionmarks instead.
Not only would this let you avoid having to think about quoting, it also removes a serious security issue: the JSON you load could contain a SQL injection attack and SQL parameters are the best way to neutralise that attack vector.
For the error message you posted, you have forgotten to place the closing parentheses before the VALUES in your SQL Query. The query should be like:
cur.execute("INSERT INTO devices(apID,mac,type,manuf,last_seen) VALUES(1,'" + str(sqlMac) + "','" + str(sqlType) + "','" + str(sqlManuf) + "','" + str(sqlTime) + "');")

Getting the "Arguments are of the wrong type..." exception just by assigning query text

I have downloaded and installed MySQL Connector 5.1 x64 so I can use MySQL with Delphi. I can make connection with ODBC and do a connection from my Delphi environment and from MySQL Workbench.
But, when I build my Query at runtime, I get an error saying:
Project AAA.exe raised exception class EOleException with message 'Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another'. Process stopped. Use Step or Run to continue.
My code:
qDates := TADOQuery.Create(Component);
qDates.Connection := FConnection;
qDates.SQL.Text :=
'select ' +
' * ' +
'from ' +
' resulttable ' +
'where ' +
' oid = :oid ' +
' and datedial >= :datebegin and datedial <= :dateend'; // <<-- Exception here
Details:
The exception happens right on the text assignment, before I have a chance to configure parameters.
If I comment out the where clause the assignment goes fine.
This is similar to Using parameters with ADO Query (mysql/MyConnector) but the difference is that I assign whole text at once and I get the exception before I have a chance to configure parameters.
The puzzling part - exact same code works fine on my other machine, but I can not figure out what is different.
Hence the question - what could cause the above exception outside of the Delphi code and MySQL server?
This seems to be a quirk with the MySQL ODBC provider.
If you assign the connection after setting the SQL text, then it will work.
The reason why can be found here.
qDates := TADOQuery.Create(Component);
// do net yet assign TADOConnection to prevent roundtrip to ODBC provider
qDates.SQL.Text :=
'select ' +
' * ' +
'from ' +
' resulttable ' +
'where ' +
' oid = :oid ' +
' and datedial >= :datebegin and datedial <= :dateend';
qDates.Connection := FConnection;
UPDATE
This QC entry explains the exact reason for this problem.
In short, the ADODB unit, patch this line from the RefreshFromOleDB procedure :
Parameter.Attributes := dwFlags and $FFFFFFF0; { Mask out Input/Output flags }
To:
if dwFlags and $FFFFFFF0 <= adParamSigned + adParamNullable + adParamLong then
Parameter.Attributes := dwFlags and $FFFFFFF0; { Mask out Input/Output flags }