AWS Lambda - time.time() returns undefined - python

Im working with a lambda function where i use boto3 to put_item() into my DynamoBD Table and on the code im adding the ttl parameter (Time to live).
ttl = str(int(time.time() + 2629746))
This line gives me a 1 month ttl but for some reason im getting alot of this errors:
An error occurred (ValidationException) when calling the PutItem operation: The parameter cannot be converted to a numeric value: undefined: ClientError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 52, in handler
response = c_put_item(d)
File "/var/task/lambda_function.py", line 40, in c_put_item
'ttl':{'N':ttl}
File "/var/runtime/botocore/client.py", line 317, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 615, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the PutItem operation: The parameter cannot be converted to a numeric value: undefined
Any idea why?
PS: im using python3
-- EDIT:
Im adding a little bit more of the code.
For some reason this isnt working.
ttl = str(int(time.time() + 2629746))
response = client.put_item(TableName='MYTABLENAME',Item={
'item':{'S':item},
'title':{'S':title},
'link':{'S':link},
'price':{'N':price},
'category':{'S':category},
'avaliable':{'S':avaliable},
'image':{'S':image},
'ttl':{'N':ttl}
})
-- EDIT2:
The AWS docs specify you should use the put_item() as i did except im forced to use str() cause i was getting an error.

#kichik was actually right on his comment. This error doesn't necessarily say ttl is None. It says one of the fields of the entire call is None.
So ones i detected the conflicting field i added an exception and the problem stopped.

Consider making ttl an integer: ttl = int(time.time() + 2629746)
Boto3 docs, Creating a new item:
For all of the valid types that can be used for an item, refer to Valid DynamoDB Types:
These are the valid item types to use with Boto3 Table Resource (dynamodb.Table) and DynamoDB:
• integer – Number (N)
• decimal.Decimal – Number (N)

Related

How to properly serialize and deserialize paging_size in Python?

In my Python application, I make the query to the Cassandra database. I'm trying to implement pagination through the cassandra-driver package. As you can see from the code below, paging_state returns the bytes data type. I can convert this value to the string data type. Then I send the value of the str_paging_state variable to the client. If this client sends me str_paging_state again I want to use it in my query.
This part of code works:
query = "select * from users where user_type = 'clients';"
statement = SimpleStatement(query, fetch_size=10)
results = session.execute(statement)
paging_state = results.paging_state
print(type(paging_state)) # <class 'bytes'>
str_paging_state = str(paging_state)
print(str_paging_state) # "b'\\x00C\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x03_hk\\x00\\x00\\x00\\x11P]5C#\\x8bGD~\\x8b\\xc7g\\xda\\xe5rH\\xb0\\x00\\x00\\x00\\x03_rk\\x00\\x00\\x00\\x18\\xee\\x14\\xf7\\x83\\x84\\x00tTmw[\\x00\\xec\\xdb\\x9b\\xa9\\xfd\\x00\\xb9\\xff\\xff\\xff\\xff\\xfe\\x01\\x00'"
This part of code raise error:
results = session.execute(
statement,
paging_state=bytes(str_paging_state.encode())
)
Error:
[ERROR] NoHostAvailable: ('Unable to complete the operation against any hosts')
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 51, in lambda_handler
    results = cassandra_connection.execute(statement, paging_state=bytes(paging_state.encode()))
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 2618, in execute
    return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile, paging_state, host, execute_as).result()
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 4877, in result
    raise self._final_exceptionEND RequestId: 4b7bf588-a2d2-45e5-ad7e-8611f1704313
In Java documentation I found the .fromString method which creates a PagingState object from a string previously generated with toString(). Unfortunately, I didn't find an equivalent for this method in Python.
I also tried to use codecs package to decode and encode the paging_state.
str_paging_state = codecs.decode(paging_state, encoding='utf-8', errors='ignore')
# "\u0000C\u0000\u0000\u0000\u0002\u0000\u0000\u0000\u0003_hk\u0000\u0000\u0000\u0011P]5C#GD~grH\u0000\u0000\u0000\u0003_rk\u0000\u0000\u0000\u0018\u0014\u0000tTmw[\u0000ۛ\u0000\u0001\u0000"
# Raise error
results = session.execute(statement, paging_state=codecs.encode(str_paging_state, encoding='utf-8', errors='ignore'))
In this case I see next error:
[ERROR] ProtocolException: <Error from server: code=000a [Protocol error] message="Invalid value for the paging state">
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 50, in lambda_handler
    results = cassandra_connection.execute(
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 2618, in execute
    return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile, paging_state, host, execute_as).result()
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 4877, in result
    raise self._final_exceptionEND RequestId: 979f098a-a566-4904-821a-2ce06522d909
In my case, protocol version is 4.
cluster = Cluster(..., protocol_version=4)
I would appreciate any help!
Just convert the binary data into hex string or base64 - use binascii module for that. For example, for first case functions hexlify/unhexlify (or in Python 3 use .hex method of binary data), and for base64 - use functions b2a_base64/a2b_base64

custom exception message in sqlalchemy

