Python Forum

Full Version: Are braces rather than indentation ever wanted?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I have a small application that can convert indented python3 code into the equivalent code with braces {} and back again to indented python.

Here is an example of aby.py from my python3 windows installation.

It needs more work (the output needs better layout of spaces etc) but in principal it works. 

I can see it being useful to people with C++/JAVA background that prefer to work in the braces style.

Also, I noticed a couple of days ago this link http://python-with-braces.appspot.com/

It seems to require a complete installation of python 2.x? What I'm proposing doesn't require changes to installation. But maybe my convertor could work alongside it, does anyone use it?


I'd like to develop it further but I don't know what interest there would be in it, it's only a sideline in what I'm currently doing but it does interest me.. 


"""Abstract Base Classes (ABCs) according to PEP 3119.""" ;
from _weakrefset import    WeakSet  ;
def abstractmethod (
   funcobj
) :
{
 funcobj . __isabstractmethod__ = True ;
 return    funcobj ;
}
;
class abstractclassmethod (   classmethod ) :
{
 __isabstractmethod__ = True ;
 def __init__ (
    self
 ,  callable
 ) :
 {
  callable . __isabstractmethod__ = True ;
  super ( ) . __init__ ( callable ) ;
 }
 ;
}
;
class abstractstaticmethod (   staticmethod ) :
{
 __isabstractmethod__ = True ;
 def __init__ (
    self
 ,  callable
 ) :
 {
  callable . __isabstractmethod__ = True ;
  super ( ) . __init__ ( callable ) ;
 }
 ;
}
;
class abstractproperty (   property ) :
{
 __isabstractmethod__ = True ;
}
;
class ABCMeta (   type ) :
{
 _abc_invalidation_counter = 0 ;
 def __new__ (
    mcls
 ,  name
 ,  bases
 ,  namespace
 
 ) :
 {
  cls = super ( ) . __new__ ( mcls , name , bases , namespace ) ;
  abstracts = { name for name , value in namespace . items ( ) if getattr ( value , "__isabstractmethod__" , False ) } ;
  for    base in    bases :
  {
   for    name in    getattr ( base , "__abstractmethods__" , set ( ) ) :
   {
    value = getattr ( cls , name , None ) ;
    if getattr ( value , "__isabstractmethod__" , False ) :
    {
     abstracts . add ( name ) ;
    }
    ;
   }
   ;
  }
  ;
  cls . __abstractmethods__ = frozenset ( abstracts ) ;
  cls . _abc_registry = WeakSet ( ) ;
  cls . _abc_cache = WeakSet ( ) ;
  cls . _abc_negative_cache = WeakSet ( ) ;
  cls . _abc_negative_cache_version = ABCMeta . _abc_invalidation_counter ;
  return    cls ;
 }
 ;
 def register (
    cls
 ,  subclass
 
 ) :
 {
  if not isinstance ( subclass , type ) :
  {
   raise TypeError ( "Can only register classes" )  ;
  }
  ;
  if issubclass ( subclass , cls ) :
  {
   return    subclass ;
  }
  ;
  if issubclass ( cls , subclass ) :
  {
   raise RuntimeError ( "Refusing to create an inheritance cycle" )  ;
  }
  ;
  cls . _abc_registry . add ( subclass ) ;
  ABCMeta . _abc_invalidation_counter += 1 ;
  return    subclass ;
 }
 ;
 def _dump_registry (
    cls
 , file = None
 
 ) :
 {
  """Debug helper to print the ABC registry.""" ;
  print ( "Class: %s.%s" % ( cls . __module__ , cls . __qualname__ ) , file = file ) ;
  print ( "Inv.counter: %s" % ABCMeta . _abc_invalidation_counter , file = file ) ;
  for    name in    sorted ( cls . __dict__ . keys ( ) ) :
  {
   if name . startswith ( "_abc_" ) :
   {
    value = getattr ( cls , name ) ;
    print ( "%s: %r" % ( name , value ) , file = file ) ;
   }
   ;
  }
  ;
 }
 ;
 def __instancecheck__ (
    cls
 ,  instance
 
 ) :
 {
  """Override for isinstance(instance, cls).""" ;
  subclass = instance . __class__ ;
  if subclass in cls . _abc_cache :
  {
   return    True ;
  }
  ;
  subtype = type ( instance ) ;
  if subtype is subclass :
  {
   if ( cls . _abc_negative_cache_version == ABCMeta . _abc_invalidation_counter and subclass in cls . _abc_negative_cache ) :
   {
    return    False ;
   }
   ;
   return    cls . __subclasscheck__ ( subclass ) ;
  }
  ;
  return    any ( cls . __subclasscheck__ ( c ) for c in { subclass , subtype } ) ;
 }
 ;
 def __subclasscheck__ (
    cls
 ,  subclass
 
 ) :
 {
  """Override for issubclass(subclass, cls).""" ;
  if subclass in cls . _abc_cache :
  {
   return    True ;
  }
  ;
  if cls . _abc_negative_cache_version < ABCMeta . _abc_invalidation_counter :
  {
   cls . _abc_negative_cache = WeakSet ( ) ;
   cls . _abc_negative_cache_version = ABCMeta . _abc_invalidation_counter ;
  }
  elif subclass in cls . _abc_negative_cache :
  {
   return    False ;
  }
  ;
  ok = cls . __subclasshook__ ( subclass ) ;
  if ok is not NotImplemented :
  {
   assert isinstance ( ok , bool )  ;
   if ok :
   {
    cls . _abc_cache . add ( subclass ) ;
   }
   else :
   {
    cls . _abc_negative_cache . add ( subclass ) ;
   }
   ;
   return    ok ;
  }
  ;
  if cls in getattr ( subclass , '__mro__' , ( ) ) :
  {
   cls . _abc_cache . add ( subclass ) ;
   return    True ;
  }
  ;
  for    rcls in    cls . _abc_registry :
  {
   if issubclass ( subclass , rcls ) :
   {
    cls . _abc_cache . add ( subclass ) ;
    return    True ;
   }
   ;
  }
  ;
  for    scls in    cls . __subclasses__ ( ) :
  {
   if issubclass ( subclass , scls ) :
   {
    cls . _abc_cache . add ( subclass ) ;
    return    True ;
   }
   ;
  }
  ;
  cls . _abc_negative_cache . add ( subclass ) ;
  return    False ;
 }
 ;
}
;
class ABC (   metaclass = ABCMeta ) :
{
 pass ;
}
;
def get_cache_token (
) :
{
 return    ABCMeta . _abc_invalidation_counter ;
}
;
  • I think that quite a few people actually like Python as it is - besides, it enforces indent discipline. And I have seen code with lousy indents - in C, bash, and more.
  • Python 3.x is the future
  • I hated the way your code looks (OK, that's me). Part of the beauty of Python - it's concise.
  • "I don't like how the code looks" is a poor excuse to mangle popular and successful language, again - my personal opinion
  • If you ever use Python professionally, and try this stint - you'll lose your job quickly

My 2 cents. I personally like Python style much more than C style.
nix nix (on braces) (from long time C and C++ programmer)
Volcano63

The braces view is purely for anyone wanting to view/edit raw python in braces format. Nobody else would know that a user has done that. The files are saved as indented python when the user is finished. My boss would never find out.
(Apr-30-2017, 04:00 PM)michaelcollier Wrote: [ -> ]Volcano63

The braces view is purely for anyone wanting to view/edit raw python in braces format.
  • Will it run in any decent IDE?
  • How easy it will be to debug?
  • If you get used to that abomination Hand - how could you be successful receiver - or contributor - in code reviews?

Of course, it's your funeral Naughty - but for me personally Python style after several years of C/C++ was a source of joy Heart
(Apr-30-2017, 03:21 PM)michaelcollier Wrote: [ -> ]does anyone use it?
No do not want braces at all Angry

Side bye side of your mess and Python code Wink
[Image: b4IEAc.jpg]
(Apr-30-2017, 04:07 PM)snippsat Wrote: [ -> ]Side bye side of your mess and Python code Wink

Size matters Dance
I understand what you mean, the layout I posted uses the most amount of lines because {} get their own lines and so does a semicolon. Each parameter is also given its own line. If I changed the output format (a feature I haven't finished yet) then it would be more concise.
Though I see this as a way to teach yourself Python, I don't see any practical use for it.
I guess the consensus among python programmers is that they don't want braces. I use both methods myself, braces in my own editor, indentation in IDLE. Most of my programming is for microcontrollers so I'm more C/C++ oriented. Hitting the keyboard with {}; is instinctive to me, whereas indentation causes me to slow down a bit. I first got the idea of offering braces when I found this "from __future__ import braces" so I thought I'd give it a go. Thanks for your replies..
Pages: 1 2 3