Breaking Free with Rope

Vuay Kumar B.
Euild a »GFRS -= - GPS ilehicle Tracking Sgvstem.
‘ELEM. -i: L!. *i‘. |. ‘ii’: m Tfiiii

GL865-DUAL

/  O
Tellt

 

JJJ. ..'J. J Ll.  J.  LI.  3'.  

KLLI.  

'TI"i'TT" ’TI...
:I.1:: I . ‘“.   afii [-i- g ‘,5 A
[:4 '-I ' ‘ ,   i
ll . “ . "mI;  i“ 3 ~

V'"V _ _V 3.
u_: | . - ‘zgsni,  i

‘I O _ ‘II! ...
ir*irsrisi: ,v; jgx:4! mini u: -u'ii. i7i: Jui g1!| Ti, ii1lili. i7ilQ'/ lsi
Hacia    Hacia    Hack    I*. n~" one fine (. i»: ;_‘, ’,
Crash
F
I I

lieaa the

 

3     print?  iviaxz.  unique
W J < J

O O
vrarlaiile namesls 3
UNIQUE VARIABLE NAMES

Sample Code

def grayscale(red,  green,  blue): 
sum =  red + green + blue
average =  sum /  3
retu...
UNIQUE VARIABLE NAMES (CONTD. )

Sample Code

def grayscale(red,  green,  blue): 
sum =  red + green + blue
average =  sum...
REDUCING VARIABLE USAGE

Sample Code

def ’g1r: .ays. c,a7'l: e(red,  green,  blue): 
la ‘=  red + green + blue
lb =  la /...
Local
Variable
Count

 ’
No.  of Functions
rope.
me Ill .  ‘I:  3 3 rrra V r '1':  Q
: *.reraclorrng| rmry_ I-I . : yur. .. A
Re-ogranizing code,  1 'i‘tIr: ::‘i clrmging the
in'terf': :ce
:-. m:13 the : ther 'tIlin3:,  Rope can rename
variables‘-
SCRIPT T0 RENAME VARIABLES

Renamer

from rope. base. project import Project
from rope. refactor. rename import Rename

pr...
0
Va  a  to be renamed,  is
specified using an
lviechanism to identity the o‘ffse‘t ofvarial; |e
cie'iini'tionsl-
Get help tram Python‘:  parser.
PARSE TREE
pgm. py -:  Parse —:  Emit Byte Code -:  pgm. pyc

Dump Parse Tree

>>> import ast

>>> tree =  ast. parse(open...
Function Body

>>> func =  tree. body[0]

>>> print func. 
[<_ast. Assign
>>> print func. 
[<_ast. Hame obj
>>> print func...
ALGORITHM

Rename local variables in Function

nane_index =  0

assign_nodes =  find_assignment_nodes()

for node in assig...
lvlore Savings
E . ... » 7 ) IILE
I uncu. .n . ".r gu: 'nent_
0_ [I] 1' III! ’ “-‘l)'; '_> ‘. I
Fur-me Funcu. .n- . .. I-I...
Breaking Free with Rope
Breaking Free with Rope
Breaking Free with Rope
Breaking Free with Rope
Breaking Free with Rope
Breaking Free with Rope
Upcoming SlideShare
Loading in …5
×

Breaking Free with Rope

480 views

Published on

Embedded systems have crazy restrictions. And one embedded Python interpreter I happened to work on, had a restriction that you couldn't have more that 500 distinct variable names in your application! We quickly hit this limit. This talk will cover how we overcame this restriction using Rope, a refactoring library for Python and some Python scripting hackery.

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
480
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Breaking Free with Rope

  1. 1. Breaking Free with Rope Vuay Kumar B.
  2. 2. Euild a »GFRS -= - GPS ilehicle Tracking Sgvstem.
  3. 3. ‘ELEM. -i: L!. *i‘. |. ‘ii’: m Tfiiii GL865-DUAL / O Tellt JJJ. ..'J. J Ll. J. LI. 3'. KLLI. 'TI"i'TT" ’TITi r“rn"n
  4. 4. :I.1:: I . ‘“. afii [-i- g ‘,5 A [:4 '-I ' ‘ , i ll . “ . "mI; i“ 3 ~ V'"V _ _V 3. u_: | . - ‘zgsni, i ‘I O _ ‘II! II alt‘-yr a-1 . - 1- . !,I. .., .. . ~,. ,_ E; I. I Ix? I | ... . , . . ._. .“, .., ... J. ‘ [I . Lu. n.iiI. . L_, _;; ” 3 powered a= ‘I Q _ . r V‘! » , .k. I) , ‘I II I val Ina. ; NJ
  5. 5. ir*irsrisi: ,v; jgx:4! mini u: -u'ii. i7i: Jui g1!| Ti, ii1lili. i7ilQ'/ lsi
  6. 6. Hacia Hacia Hack I*. n~" one fine (. i»: ;_‘, ’,
  7. 7. Crash
  8. 8. F I I lieaa the 3 print? iviaxz. unique W J < J O O vrarlaiile namesls 3
  9. 9. UNIQUE VARIABLE NAMES Sample Code def grayscale(red, green, blue): sum = red + green + blue average = sum / 3 return average Local variables added: 2 SUIII, average
  10. 10. UNIQUE VARIABLE NAMES (CONTD. ) Sample Code def grayscale(red, green, blue): sum = red + green + blue average = sum / 3 return average def to_hex(red, green, blue): red_str = "%02x" °’o red green_str = ''‘’/ ..02x'' "/0 green blue_str = ”°’o02X" "4. blue return "#" + red_str + green_str + blue_str Local variables added: 5 sum, average, red_str, green_str, b| ue_str.
  11. 11. REDUCING VARIABLE USAGE Sample Code def ’g1r: .ays. c,a7'l: e(red, green, blue): la ‘= red + green + blue lb = la / 3 return lb def -1:9._h‘e; xfl( red, green, blue): 1a = f. '‘_%()2X'-' 95 red lb = ‘V"‘. _’/ ..3E22,§<; " 9: green lc = "°<; @2>$"’ 95 blue return “"#" + la + lb + lc Local variables added: 3 la, lb, Ic.
  12. 12. Local Variable Count ’ No. of Functions
  13. 13. rope.
  14. 14. me Ill . ‘I: 3 3 rrra V r '1': Q : *.reraclorrng| rmry_ I-I . : yur. .. A
  15. 15. Re-ogranizing code, 1 'i‘tIr: ::‘i clrmging the in'terf': :ce
  16. 16. :-. m:13 the : ther 'tIlin3:, Rope can rename variables‘-
  17. 17. SCRIPT T0 RENAME VARIABLES Renamer from rope. base. project import Project from rope. refactor. rename import Rename proj_dir = “code” project = Project(proj_dir) module = project. pycore. find_module(”colors“) é Offset of The uariable ‘éunfl renamer Rename(project, module, 37) changes renamer. get_changes(“la“) project. do(changes) II II # Offset of the valiahle ‘average’ renamer Rename(project, module, 67) changes renamer. get_changes(“lb”) project. do(changes) II II
  18. 18. 0 Va a to be renamed, is specified using an
  19. 19. lviechanism to identity the o‘ffse‘t ofvarial; |e cie'iini'tionsl-
  20. 20. Get help tram Python‘: parser.
  21. 21. PARSE TREE pgm. py -: Parse —: Emit Byte Code -: pgm. pyc Dump Parse Tree >>> import ast >>> tree = ast. parse(open("colors. py”). read()) >>> print tree. body [<_ast. FunctionDef . ..>, _ast. FunctionDef . ..>] >>> print tree. body[0]. name grayscale >>> print tree. body[1]. name to_hex >>> print tree. body[0]. body [<_ast. Assign . ..= , i_ast. Assign . .. , _ast. Return . ..>]
  22. 22. Function Body >>> func = tree. body[0] >>> print func. [<_ast. Assign >>> print func. [<_ast. Hame obj >>> print func. sum >>> 2 >>> 4 >>> print func. average >>> print func 3 >>> print func 4 print func. print func. body . ..>, <_ast. Assign . . body[0]. targets ect . ..>] body[0]. targets[O] body[0]. targets[0]. body[0]. targets[0]. body[l]. targets[0]. .body[l]. targets[0]. . body[l]. targets[0] . id lineno col_offset id lineno . col_offset . >, <_ast. Return . .. PARSE rncc (conrn) >1
  23. 23. ALGORITHM Rename local variables in Function nane_index = 0 assign_nodes = find_assignment_nodes() for node in assign_nodes: offset = get_assign_target_o‘Ffset(node) renane_target(offset, "_l’cd" 95 name_index)
  24. 24. lvlore Savings E . ... » 7 ) IILE I uncu. .n . ".r gu: 'nent_ 0_ [I] 1' III! ’ “-‘l)'; '_> ‘. I Fur-me Funcu. .n- . .. I-I-. |UIL‘ 1.“ air‘: A! ’ ‘I 5-‘: A4. I: II*'; -l3‘ II-! .3I. ;. I I-I-. |UI€ N I. " all)“ )‘l. lll 1.! ’ | l)n‘I 4-“ca, M‘ an I‘‘II"; -l. c. . .ll. II! .Ul. .;. ul :3— , an Fr‘iv3'te lvretl‘ "I: :'f: = “Tla “ Non {nu

×