Im trying to raise a custom exception message when I get an exception but I get the following error -
try:
query_start_time = time.time()
execute_sql_alchemy_query
except Exception as ex:
elapsed_time = (time.time() - query_start_time)/60
print(type(ex))
raise type(ex)("Query elapsed time(in mins) - {0}".format(elapsed_time))
Error:-
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/profiles/sources/impact/test.py", line 110, in _handle_future_exception
future.result()
File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
TypeError: __init__() missing 2 required positional arguments: 'params' and 'orig'
The exception being raised is probably a subclass of DBAPIError (https://docs.sqlalchemy.org/en/13/core/exceptions.html#sqlalchemy.exc.DBAPIError) which takes 3 positional/mandatory parameters in its constructor (statement, params and orig), by trying to raise it like this, you are only providing a “statement” but not the 2 other arguments.
You could probably manage to make it work by providing the .params and .orig from the initial exception but I’d discourage you to go that way because it’s error prone and a bit fragile (it will break if the exception is not a DBAPIError), instead I’d suggest to simply log the time separately from the error (using logging.warning)

Boto3 - Delete_snapshot not evaluating variables

I'm trying to run boto3 to loop through snapshots older than 14 days.
It can find all the snapshots older than 14 days fine, and I've verified that all that works okay. The problem is when it runs through the dictionary trying to delete, it looks like the function isn't correctly evaluating the variable (See below).
It seems to just include it as a string.
The loop runs through the dict using a "for snapshot in ..." if'ing the tags to find the snapshots ready for deletion. Here's the 'if' part:
if snap_start_time < expiry: # check if it's more than a <expiry> old
print "Deleting Snapshot: " + snapshot['SnapshotId']
response = ec2client.delete_snapshot(
SnapshotId=snapshot['SnapshotId']
)
errors here:
Deleting Snapshot: snap-f4f0079d
Traceback (most recent call last):
File "./aws-snap.py", line 27, in <module>
SnapshotId=snapshot['SnapshotId']
File "/usr/lib/python2.6/site-packages/botocore/client.py", line 159, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/lib/python2.6/site-packages/botocore/client.py", line 494, in _make_api_call
raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidSnapshot.NotFound) when calling the DeleteSnapshot operation: None
Any clues? \o/
I would doubt that the SnapshotId might not be passing as a string.
Change the SnapshotId to a string format and pass it for deletion.
str(snapshot['SnapshotId'])
As it turns out, referencing straight from the dictionary is a bad idea. It needs to be wrapped in str() and provided with the DryRun=False option too.

TypeError neo4jrestclient append() got an unexpected keyword argument 'data'

I am using neo4jrestclient for Neo4j in python and locally it works perfectly. When I host it using webfaction it returns the following error:
TypeError at /add/
append() got an unexpected keyword argument 'data'
Django Version: 1.6.1
Exception Type: TypeError
Exception Value:
append() got an unexpected keyword argument 'data'
Exception Location: /home/kokos/lib/python2.7/neo4jrestclient/client.py in create, line 1036
I have no clue where the problem might be. Thanks in advance.
I have the same problem. See the minimal example below. It seems that query call messes up some internal state of library. I'll investigate further.
>>> gdb.node()
<Neo4j Node: http://localhost:7474/db/data/node/140>
>>> gdb.query("match (n) where 0 > 1 return n")
<neo4jrestclient.query.QuerySequence object at 0x00000000037A5C88>
>>> gdb.node()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python34\lib\site-packages\neo4jrestclient\client.py", line 1000, in __call__
return self.create(**kwargs)
File "C:\Program Files\Python34\lib\site-packages\neo4jrestclient\client.py", line 1036, in create
returns=NODE)
TypeError: append() got an unexpected keyword argument 'data'
It's because query stores corresponding transaction in global variable and this transaction is tried to be used during later call. However Transaction and QueryTransaction are different incompatible classes. See https://github.com/versae/neo4j-rest-client/issues/103.

Windows Python 2.5 Traceback.py Nonetype error

Hello I am running Python 2.5 on Windows and whenever my application gets an exception rather than seeing the debug information I get an error inside of the traceback.py file itself. Anyone know a fix for this mb a patch or replacement file.
Traceback (most recent call last):
File "C:\Python25\lib\logging\__init__.py", line 744, in emit
msg = self.format(record)
File "C:\Python25\lib\logging\__init__.py", line 630, in format
return fmt.format(record)
File "C:\Python25\lib\logging\__init__.py", line 426, in format
record.exc_text = self.formatException(record.exc_info)
File "C:\Python25\lib\logging\__init__.py", line 398, in formatException
traceback.print_exception(ei[0], ei[1], ei[2], None, sio)
File "C:\Python25\lib\traceback.py", line 126, in print_exception
lines = format_exception_only(etype, value)
File "C:\Python25\lib\traceback.py", line 176, in format_exception_only
stype = etype.__name__
AttributeError: 'NoneType' object has no attribute '__name__'
===EDIT===
Found same error in mailing list here outdated answer it seems
http://mail.python.org/pipermail/python-dev/2006-September/068975.html
Possible causes:
Calling logging.exception() when there is no active exception
Calling a logging function with exc_info=1, when there is no active exception.
Calling a logging function with exc_info=(None, None, None) to a logging function (e.g. if doing the exception logging manually).
You should not use logging.exception outside of an except block.
The exception is caused by a None exception type passed to traceback.print_exception, meaning that there is no active exception to process.
Meanwhile, the newsgroup posting you linked to indicates that it was a regression in the standard library that resulted in that particular traceback. You may want to try upgrading your Python to 2.5.1, which fixed this particular problem.

Categories