Python GQL query not working [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
ID = int(data_key)
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID='ID'")
where i have done wrong...
Thanks..

Use a parameter (examples 1, 2):
ID = int(data_key)
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID=:1", ID)
# or
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID=:id", id=ID)

You could also use the more "Pythonic" model API:
from path.to.your.models import PatientMeds
results = PatientMeds.all().filter('patientinfo_ID =', int(data_key))

Related

python string find and look up on string until found space from json [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Below is my data.
'{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}'
My output should be as below
[{"a":5647953897,"b":"323299901059958183671030","c":1605858513465},{"a":5647953897,"b":"323299901059958183671030","c":1605858513465}]
This should solve your case.
from json import JSONDecoder, JSONDecodeError
import re
NOT_WHITESPACE = re.compile(r'[^\s]')
data = '''{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}'''
def decode_stacked(document, pos=0, decoder=JSONDecoder()):
while True:
match = NOT_WHITESPACE.search(document, pos)
if not match:
return
pos = match.start()
try:
obj, pos = decoder.raw_decode(document, pos)
except JSONDecodeError:
raise
yield obj
for obj in decode_stacked(data):
print(obj)

Create email with the firsts letters of the name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any magic python way to get the firsts letters of each name and join with '#company.com' to create a email address?
Here is how I did:
name = ['Elon Reeve Musk']
full_name = [word[0].lower() for word in name[0].split(' ')]
firts_letters = "".join(full_name)
username = '%s#company.com' %(firts_letters)
The result is erm#company.com
Here's some "python magic"
name = ['Elon Reeve Musk']
f"{''.join(filter(str.isupper, name[0]))}#company.com".lower()
>>> erm#company.com
Whether this is better than your method is debatable. Most of the time a few lines of easy to read code is better than a one line hack.
My recommendation would be
name = ['Elon Reeve Musk']
initials = ''.join(word[0] for word in name[0].split())
f'{initials.lower()}#company.com'
>>> erm#company.com

Very simple python cycle string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to write in a file which changes the name with the counter.
for j in range(30):
fileout.write("bla bla")
and fileout should be
file001 if j=1
file002 if j=2
...
...
I cannot format it in the right way.
Anyone can help me?
Thank you
You can try to use the .zfill( < number of digits > ) option. It will complet your string with 0.
For example, this code should work :
radical ='file'
numberOfDigits = 3
for j in range( 30 ) :
fileout = open ( radical + '%s' %(str(j).zfill(numberOfDigits)) + '.txt' , 'w' )
fileout.write('bla bla')
fileout.close()
Not sure if below means what you needed ?
Code below will create file000 - file029 with 'bla bla' inside
for i in range(30):
with open("file%03d" % i, 'w') as f:
f.write('bla bla')

Create correct loop output with python function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Could someone help me with following function:
def nagios_chart():
alpha = [chr(item).upper() for item in range(ord('b'), ord('l')+1)]
for idx, column in enumerate(alpha):
print worksheet.write_column('column + 1', nagios_data[idx])
if __name__ == '__main__':
nagios_chart()
I need output like this:
worksheet.write_column('B1', nagios_data[0])
worksheet.write_column('C1', nagios_data[1])
worksheet.write_column('D1', nagios_data[2])
worksheet.write_column('E1', nagios_data[3])
worksheet.write_column('F1', nagios_data[4])
worksheet.write_column('G1', nagios_data[5])
worksheet.write_column('H1', nagios_data[6])
worksheet.write_column('I1', nagios_data[7])
worksheet.write_column('J1', nagios_data[8])
worksheet.write_column('K1', nagios_data[9])
worksheet.write_column('L1', nagios_data[10])
To use the column as a variable, remove it from the quotes so it isn't treated as a string literal. Then you can concatenate it with '1'.
def nagios_chart():
alpha = [chr(item).upper() for item in range(ord('b'), ord('l')+1)]
for idx, column in enumerate(alpha):
print worksheet.write_column(column + '1', nagios_data[idx])

How to format this line according to PEP 8? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to adhere to PEP 8, with a 78 character limit on the length of my lines.
I have the following statement:
startTime = time.strptime(request.GET.get('st', (dt.datetime.now() - dt.timedelta(days=1)).strftime("%d/%b/%Y:%H:%M:%S")), "%d/%b/%Y:%H:%M:%S")
How should I format it so that it adheres to PEP8 (where should I break it into new lines?)
Avoid writing such overly convoluted code in the first place:
if 'st' in request.GET:
startTime = time.strptime(request.GET['st'], "%d/%b/%Y:%H:%M:%S")
else:
startTime = (dt.datetime.now() - dt.timedelta(days=1)).timetuple()
Why generate a default to be parsed out to a timetuple again instead of just going there straight?
startTime = time.strptime(
request.GET.get(
'st',
(
dt.datetime.now() - dt.timedelta(days=1)
).strftime("%d/%b/%Y:%H:%M:%S")
),
"%d/%b/%Y:%H:%M:%S"
)
This one solution, but you use more variables for this. For example:
time_format = "%d/%b/%Y:%H:%M:%S"
yesterday_date = dt.datetime.now() - dt.timedelta(days=1
This would make the code more readable
Split it up into different variables:
a_date = (dt.datetime.now() - dt.timedelta(days=1)).strftime("%d/%b/%Y:%H:%M:%S")
req = request.GET.get('st', a_date)
startTime = time.strptime(req, "%d/%b/%Y:%H:%M:%S")

Categories