Created
May 23, 2019 01:38
-
-
Save xtornasol512/217eb49788a02e62c47a6c40d8fec073 to your computer and use it in GitHub Desktop.
Mas alla de las guias de estilo | Talk 22 May , 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Pythonic means "coding beautifully in harmony with the language to get the | |
maximum benefits from python" | |
Learn to recognize non-pythonic APIs and to recognize good code. | |
Dont get distracted by PEP8. Focus first on Pythonic vs NonPythonic(P vs NP) | |
When needed, write an adapter class to convert from the former to the latter | |
========================================================== | |
""" | |
# Adapter Pattern Class | |
import jnettool.tools.elements.NetworkElement | |
import jnettool.tools.Routing | |
class NetworkElementError(Exception): | |
pass | |
class NetworkElement: | |
def __init__(self, ipaddr): | |
self.ipaddr = ipaddr | |
self.oldne = jnettool.tools.elements.NetworkElement(ipaddr) | |
@property | |
def routing_table(self): | |
try: | |
return RoutingTable(self.oldne.getRoutingTable()) | |
except jnettool.tools.elements.MissingVar: | |
raise NetworkElementError("No routing table") | |
def __enter__(self): | |
return self | |
def __exit__(self, exctype, excinst, exctb): | |
if exctype == NetworkElementError: | |
logging.exception("No routing table form") | |
self.oldne.cleanup("rollback") | |
else: | |
self.oldne.cleanup("commit") | |
return self.oldne.disconnect() | |
class RoutingTable: | |
def __init__(self, oldrt): | |
self.oldrt = oldrt | |
def __len__(self): | |
return self.oldrt.getSize() | |
def __getitem__(self, index): | |
if index >= len(self): | |
raise IndexError | |
return Route(self.oldrt.getRouteByIndex(index)) | |
class Route: | |
def __init__(self, old_route): | |
self.old_route = old_route | |
@property | |
def name(self): | |
return self.old_route.getName() | |
@property | |
def ipaddr(self): | |
return self.old_route.getIPAddr() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from nettools import NetworkElement | |
with NetworkElement("171.0.2.45") as ne: | |
for route in ne.routing_table: | |
print ("%15s -> %s" % (route.name, route.ipaddr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jnettool.tools.elements.NetworkElement, \ | |
jnettool.tools.Routing, \ | |
jnettool.tools.RouteInspector | |
ne=jnettool.tools.elements.NetworkElement( "171.0.2.45" ) | |
try: | |
routing_table=ne.getRoutingTable() # fetch table | |
except jnettool.tools.elements.MissingVar: | |
# Record table fault | |
logging.exception( ''' No routing table found''' ) | |
ne.cleanup( ''' rollback ''' ) | |
else: | |
num_routes=routing_table.getSize() # determinate table size | |
for RToffset in range( num_routes ): | |
route=routing_table.getRouteByIndex( RToffset ) | |
name=route.getName() # route name | |
ipaddr=route.getIPAddr() # ip address | |
print( "%15s -> %s" % (name, ipaddr) ) # Format nicely | |
finally: | |
ne.cleanup( ''' commit ''' ) # lockin changes | |
ne.disconnect() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
El codigo fue tomado de este video https://www.youtube.com/watch?v=wf-BqAjZb8M&t=2137s
PDF de la presentacion https://www.dropbox.com/s/f4du53jqxkf1e9v/Beyong_style_guides.pdf?dl=0