The python operation database error - python

I use python operation postgresql database, the implementation of sql, it removed the quotation marks, resulting in inquiries failed, how to avoid?
def build_sql(self,table_name,keys,condition):
print(condition)
# condition = {
# "os":["Linux","Windows"],
# "client_type":["ordinary"],
# "client_status":'1',
# "offset":"1",
# "limit":"8"
# }
sql_header = "SELECT %s FROM %s" % (keys,table_name)
sql_condition = []
sql_range = []
sql_sort = []
sql_orederby = []
for key in condition:
if isinstance(condition[key],list):
sql_condition.append(key+" in ("+",".join(condition[key])+")")
elif key == 'limit' or key == 'offset':
sql_range.append(key + " " + condition[key])
else:
sql_condition.append(key + " = " + condition[key])
print(sql_condition)
print(sql_range)
sql_condition = [str(i) for i in sql_condition]
if not sql_condition == []:
sql_condition = " where " + " and ".join(sql_condition) + " "
sql = sql_header + sql_condition + " ".join(sql_range)
return sql
Error:
MySQL Error Code : column "winxp" does not exist
LINE 1: ...T * FROM ksc_client_info where base_client_os in (WinXP) and...

Mind you I do not have much Python experience, but basically you don't have single quotes in that sequence, so you either need to add those before passing it to function or for example during join(), like that:
sql_condition.append(key+" in ("+"'{0}'".format("','".join(condition[key]))+")")
You can see other solutions in those questions:
Join a list of strings in python and wrap each string in quotation marks
Add quotes to every list elements

Related

Iterating with multiple variables

I am taking a course from Georgia Tech and I have spent all my evening trying to figure this out and I havent been able to do so. My task is as follows:
Write a function called my_TAs. The function should take as
input three strings: first_TA, second_TA, and third_TA. It
should return as output the string, "[first_TA], [second_TA],#and [third_TA] are awesome!", with the values replacing the
variable names.
For example, my_TAs("Sridevi", "Lucy", "Xu") would return
the string "Sridevi, Lucy, and Xu are awesome!".
Hint: Notice that because you're returning a string instead
of printing a string, you can't use the print() statement
-- you'll have to create the string yourself, then return it.
My function returns "Joshua are awesome" instead of all three variables names. I tried this
result = str(first_TA), str(second_TA), str(third_TA) + "are awesome!"
but didn't work.
def my_TAs(first_TA, second_TA, third_TA):
result = str(first_TA) + " are Awesome!"
return result
first_TA = "Joshua"
second_TA = "Jackie"
third_TA = "Marguerite"
test_first_TA = "Joshua"
test_second_TA = "Jackie"
test_third_TA = "Marguerite"
print(my_TAs(test_first_TA, test_second_TA, test_third_TA))
You can use f-Strings to accomplish this:
def my_TAs(first_TA, second_TA, third_TA):
return f"{first_TA}, {second_TA}, and {third_TA} are awesome!"
test_first_TA = "Joshua"
test_second_TA = "Jackie"
test_third_TA = "Marguerite"
print(my_TAs(test_first_TA, test_second_TA, test_third_TA))
Output:
Joshua, Jackie, and Marguerite are awesome!
Use + instead of ,
def my_TAs(first_TA, second_TA, third_TA):
result = str(first_TA) + ", " + str(second_TA) + ", and " + str(third_TA)
+ " are Awesome!"
return result
first_TA = "Joshua"
second_TA = "Jackie"
third_TA = "Marguerite"
test_first_TA = "Joshua"
test_second_TA = "Jackie"
test_third_TA = "Marguerite"
print(my_TAs(test_first_TA, test_second_TA, test_third_TA))

What cause SQL slowdown in Python?

