I try to get the custom json from my OpsWorks stacks with python and boto3. Getting the name is ok but if I want to get the CustomJson - KeyError. Don't have a clue why.
import boto3
import traceback
client = boto3.client('opsworks')
response = client.describe_stacks()
max_elements = len(response['Stacks'])
for i in range(max_elements):
stack_Name = response['Stacks'][i]['Name'] # works
try:
stack_CustomJson = response['Stacks'][i]['CustomJson'] # KeyError
except:
traceback.print_exc()
That's the console output:
$ python3 get_custom_json.py
Traceback (most recent call last):
File "get_custom_json.py", line 27, in get_opsworks_details
stack_CustomJson = response['Stacks'][i]['CustomJson']
KeyError: 'CustomJson'
Reading the docs from http://boto3.readthedocs.org/en/latest/reference/services/opsworks.html#OpsWorks.Client.describe_stacks I don't see a difference between 'Name' and 'CustomJson' except that CustomJson is a JSON object. Do I have to transform it?
Thx in advance
You are getting a KeyError occasionally because the CustomStack element in the response is optional. If a custom stack is specified for the stack, it will be returned. Otherwise, the CustomStack key will not be there at all. You should do something like:
if 'CustomStack' in stack:
# do your thing
Had a quick chat with a developer in my company. Got some basic introductions for getting better in coding and python and whatever (must loose some of my admin thinking).
Don't iterate about max_elements, better iterate above 'stack in stacks'.
for stack in response['Stacks']:
print(stack['CustomJson'])
Now it works - I'll get the custom json from the OpsWorks stacks. But still there is the KeyError.
Traceback (most recent call last):
File "get_custom_json.py", line 22, in <module>
get_opsworks_details()
File "get_custom_json.py", line 18, in get_opsworks_details
print(stack['CustomJson'])
KeyError: 'CustomJson'
I'll check if I can fetch him again to see why that's happening.
[EDIT] Blind spot - if a stack has no custom json then the KeyError will occur.
Related
Trying to store/re-use cookies between code-executions, similar to this question, but using only python3's urllib.
Thanks to this answer for the process for creating a cookiejar for automatic use across urllib.request.Request calls:
cookie_jar = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie_jar))
urllib.request.install_opener(opener)
Initial searches on cookie storage led to SO questions regarding requests (the module) + cookies, but unfortunately, http.cookiejar objects cannot be pickled (below), as opposed to their requests brethren, which can:
>>> pickle.dumps(cookie_jar)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't pickle _thread.RLock objects
The other queries' results mostly pointed back the python3 docs: https://docs.python.org/3/library/http.cookiejar.html#http.cookiejar.FileCookieJar
Playing around with the FileCookieJar class led to noticing in the description: A CookieJar which can load cookies from, and *perhaps* save cookies to, a file on disk. and the associated error:
>>> FCJ=cookiejar.FileCookieJar("unversioned.cookies")
>>> FCJ.save()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/http/cookiejar.py", line 1785, in save
raise NotImplementedError()
Both of which were clarified by this answer - which suggested using LWPCookieJar (among others) instead, but I couldn't find any existing examples on how to use this class in practice.
LWPCookieJar was the answer:
Create the CookieJar + try loading from any existing cookie file:
cookie_filename = "unversioned.cookies"
cookie_jar = cookiejar.LWPCookieJar(cookie_filename)
try:
cookie_jar.load()
except FileNotFoundError as fnfe:
# No existing/adjacent cookie file
pass
opener = request.build_opener(request.HTTPCookieProcessor(cookie_jar))
request.install_opener(opener)
complete any urllib.request.Request call that populates cookies
store the cookies to file (named in the constructor):
cookie_jar.save()
And to confirm, after having saved some cookies in a previous execution-run:
prepare the cookiejar, but do not call .load()
test-request the page -> logged-out version received
call cookiejar.load()
test-request the page -> logged-in version received
I am using datetimerange to check if a date is in between two dates. Unfortunetaly I somehow got a strange error message without quitting the program or showing anything. Only when I ctrl+c it I get this error:
ValueError: time inversion found: 2021-09-02 14:48:34.796000+00:00 > 2021-08-25 12:27:20.603000+00:00
So this is the lines causing it:
try:
inRange = time_in_range(start_date, end_date, timestamp)
except:
inRange = time_in_range(end_date, start_date, timestamp)
I got the dates from elasticsearch logs and didn't get this before, so I don't know what has it caused. I even don't understand this error message.
Do you know what the problem is? THere is literally no information on the internet, so I think I ran into a bug or it is very obvious.
Thanks
I guess the issue is that range is incorrect. I can easily trigger this message - see below code:
#!/usr/bin/python3.9
from datetimerange import DateTimeRange
d1="2015-03-22T10:00:00+0900";
d2="2015-03-22T10:10:00+0900";
print(DateTimeRange(d1,d2).validate_time_inversion())
print(DateTimeRange(d2,d1).validate_time_inversion())
Output:
None
Traceback (most recent call last):
File "/home/username/py/time_inversion_ex.py", line 6, in <module>
print(DateTimeRange(d2,d1).validate_time_inversion())
File "/usr/local/lib/python3.9/dist-packages/datetimerange/__init__.py", line 272, in validate_time_inversion
raise ValueError(
ValueError: time inversion found: 2015-03-22 10:10:00+09:00 > 2015-03-22 10:00:00+09:00
this is my code
import requests
r = requests.get('https://www.reddit.com/r/Art.json').json()
print(r['data'])
this code sometimes work but sometimes get fail
Exception is
Traceback (most recent call last):
File "c:/Users/SAMET/Desktop/python/a.py", line 5, in <module>
print(r['data'])
KeyError: 'data'
You must use r.data. r['data'] could work in Javascript, but in Python you can't access objects as dictionaries.
I have code that sets up an environment for running and logging scientific experiments. Some of the initial setup involves using the built in input() method to query the user for values. I keep getting a I/O operation on closed file error whenever I try to call input however.
Code flow: Control.py calls Analyzer.py which calls a specific method in Prompts.py (the code for which is below).
def prompt_instruments(message):
res = input(message) # query user with arg message
print("done")
if '.' in res:
print("User input not cool. Use comma-separated values.")
return None # to continue prompting
...
I have searched all over the internet and have been unable to find anything remotely related. Thank you so much!!
The code you posted seems ok, and the error is probably in one of your other files.
The input() function uses sys.stdout to display the prompt text, and sys.stdin to get the user's input text.
The error message you get is probably caused by one of these files being closed, e.g.:
>>> import sys
>>> input('test: ')
test: hello
'hello'
>>> sys.stdin.close()
>>> input('test: ')
test: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
or:
>>> import sys
>>> input('test: ')
test: hi
'hi'
>>> sys.stdout.close()
>>> input('test: ')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
I can't tell you exactly where to fix this issue, but look for things that might close one of these file, either directly or indirectly (e.g. context manager).
I'm having quite a struggle trying to use Morfologik library in my project in jython. I need to use a PolishStemmer, so I imported morfologik-polish, morfologik-stemming and morfologik-fsa in jython console, using sys.path.append. However, each time I try to create a PolishStemmer object, the following situation occurs:
>>> p = PolishStemmer(PolishStemmer.DICTIONARY.COMBINED)
Traceback (most recent call last):
File "<console>", line 1, in <module>
RuntimeException: java.lang.RuntimeException: Default dictionary resource for language 'plnot found.
Now, according to the Morfologik API, this occurs only when the dictionary itself is not available. What additional .jars should I import to create and use PolishStemmer? (Note: I already tried adding morfologik-tools-standalone instead to the classpath, with no luck).