Skip to main content
Edit summary overflow [<https://en.wiktionary.org/wiki/Pythonic#Adjective>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

This approach uses:

  • almost no internal punctuation by using a triple quoted string
  • strips away local indentation using the inspect module
  • uses Python 3.6 formatted string interpolation ('f') for the account_id and def_id variables.

This way looks the most Pythonic to me.

import inspect query = inspect.cleandoc(f''' SELECT action.descr as "action", role.id as role_id, role.descr as role FROM public.role_action_def, public.role, public.record_def, public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 

This approach uses:

  • almost no internal punctuation by using a triple quoted string
  • strips away local indentation using the inspect module
  • uses Python 3.6 formatted string interpolation ('f') for the account_id and def_id variables.

This way looks the most Pythonic to me.

import inspect query = inspect.cleandoc(f''' SELECT action.descr as "action", role.id as role_id, role.descr as role FROM public.role_action_def, public.role, public.record_def, public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 

This approach uses:

  • almost no internal punctuation by using a triple quoted string
  • strips away local indentation using the inspect module
  • uses Python 3.6 formatted string interpolation ('f') for the account_id and def_id variables.

This way looks the most Pythonic to me.

import inspect query = inspect.cleandoc(f''' SELECT action.descr as "action", role.id as role_id, role.descr as role FROM public.role_action_def, public.role, public.record_def, public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 
Active reading [<https://en.wikipedia.org/wiki/Python_%28programming_language%29>]. Removed historical information (that is what the revision history is for) - the answer should be as if it was written right now (if several versions need to be included, e.g. use separate sections in the answer)
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

This approach uses:

  • just one backslash to avoid an initial linefeed
  • almost no internal punctuation by using a triple quoted string
  • strips away local indentation using the textwrap inspect module
  • uses pythonPython 3.6 formatted string interpolation ('f') for the account_id and def_id variables.

This way looks the most pythonicPythonic to me.

# import textwrap # See update to answer below import inspect   # query = textwrap.dedent(f'''\ query = inspect.cleandoc(f''' SELECT action.descr as "action",  role.id as role_id, role.descr as role FROM  public.role_action_def, public.role, public.record_def,  public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 

Update: 1/29/2019 Incorporate @ShadowRanger's suggestion to use inspect.cleandoc instead of textwrap.dedent

This approach uses:

  • just one backslash to avoid an initial linefeed
  • almost no internal punctuation by using a triple quoted string
  • strips away local indentation using the textwrap inspect module
  • uses python 3.6 formatted string interpolation ('f') for the account_id and def_id variables.

This way looks the most pythonic to me.

# import textwrap # See update to answer below import inspect   # query = textwrap.dedent(f'''\ query = inspect.cleandoc(f''' SELECT action.descr as "action",  role.id as role_id, role.descr as role FROM  public.role_action_def, public.role, public.record_def,  public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 

Update: 1/29/2019 Incorporate @ShadowRanger's suggestion to use inspect.cleandoc instead of textwrap.dedent

This approach uses:

  • almost no internal punctuation by using a triple quoted string
  • strips away local indentation using the inspect module
  • uses Python 3.6 formatted string interpolation ('f') for the account_id and def_id variables.

This way looks the most Pythonic to me.

import inspect query = inspect.cleandoc(f''' SELECT action.descr as "action", role.id as role_id, role.descr as role FROM public.role_action_def, public.role, public.record_def, public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 
incorporate suggestion to use inspect.cleandoc instead of textwrap.dedent; improve formatting
Source Link
Christopher Bruns
  • 9.5k
  • 7
  • 49
  • 63

This approach uses just one backslash to avoid an initial linefeed, almost no internal punctuation by using a triple quoted string, strips away local indentation using the textwrap module, and also uses python 3.6 formatted string interpolation ('f') for the account_id and def_id variables.:

  • just one backslash to avoid an initial linefeed
  • almost no internal punctuation by using a triple quoted string
  • strips away local indentation using the textwrap inspect module
  • uses python 3.6 formatted string interpolation ('f') for the account_id and def_id variables.

This way looks the most pythonic to me.

# import textwrap # See update to answer below import inspect  # query = textwrap.dedent(f'''\ query = inspect.cleandoc(f'''  SELECT action.descr as "action", role.id as role_id, role.descr as role FROM public.role_action_def, public.role, public.record_def, public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 

Update: 1/29/2019 Incorporate @ShadowRanger's suggestion to use inspect.cleandoc instead of textwrap.dedent

This approach uses just one backslash to avoid an initial linefeed, almost no internal punctuation by using a triple quoted string, strips away local indentation using the textwrap module, and also uses python 3.6 formatted string interpolation ('f') for the account_id and def_id variables. This way looks the most pythonic to me.

import textwrap query = textwrap.dedent(f'''\ SELECT action.descr as "action", role.id as role_id, role.descr as role FROM public.role_action_def, public.role, public.record_def, public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 

This approach uses:

  • just one backslash to avoid an initial linefeed
  • almost no internal punctuation by using a triple quoted string
  • strips away local indentation using the textwrap inspect module
  • uses python 3.6 formatted string interpolation ('f') for the account_id and def_id variables.

This way looks the most pythonic to me.

# import textwrap # See update to answer below import inspect  # query = textwrap.dedent(f'''\ query = inspect.cleandoc(f'''  SELECT action.descr as "action", role.id as role_id, role.descr as role FROM public.role_action_def, public.role, public.record_def, public.action WHERE role.id = role_action_def.role_id AND record_def.id = role_action_def.def_id AND action.id = role_action_def.action_id AND role_action_def.account_id = {account_id} AND record_def.account_id={account_id} AND def_id={def_id}''' ) 

Update: 1/29/2019 Incorporate @ShadowRanger's suggestion to use inspect.cleandoc instead of textwrap.dedent

Source Link
Christopher Bruns
  • 9.5k
  • 7
  • 49
  • 63
Loading