My problem is that when I run project or debug, the first query run so fast in about < 1s, but when it comes to run second query, it costs more than 30s. I'm so confused about it. I have already ran it in DB editor, both of them run so fast, doesn't have any problem. First look, two queries is quite similar so I do not know why caused it.
By the way sometimes I debug, it pop up a red notice in the left debug and run tab, but I cannot get screen shot of this. It just appear once or twice .
This is screen shot of sql query
query 1: rows = db.select("SELECT recruiter_id FROM linkedin.candidates WHERE recruiter_id in (" + ",".join(recruiter_ids) + ")")
query 2: rows = db.select("select c.recruiter_id, c.updated from linkedin.candidates c where c.recruiter_id in (" + ",".join(duplicates_rid) + ")")
This is my code
if recruiter_ids:
print("Creating connection to MySQL in recruiter 12")
rows = db.select("SELECT recruiter_id FROM linkedin.candidates WHERE recruiter_id in (" + ",".join(recruiter_ids) + ")")
db_recruiter_ids = [r['recruiter_id'] for r in rows] + [get_recruiter_id(url) for url in duplicates]
print("Recruiter ids in database:", len(db_recruiter_ids), db_recruiter_ids[:5])
duplicates = [url for url in profile_urls if any(get_recruiter_id(url) == rid for rid in db_recruiter_ids)]
duplicates_rid = [ get_recruiter_id(url) for url in duplicates]
if duplicates_rid:
rows = db.select("select c.recruiter_id, c.updated from linkedin.candidates c where c.recruiter_id in (" + ", ".join(duplicates_rid) + ")")
#rows = db.select("select c.recruiter_id, c.updated from linkedin.candidates c where c.recruiter_id in {}".format(tuple(duplicates_rid)))
rows = [r['recruiter_id'] for r in rows if r['updated'] < datetime.datetime.now() - datetime.timedelta(days=90)]
old_resumes = [url for url in profile_urls if any(get_recruiter_id(url) == r for r in rows)]
profile_urls = [url for url in profile_urls if not any(get_recruiter_id(url) == rid for rid in db_recruiter_ids)]
print("Found duplicates in list:", len(duplicates), duplicates[:3])
if db_recruiter_ids:
tag_candidate_by_recruiter_id(db, db_recruiter_ids, project_id, tracker_id)
Thank you guy so much !!

SPSS/Python - Accessing variable labels

I am trying to access variable labels in for loop in SPSS using Python. The for loop iterates over a range of variables, deleting 1-3 and renaming 4 and 5 in a sequence of 5 variables. This works fine, but now when trying to access the variable labels via SPSS I am running into the 'unicode object has no attribute keyes' error.
I recognize that I need to somehow refer to the key instead of the string in my array, but as a novice programmer I am struggling to figure out how to update my existing code:
begin program.
import spss, spssaux
vdict=spssaux.VariableDict()
mylist=vdict.range(start="M10", end="ENDOK_D")
nvars = len(mylist)
mycounter = 1
durations = ""
for i in range(nvars):
myvar = mylist[i]
if (mycounter < 4):
spss.Submit("delete variables %s." % myvar)
mycounter +=1
elif (mycounter == 4):
varlabel = mylist[i].VariableLabel
spss.Submit('variable labels %s "%s" [TimeStamp]' % (myvar,varlabel) + ".")
if (myvar.endswith("_C")): mynewvar = myvar[:-2] + "_TS"
spss.Submit("rename variables (%s = %s)" % (myvar,mynewvar) + ".")
spss.Submit("formats %s (DATETIME28.4)" % (mynewvar) + ".")
mycounter +=1
elif (mycounter == 5):
varlabel = mylist[i].VariableLabel
spss.Submit('variable labels %s "%s" [TimeStamp]' % (myvar,varlabel) + ".")
if (myvar.endswith("_D")): mynewvar = myvar[:-2] + "_TSD"
spss.Submit("rename variables (%s = %s)" % (myvar,mynewvar) + ".")
durations += mynewvar + " "
mycounter = 1
spss.Submit("alter type %s (F4.0)" % durations + ".")
end program.
Any help would be greatly appreciated.
The line
spss.Submit('variable labels %s "%s" [TimeStamp]' % (myvar,varlabel) + ".") is what's causing you trouble.
The [TimeStamp] bit is basically telling python to look for a key named "TimeStamp" in a dictionary but before the [TimeStamp] it doesn't find the right data structure - namely a dictionary - but a string which does not have keys. Maybe this might make it clearer:
myDict = {
"varname": "myVariable",
"label": "This is myVariable's label!",
"TimeStamp": "20190204-0814"
}
print (myDict["varname"])
print (myDict["TimeStamp"])
>>> myVariable
>>> 20190204-0814
Python is looking for a data structure like this and wants to look up the key "TimeStamp". Now, I assume you just want your labels to read "Whatever Variable Label was assigned [TimeStamp]"?
Simply change the two lines to
spss.Submit('variable labels %s "%s" + " [TimeStamp]"' % (myvar,varlabel) + ".")

Dictionary query

