Using cx_Oracle, I am trying to use a Python script to execute a sql command, using the bind variables 'plat' and 'tick'. When trying to execute this command, it gives me the error "ORA-24373: invalid length specified for statement".
To debug, I made a SQL call through Oracle (not Python) using the same arguments as my script (plat=1234567, tick='ABCDE'), and it ran as expected. I tried passing the parameters as both a dict and individual named variables, but both times I got the same error.
I tried changing the values to be lower ('1' and 'A'), but even that is an 'invalid length'.
updateRecords.py
import os
import cx_Oracle
# For security reasons I cannot show my 'create_connection()' function,
# but suffice to say I have tested it and it works as desired.
...
#Setup:
WORKING_PATH = os.path.dirname(os.path.abspath(__file__))
SQL_PATH = os.path.join(WORKING_PATH, 'sql')
cnnSB = create_connection()
cnnSB_cursor = cnnSB.cursor()
...
fetchComp = open(os.path.join(SQL_PATH, 'fetchRecentEntry.sql'), 'r')
for x in range(0, 5):
cnnSB_cursor.execute(fetchComp.read(), {"plat":'A', "tick":1}) # ERROR LINE
fetchRecentEntry.sql
select *
from MFS_PCIINCEXTFUNDBYPLAT
where PLATFORM = :plat
and TICKER = :tick
and STARTDATE = (select max(STARTDATE) from MFS_PCIINCEXTFUNDBYPLAT
where PLATFORM = :plat
and TICKER = :tick)
The above snippet results in the following error message:
File "updateRecords.py", line 297, in main
cnnSB_cursor.execute(fetchComp.read(), plat='A', tick=1)
cx_Oracle.DatabaseError: ORA-24373: invalid length specified for statement
Other things I have checked:
-My fetchComp.read() DOES return the desired code
-Passing in variables as a dict object does NOT change the error message
I found a solution:
The issue comes from the .read() being called inside of a loop. As a result, it would read the file correctly the first time, but on subsequent loops it would only read the null/EOF.
To fix, all I had to do was set the sql.read() to a variable before the loop, and use that variable instead of calling .read() with each loop.
Example:
sql = fetchComp.read()
for index, testRow in testDF.iterrows():
cnnSB_cursor.execute(sql, tick=testRow[1], plat=testRow[0])
compDF = pd.DataFrame(cnnSB_cursor.fetchall())
Related
I'm trying to run a sample function that allows a user to execute arbitrary code
Note: I"m assuming this is ok because Azure Functions will by default provide a sandbox. (And the end user will need to write code with dataframes, objects etc. I've looked into pypy.org but don't think I need it as I am not worried about attacks that use it as a spambot or something):
import os
import json
import ast
print('==============in python function========================')
postreqdata = json.loads(open(os.environ['req']).read())
response = open(os.environ['res'], 'w')
response.write("hello world from "+postreqdata['name'])
response.close()
logic = (postreqdata['logic'])
eval(logic)
but I keep getting the following output/error:
2018-01-17T09:09:08.949 ==============in python function========================
2018-01-17T09:09:09.207 Exception while executing function: Functions.ccfinopsRunModel. Microsoft.Azure.WebJobs.Script: Traceback (most recent call last):
File "D:\home\site\wwwroot\ccfinopsRunModel\run.py", line 12, in <module>
eval(logic)
File "<string>", line 1
print('code sent from client')
^
SyntaxError: invalid syntax
.
My POST request body contains the following:
{
"name": "Python Function App",
"logic": "print('code sent from client')"
}
So the "logic" variable is being read in, and eval() is trying to interpret the string as python code, but it is causing a Syntax Error where there appears to be none.
What am I doing wrong? If there was a restriction on 'eval' I'm assuming it would say that instead of "Syntax Error"
Thanks for any help you can provide!
Use exec to run your code. eval is used evaluating expressions.
logic = (postreqdata['logic'])
exec(logic)
Also can try sending your code as multi-line string as below,
>>> s = '''
for i in range(3):
print("i")
'''
>>> exec(s)
0
1
2
I am trying to build a Phasor Measurement Unit using Beaglebone Black rev 3. The following is the code. While running it gives an error as:
prussdrv_open open failed
Traceback (most recent call last):
File "/var/lib/cloud9/pmu.py", line 36, in <module>
pru.open(0) # open connection to PRU 0
SystemError: error return without exception set
The code goes here:
import pypruss as pru
import mmap
import numpy as np
import struct
import time
## MEMORY LOCATIONS ##
PRU_ICSS=0x4A300000
PRU_ICSS_LEN=512*1024
RAM_START=0x00000000
RAM1_START=0x00002000
RAM2_START=0x00012000
TOTAL_BUFFER_LEN=0x00000FA0
BUFFER_LEN=TOTAL_BUFFER_LEN/2
BUFFER1_START=RAM2_START+4
BUFFER2_START=BUFFER1_START+BUFFER_LEN
## FUNCTION DEFINITIONS ##
def processRawADC(value):
value=0x00000FFF&value
value=int(value)
value=(value*1.8)/(2^12)
return value
def channelID(value):
value=0x000F0000&value
value=value>>16
return value
## PRU SETUP ##
pru.modprobe( ) # enable uio_pruss module
pru.init( ) #initialize PRU
pru.open(0) # open connection to PRU 0
pru.pruintc_init( ) # configure interrupt handlers
pru.exec_program(0,"./oneshot.bin") # load assembly file
counter = 0
f=open("/dev/mem","r+b")
output=open("./results.txt","w")
while counter<10 :
start=time.time()
pru.wait_for_event(0)
ddr_mem=mmap.mmap(f.fileno( ),PRU_ICSS_LEN,offset=PRU_ICSS)
shared=struct.unpack('L ',ddr_mem[RAM2_START:RAM2_START+4])
print(shared[0])
if shared[0]==1 :
print ("buffer 1")
for i in range(0,500) :
fifo = struct.unpack ( 'L ' ,ddr_mem[BUFFER2_START+( i*4)
:BUFFER2_START+4+(i*4)])[0]
value=processRawADC(fifo)
channelNum=channelID(fifo)
output.write(str(channelNum)+","+str(value)+"nn")
counter += 1
pru.clear_event(0)
elif shared[0] == 2:
shared=struct.unpack('L ',ddr_mem[RAM2_START:RAM2_START+4])
print("buffer 2")
for i in range(0,500):
fifo=struct.unpack('L',ddr_mem[BUFFER2_START+(i*4) :BUFFER2_START+4+
(i*4)])[0]
value = processRawADC(fifo)
channelNum = channelID(fifo)
output.write(str(channelNum)+","+str(value)+"nn")
counter +=1
pru.clear_event(0)
end=time.time( )
#print end-start
f.close( )
output.close( )
pru.clear_event(0)
pru.pru_disable(0)
pru.exit ( )
I am unable to find, where is the mistake lies. Please Help.
Looks like there is a bug in PyPRUSS code.
Its pypruss_open function does not properly set exception information but returns an error indication (NULL). Python doesn't like when a function does so.
Looking at the pypruss_open source, it will fail in such way if prussdrv_open fails and returns -1 as an error indication. It in turn might fail either itself (if that device is already opened) or if __prussdrv_memmap_init fails.
Unfortunately, looks like there is no way to get information about the exact reason of the error.
What can you do to debug this issue? If you won't be able to find anything obvious (like missing /dev/uid0 after calling pru.modprobe()) then you can run your script with strace to see which system calls precede an error. Then you look at the source code under links I gave you above and see when exactly does the failure happen.
I have a simple function that I use to read the contents of a text file into a Python variable. I use it to import SQL queries. The function takes a parameter that is the path and name of the text file and allows several attempts to get the name right, allowing for typos or mis-spellings. By default, this parameter is set to None. If the file can't be found, the function prints an error message and presents an input() box to allow a new path and filename to be entered. The function then either returns the string (representing the SQL query) or returns None if the file can't be found.
The function is:
def readQueryFromFile(queryPathAndFileName = None):
maxAttempts = 3
for i in range(maxAttempts):
if (queryPathAndFileName is None) or (i > 0):
queryPathAndFileName = input("Enter path and filename for text file contains SQL query: ")
try:
tempFileObject = open(queryPathAndFileName)
tempQuery = tempFileObject.read()
tempFileObject.close()
break
except FileNotFoundError as e:
print("\nA FileNotFoundError occurred.\nError number {0}: {1}. File named \'{2}\' does not exist at that location.".format(e.args[0],e.args[1],queryPathAndFileName))
if i < (maxAttempts-1):
print('\nPlease re-enter path and filename details.\n') # Only say 'Please try again' if not last attempt.
else:
# If query file can't be found then set tempQuery to None
print('\nFailed to find file containing query after {0} attempts.\n'.format(i+1))
tempQuery = None
return tempQuery
The function could be called in a Jupyter notebook cell using:
myQuery = readQueryFromFile(queryPathAndFileName = '/geosgnasoeg/asgogeso.sges')
Clearly, the path and file name is nonsensical and the function presents an error message and a prompt to enter the path and file name again. However, the error messages appear after the input box is displayed as follows:
Enter path and filename for text file contains SQL query: |________|
A FileNotFoundError occurred.
Error number 2: No such file or directory. File named '/geosgnasoeg/asgogeso.sges' does not exist at that location.
Please re-enter path and filename details.
Having the messages appear out of sequence can be confusing. Interestingly, if a second incorrect path and file name is entered, the output realigns itself correctly.
I'm using a Mac running El Capitan and this issues occurs in both Safari and Firefox.
Is there a way to force the output displayed in Jupyter notebook to appear in the correct (i.e. sequential) order?
I run a command in which it creates a new camera, however at the end of the said function, there is no selection nor does the function selects the object after it has run its course.
So are there any commands in which I could possible query for the last created item?
I tried using `cmds.listHistory' but that will only shows you results if there is already a selection..
Any ways in which I can get around with it?
Additionally, say I am using the following command using the
cameraShape...
aaa = "cameraShape1"
mel.eval('<Some mel-based command> cameraShape.transformX cameraShape.transformY cameraShape.transformZ;')
but when I tried writing that command in another way such as :
mel.eval('<Some mel-based command> %s.transformX %s.transformY %s.transformZ;' %aaa)
I got an error saying
# Error: not enough arguments for format string
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# TypeError: not enough arguments for format string #
Where am I writing it wrong exactly? I tried writing like %aaa, aaa, aaa still the same error occurs
Why can't you just stuff the new camera into a variable instead of relying on selection?
new_camera, new_camera_shape = cmds.camera()
You're not using the right syntax when formatting with %:
"My name is %s" % "Jon Snow" # Works for single
"My name is %s and I was born in %s" % ("Jon Snow", "Winterfell") # Multiple
Personally I prefer format() as it's suppose to be more forward compatible for Python 3:
"My name is {0} and I was born in {1}".format("Jon Snow", "Winterfell")
Detect new objects:
scene_before = cmds.ls(l=True, transforms=True)
# Run command to import object here
scene_after = cmds.ls(l=True, transforms=True)
new_objs = list( set(scene_after).difference(scene_before) )
If you want to keep the last created object. You can create a class that contain a variable history where your append in your other script the last object created.
class History:
idCounter = []
def __init__(self, name):
History.idCounter.append(name)
print(History.idCounter)
for name in ['nana', 'tata', 'zaza']:
objectCreated = History(name)
I'm trying to delete all stacks those environments(instances) were terminated. while passing the stack_name to the delete_stack it's throwing an error. I've tried to add the special characters (such as '') to the value of stack_name,but there was no luck. Can someone please help me to fix the issue. Thanks in advance!
#!/usr/bin/env python
import boto
import boto.ec2
import boto.cloudformation
import re
from datetime import datetime, timedelta
utclast = datetime.utcnow() - timedelta(2)
conn = boto.cloudformation.connect_to_region('us-west-1',aws_access_key_id = '<access_key>',aws_secret_access_key = '<secret_key>')
conn_ec2 = boto.ec2.connect_to_region('us-west-1',aws_access_key_id = '<access_key>',aws_secret_access_key = '<secret_key>')
stacks = conn.list_stacks()
for stackSumm in stacks:
pattern = re.compile("Testupload-env([a-zA-Z0-9]+)")
match = pattern.match(stackSumm.stack_name)
if stackSumm.stack_status in "CREATE_COMPLETE" and match and stackSumm.stack_name in match.string:
m = re.split(r'Testupload-', stackSumm.stack_name)
instance = conn_ec2.get_all_instances(filters={"tag:Name": m[1]})
if not instance:
try:
print "Trying to delete stack: %s" % stackSumm.stack_name
conn.delete_stack(stackSumm.stack_name)
except boto.exception.BotoServerError, e:
print e.error_message
Error:
File "delete_stack.py", line 7, in <module>
conn.delete_stack(Testupload-envmeraleb8b01739116b0f36d17a2b5445b949f592bb625-6293)
NameError: name 'Testupload' is not defined
None of you posted code generated the error, but the NameError says that you didn't provide a valid stack name
delete_stack(stack_name_or_id)
Deletes a specified stack. Once the call completes successfully, stack deletion starts. Deleted stacks do not show up in the DescribeStacks API if the deletion has been completed successfully.
Parameters: stack_name_or_id (string) – The name or the unique identifier associated with the stack.
The name/id should be a string, a string in python must be quoted, either by single quotes or double quotes
stack_name = "Testupload-envmeraleb8b01739116b0f36d17a2b5445b949f592bb625-6293"
conn.delete_stack(stack_name)