Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

God.... I hate regex..... hELP

Options
  • 25-04-2018 5:20pm
    #1
    Registered Users Posts: 1,704 ✭✭✭


    {u'NatGateway': {u'NatGatewayAddresses': [{u'PublicIp': '11.116.111.12', u'NetworkInterfaceId': 'eni-22576510', u'AllocationId': 'eipalloc-da82e9e7', u'PrivateIp': '192.168.2.172'}], u'VpcId': 'vpc-f00d6996', u'State': 'available', u'NatGatewayId': 'nat-0ec228a4a3beac32b', u'SubnetId': 'subnet-1e03b256', u'CreateTime': datetime.datetime(2018, 4, 25, 11, 26, 50, tzinfo=tzutc())}, 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'c12cf2ad-c17d-4d08-97c8-8f7ee890f0dd', 'HTTPHeaders': {'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Wed, 25 Apr 2018 15:10:34 GMT'}}, u'ClientToken': '446655440000'}


    Guys I need to pull out nat-0ec228a4a3beac32b from this response. The value will change as its randomly generated but the first part "nat-" wont. Any ideas how to match it, im going insane. Using python 2.7

    p.s. couldnt use code tags, it looked funky


Comments

  • Moderators, Recreation & Hobbies Moderators Posts: 11,429 Mod ✭✭✭✭igCorcaigh


    Can you search for anything starting with nat- and then check if the remainder is a hex value?

    ^nat- will catch every occurence of nat-xxxxx

    If you can parse off the xxx and check if it's a hex value, then you have matched it.


  • Registered Users Posts: 1,704 ✭✭✭Doylers


    OSI wrote: »
    That's a dictionary, just read the value of the key.

    I was trying to but python shat the bed. I tried:
    json.loads which gave obj, end = self.raw_decode(s, idx=_w(s, 0).end())

    then

    from ast import literal_eval
    d = literal_eval(natGW) which gave ValueError: malformed string

    Any ideas? I'm learning all this as go.(Translation im a noob)

    oh and I tried natGW to try just read the value


    Edit: Yep it boto3


  • Moderators, Recreation & Hobbies Moderators Posts: 11,429 Mod ✭✭✭✭igCorcaigh


    What does natGW result in?


  • Registered Users Posts: 1,704 ✭✭✭Doylers


    igCorcaigh wrote: »
    What does natGW result in?

    >>> natGW
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 'NatGatewayId'
    >>>

    print natGW gives the full output in post 1

    Is the dictionary a little off, since it has {} inside of {} ?


  • Moderators, Recreation & Hobbies Moderators Posts: 11,429 Mod ✭✭✭✭igCorcaigh


    Doylers wrote: »
    >>> natGW
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 'NatGatewayId'
    >>>

    print natGW gives the full output in post 1

    Is the dictionary a little off, since it has {} inside of {} ?

    Oh, possibly you might need [].

    I'm no Py expert :)


  • Advertisement
  • Registered Users Posts: 1,704 ✭✭✭Doylers


    OSI wrote: »
    OK, you don't anyway of that json rubbish :)

    So you're calling the describe_nat_gateways function?

    like:
    response = client.describe_nat_gateways()
    

    This will return a dictionary similar in structure to what you have above. The dictionary itself will have one key, who's value is a list of dictionaries for each of the NAT gateways you have.

    If you only care about the first one or you're sure it's only ever going to return a single instance, you can just use:
    natGatewayID = response['NatGateways'][0]['NatGatewayId']
    

    But you're more likely going to want to iterate over the list and note them all
    for natGateway in response['NatGateways']:
        natGatewayID = natGateway['NatGatewayId']
    

    You god among men :D Its only every going to turn up once so I'll use the first piece.

    What does [0] do? It cause a key error but removed it worked!

    >>> print natGW
    nat-0ec228a4a3beac32b


  • Registered Users Posts: 1,704 ✭✭✭Doylers


    If I could ask one more question:

    >>> waiter.wait(NatGatewayIds=natGW )
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/Users/sean/Library/Python/2.7/lib/python/site-packages/botocore/waiter.py", line 53, in wait
    Waiter.wait(self, **kwargs)
    File "/Users/sean/Library/Python/2.7/lib/python/site-packages/botocore/waiter.py", line 297, in wait
    response = self._operation_method(**kwargs)
    File "/Users/sean/Library/Python/2.7/lib/python/site-packages/botocore/waiter.py", line 84, in __call__
    return self._client_method(**kwargs)
    File "/Users/sean/Library/Python/2.7/lib/python/site-packages/botocore/client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
    File "/Users/sean/Library/Python/2.7/lib/python/site-packages/botocore/client.py", line 586, in _make_api_call
    api_params, operation_model, context=request_context)
    File "/Users/sean/Library/Python/2.7/lib/python/site-packages/botocore/client.py", line 621, in _convert_to_request_dict
    api_params, operation_model)
    File "/Users/sean/Library/Python/2.7/lib/python/site-packages/botocore/validate.py", line 291, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
    botocore.exceptions.ParamValidationError: Parameter validation failed:
    Invalid type for parameter NatGatewayIds, value: nat-01b0f5054d26e2f09, type: <type 'str'>, valid types: <type 'list'>, <type 'tuple'>
    >>>


    Is there a clean way of handling this?


  • Registered Users Posts: 1,704 ✭✭✭Doylers


    client = boto3.client('ec2')
    natGW = client.create_nat_gateway(AllocationId='eipalloc-da82e9e7', SubnetId=subnet2.id)
    waiter = client.get_waiter('nat_gateway_available')
    waiter.wait(NatGatewayIds=natGW ['NatGateway']['NatGatewayId'])
    print('Waiting for gateway Status: Available ')
    

    I have a waiting for my ec2 instances which is working fine so im confused. If you need more let me know, much appreciated


  • Registered Users Posts: 1,704 ✭✭✭Doylers


    OSI wrote: »
    Try:
    natGW = client.create_nat_gateway(AllocationId='eipalloc-da82e9e7', SubnetId=subnet2.id)
    waiter = client.get_waiter('nat_gateway_available')
    waiter.wait(NatGatewayIds=[natGW['NatGateway']['NatGatewayId']])
    print('Waiting for gateway Status: Available ')
    

    Note the [] around the natGW. The waiter is expecting a list, but you've just passed it a string. We can just create a single value list by surrounding the variable in []

    That worked perfectly :D:D:D:D Thank you


  • Registered Users Posts: 3,376 ✭✭✭Shemale


    This is a great site for learning regex :

    https://regex101.com/

    Put the content into the test string box and practice your regex in the regular expression box, on the right it gives you some info and any matches and in the bottom left you can actually generate code to paste into your program.


  • Advertisement
  • Registered Users Posts: 41 RoZZaH


    DevTips on youtube also have an explainer


Advertisement