![]() |
Are braces rather than indentation ever wanted? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Are braces rather than indentation ever wanted? (/thread-3128.html) |
Are braces rather than indentation ever wanted? - michaelcollier - Apr-30-2017 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 ; } ; RE: Are braces rather than indentation ever wanted? - volcano63 - Apr-30-2017
My 2 cents. I personally like Python style much more than C style. RE: Are braces rather than indentation ever wanted? - Larz60+ - Apr-30-2017 nix nix (on braces) (from long time C and C++ programmer) RE: Are braces rather than indentation ever wanted? - michaelcollier - Apr-30-2017 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. RE: Are braces rather than indentation ever wanted? - volcano63 - Apr-30-2017 (Apr-30-2017, 04:00 PM)michaelcollier Wrote: Volcano63
Of course, it's your funeral ![]() ![]() RE: Are braces rather than indentation ever wanted? - snippsat - Apr-30-2017 (Apr-30-2017, 03:21 PM)michaelcollier Wrote: does anyone use it?No do not want braces at all ![]() Side bye side of your mess and Python code ![]() [Image: b4IEAc.jpg] RE: Are braces rather than indentation ever wanted? - volcano63 - Apr-30-2017 (Apr-30-2017, 04:07 PM)snippsat Wrote: Side bye side of your mess and Python code Size matters ![]() RE: Are braces rather than indentation ever wanted? - michaelcollier - Apr-30-2017 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. RE: Are braces rather than indentation ever wanted? - sparkz_alot - Apr-30-2017 Though I see this as a way to teach yourself Python, I don't see any practical use for it. RE: Are braces rather than indentation ever wanted? - michaelcollier - Apr-30-2017 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.. |