Below is a function that extracts information from a database which holds information about events. Everything works except that when I try and iterate through times in rows in HTML it is apparently empty. I will therefore assume that rows.append(time) is not doing what it should be doing. I tried rows.append((time)) and that did not work either.
def extractor(n):
date = (datetime.datetime.now() + datetime.timedelta(days=n)).date()
rows = db.execute("SELECT * FROM events WHERE date LIKE :date ORDER BY date", date = str(date) + '%')
printed_day = date.strftime('%A') + ", " + date.strftime('%B') + " " + str(date.day) + ", " + str(datetime.datetime.now().year)
start_time = time.strftime("%H:%M:%S")
for row in rows:
date_split = str.split(row['date'])
just_time = date_split[1]
if just_time == '00:00:00':
just_time = 'All Day'
else:
just_time = just_time[0:5]
times.append((just_time))
rows.append(times)
results.append((rows, printed_day, start_time, times))
Solved it:
replace
times.append((just_time))
rows.append(times)
with
row['times'] = just_time

Any python module for customized BNF parser?

friends.
I have a 'make'-like style file needed to be parsed. The grammar is something like:
samtools=/path/to/samtools
picard=/path/to/picard
task1:
des: description
path: /path/to/task1
para: [$global.samtools,
$args.input,
$path
]
task2: task1
Where $global contains the variables defined in a global scope. $path is a 'local' variable. $args contains the key/pair values passed in by users.
I would like to parse this file by some python libraries. Better to return some parse tree. If there are some errors, better to report them. I found this one: CodeTalker and yeanpypa. Can they be used in this case? Any other recommendations?
I had to guess what your makefile structure allows based on your example, but this should get you close:
from pyparsing import *
# elements of the makefile are delimited by line, so we must
# define skippable whitespace to include just spaces and tabs
ParserElement.setDefaultWhitespaceChars(' \t')
NL = LineEnd().suppress()
EQ,COLON,LBRACK,RBRACK = map(Suppress, "=:[]")
identifier = Word(alphas+'_', alphanums)
symbol_assignment = Group(identifier("name") + EQ + empty +
restOfLine("value"))("symbol_assignment")
symbol_ref = Word("$",alphanums+"_.")
def only_column_one(s,l,t):
if col(l,s) != 1:
raise ParseException(s,l,"not in column 1")
# task identifiers have to start in column 1
task_identifier = identifier.copy().setParseAction(only_column_one)
task_description = "des:" + empty + restOfLine("des")
task_path = "path:" + empty + restOfLine("path")
task_para_body = delimitedList(symbol_ref)
task_para = "para:" + LBRACK + task_para_body("para") + RBRACK
task_para.ignore(NL)
task_definition = Group(task_identifier("target") + COLON +
Optional(delimitedList(identifier))("deps") + NL +
(
Optional(task_description + NL) &
Optional(task_path + NL) &
Optional(task_para + NL)
)
)("task_definition")
makefile_parser = ZeroOrMore(
symbol_assignment |
task_definition |
NL
)
if __name__ == "__main__":
test = """\
samtools=/path/to/samtools
picard=/path/to/picard
task1:
des: description
path: /path/to/task1
para: [$global.samtools,
$args.input,
$path
]
task2: task1
"""
# dump out what we parsed, including results names
for element in makefile_parser.parseString(test):
print element.getName()
print element.dump()
print
Prints:
symbol_assignment
['samtools', '/path/to/samtools']
- name: samtools
- value: /path/to/samtools
symbol_assignment
['picard', '/path/to/picard']
- name: picard
- value: /path/to/picard
task_definition
['task1', 'des:', 'description ', 'path:', '/path/to/task1 ', 'para:',
'$global.samtools', '$args.input', '$path']
- des: description
- para: ['$global.samtools', '$args.input', '$path']
- path: /path/to/task1
- target: task1
task_definition
['task2', 'task1']
- deps: ['task1']
- target: task2
The dump() output shows you what names you can use to get at the fields within the parsed elements, or to distinguish what kind of element you have. dump() is a handy, generic tool to output whatever pyparsing has parsed. Here is some code that is more specific to your particular parser, showing how to use the field names as either dotted object references (element.target, element.deps, element.name, etc.) or dict-style references (element[key]):
for element in makefile_parser.parseString(test):
if element.getName() == 'task_definition':
print "TASK:", element.target,
if element.deps:
print "DEPS:(" + ','.join(element.deps) + ")"
else:
print
for key in ('des', 'path', 'para'):
if key in element:
print " ", key.upper()+":", element[key]
elif element.getName() == 'symbol_assignment':
print "SYM:", element.name, "->", element.value
prints:
SYM: samtools -> /path/to/samtools
SYM: picard -> /path/to/picard
TASK: task1
DES: description
PATH: /path/to/task1
PARA: ['$global.samtools', '$args.input', '$path']
TASK: task2 DEPS:(task1)
I've used pyparsing in the past and been immensely pleased with it (q.v., the pyparsing project site).

Categories