Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected EOF
#1
Hi! New guy here! (I'm sure that your FAVORITE thing to read right off the top)

Anyway, I've been working on this script (my first in Python) and debugging it and I am down to my last error (I believe because of the position and error I am getting) and I know what the error means but I am unable to sus out what to do about it or what the actual cause is.

I had some indentation issues with my script when debugging, so it may stem from solving those. But basically, it is looking for some kind of "close" to the code that this is missing.

I'm sure this is stupid simple, but I am both. Thank you for looking in advance...

#Passing Vars

import yaml

# Get vpc data from yaml

with open(r"somepath\file.yaml", 'r') as file:
    testdata=yaml.safe_load(file)

vpcs=testdata['vpcs']
new_vpc_list= []
is_dmz=False
tags = vpc['Tags']
for tag in tags: 
  if tag['Value'] == 'DMZ':
    is_dmz = True
  if is_dmz:
    new_vpc_list.append(vpc)

# Get template from yaml
with open(r"someotherpath\otherfile.yml") as file:
    template = yaml.safe_load(file)
 
template_copy = template.copy()
 
# Edit the template data
template_copy['Parameters']['EnvType']['VPC'] = new_vpc_list[0]['VpcId']
 
 
# Write the template data to a new file.
with open(r"someotherpath\otherfile.yml") as file:
    file.write(yaml.dump(template_copy, file)
Yoriz write Jun-30-2021, 04:37 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
tags = vpc['Tags']
vpc is not defined

new_vpc_list.append(vpc)
vpc is not defined

file.write(yaml.dump(template_copy, file)
) missing from the end

for tag in tags: 
  if tag['Value'] == 'DMZ':
    is_dmz = True
  if is_dmz:
    new_vpc_list.append(vpc)
Use 4 spaces for indents
Reply
#3
Wow! You really are a noob if you believe in fairy tales like "the last bug"! Don't worry, you will soon be as jaded as the rest of us.

Other than there not being anything named "vpc", I'm pretty sure this code is not going to do what you want.
with open(r"somepath\file.yaml", 'r') as file:
    testdata=yaml.safe_load(file)
new_vpc_list= []
vpcs=testdata['vpcs']
is_dmz=False
tags = vpcs['Tags']
for tag in tags: 
  if tag['Value'] == 'DMZ':
    is_dmz = True
  if is_dmz:
    new_vpc_list.append(vpcs)
Not knowing exactly what you are trying to do, and not having the file, I don't know exactly how this is going to fail, but the logic is suspect. Either you are trying to make a list of all vspcs Tags where value is DMZ, or you want a list of all vspcs that have a Tags where value is DMZ. Your code does neither. Your code will find the first Tags with value = DMZ and append vpc to new_vpc_list for that Tags and for all remaining Tags. If you yaml file looks like this:
vpcs:
    Tags:
        Value: ABC
    Tags:
        Value: DMZ  # <- This is the match
    Tags:
        Value: XYZ   # But add for this one too because is_dmz is still True
    Tags:
        Value: 123
Your list will look like this:
new_vpc_list = [vpcs, vpcs, vpcs] # One for the match and two for the two remaining tags even though they don't match
If you want a list of matching tags, I think your code should look like this:
with open(r"somepath\file.yaml", 'r') as file:
    testdata=yaml.safe_load(file)
vpcs = testdata['vpcs']
dmz_tags = []
for tag in vpcs['Tags']: 
    if tag['Value'] == 'DMZ':
        dmz_tags.append(tag)
Or use a list comprehension to shorten things up.
with open(r"somepath\file.yaml", 'r') as file:
    vpcs = yaml.safe_load(file)['vpcs']
dmz_tags = [tag for tag in vpcs['Tags'] if tag['Value'] == 'DMZ']
Reply
#4
(Jul-01-2021, 02:21 PM)deanhystad Wrote: Wow! You really are a noob if you believe in fairy tales like "the last bug"! Don't worry, you will soon be as jaded as the rest of us.

Yes I am! I'm sure a few more exercises like this will beat the dopey optimism from my spirit.

The purpose of the script is to find and copy the VPC ID where the tag is set for DMZ. Out of any number of VPC's in the file, only one would have that tag. I have several other kinds of files to copy a value from based upon a single tag or other unique value to be copied into another file.

This is a way of automagically building stuff by knowing the least amount possible for the stuff anyone wants to build in the org.

So the process would be open and load a file, identify a unique value (tag or attribute or property) of an item. Copy the unique ID of that item based upon the unique value. Open and load a different file (yaml template). Paste that unique ID into a specific place in the yaml template at a specific spot based upon a value. Repeat steps for each item required by the yaml template which will come from different source files. Save the yaml template as something else.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020