-5

I'm running some python as a single-line script, but getting the following error

>>> import ipaddress; (ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32')) ? raise Exception('xxx') : pass;
  File "<stdin>", line 1
    import ipaddress; (ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32')) ? raise Exception('xxx') : pass;
                                                                                                ^
SyntaxError: invalid syntax

I've also tried:

>>> import ipaddress; raise Exception('xxx') if (ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32'));
  File "<stdin>", line 1
    import ipaddress; raise Exception('xxx') if (ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32'));
                                                                                                                         ^
SyntaxError: invalid syntax

Why is this invalid syntax? Is there an alternative way that I can achieve the same end goal?

I'm running this code from a third party tool (terraform) so it has to be a single-line script.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Chris Snow
  • 23,813
  • 35
  • 144
  • 309

5 Answers5

2

Python doesn't have a ternary operator with ? as such

Equivalent:

condition_if_true if condition else condition_if_false
rdas
  • 20,604
  • 6
  • 33
  • 46
1

The correct syntax here for ternary operations in Python is

a if condition else b

but note that b here is an expression, not an arbitrary Python statement. You can't use raise or pass in a conditional expression.

If you want to raise an exception when the condition is false, the shortest you can get your code is:

import ipaddress
if not ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32'): raise Exception('xxx')
glhr
  • 4,439
  • 1
  • 15
  • 26
1

You can’t raise an exception with the ternary operator, since it deals with expressions, and raise is a statement. This seems like it would work, but also raises a syntax error.

import ipaddress; if ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32'): raise Exception('xxx')

So the closest you can get is probably this (which @glhr already suggested):

import ipaddress
if ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32'): raise Exception('xxx')

or this (which also shows the correct syntax for Python's ternary operator):

def raise_error(err):
    raise err

import ipaddress; raise_error(Exception('xxx')) if ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32') else None

You may also find some useful ideas here (turns out they also thought of my second idea): raise statement on a conditional expression

Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
0

I assume you are trying to achieve this.

import ipaddress
try:
    ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32')
except:
    raise Exception('xxx')

It's one liner will be for throwing exception will be

if ipaddress.IPv4Address('1.1.1.1') in ipaddress.IPv4Network('1.1.1.1/32'): raise Exception('xxx')
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0

This is a really bad way of doing it. But everything is on one line. You are importing the module every time you need to use.

if not __import__("ipaddress").IPv4Address('1.1.1.1') in __import__("ipaddress").IPv4Network('1.1.1.1/32'): raise Exception('xxx')
Alve
  • 640
  • 2
  • 9
  • 19