Skip to content

Commit

Permalink
ccxt#106 transpiler edits for Python PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed Aug 13, 2017
1 parent d337742 commit 83394b6
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ while (exchanges = regex.exec (contents)) {
py.push ('')
py.push ('class ' + id + ' (' + (parent ? parent : 'Exchange') + '):')
py.push ('')
py.push (' def __init__ (self, config = {}):')
py.push (' def __init__(self, config = {}):')
py.push (' params = {')
py.push (' ' + params.join ("\n ").replace (/ \/\//g, ' #') + ((all.length > 1) ? ',' : ''))
py.push (' }')
Expand Down Expand Up @@ -106,70 +106,71 @@ while (exchanges = regex.exec (contents)) {
[ /typeof\s+([^\s\[]+)(?:\s|\[(.+?)\])\s+\=\=\s+\'undefined\'/g, '$1[$2] is None' ],
[ /undefined/g, 'None' ],
[ /this\.stringToBinary\s*\((.*)\)/g, '$1' ],
[ /this\.stringToBase64/g, 'base64.b64encode' ],
[ /this\.base64ToBinary/g, 'base64.b64decode' ],
[ /\.binaryConcat/g, '.binary_concat'],
[ /\.binaryToString/g, '.binary_to_string' ],
[ /\.implodeParams/g, '.implode_params'],
[ /\.extractParams/g, '.extract_params'],
[ /\.parseTicker/g, '.parse_ticker'],
[ /\.parseTrades/g, '.parse_trades'],
[ /\.parseTrade/g, '.parse_trade'],
[ /\.parseBidAsks/g, '.parse_bidasks'],
[ /\.parseBidAsk/g, '.parse_bidask'],
[ /\.indexBy/g, '.index_by'],
[ /\.sortBy/g, '.sort_by'],
[ /\.marketId/g, '.market_id'],
[ /this\.stringToBase64\s/g, 'base64.b64encode' ],
[ /this\.base64ToBinary\s/g, 'base64.b64decode' ],
[ /\.binaryConcat\s/g, '.binary_concat'],
[ /\.binaryToString\s/g, '.binary_to_string' ],
[ /\.implodeParams\s/g, '.implode_params'],
[ /\.extractParams\s/g, '.extract_params'],
[ /\.parseTicker\s/g, '.parse_ticker'],
[ /\.parseTrades\s/g, '.parse_trades'],
[ /\.parseTrade\s/g, '.parse_trade'],
[ /\.parseBidAsks\s/g, '.parse_bidasks'],
[ /\.parseBidAsk\s/g, '.parse_bidask'],
[ /\.indexBy\s/g, '.index_by'],
[ /\.sortBy\s/g, '.sort_by'],
[ /\.marketId\s/g, '.market_id'],
[ /this\.urlencode\s/g, '_urlencode.urlencode ' ],
[ /this\./g, 'self.' ],
[ /([^a-zA-Z])this([^a-zA-Z])/g, '$1self$2' ],
[ /([^a-zA-Z0-9_])let\s\[\s*([^\]]+)\s\]/g, '$1$2' ],
[ /([^a-zA-Z0-9_])let\s/g, '$1' ],
[ /Object\.keys\s*\((.*)\)\.length/g, '$1' ],
[ /Object\.keys\s*\((.*)\)/g, 'list ($1.keys ())' ],
[ /\[([^\]]+)\]\.join\s*\(([^\)]+)\)/g, "$2.join ([$1])" ],
[ /hash \(([^,]+)\, \'(sha[0-9])\'/g, "hash ($1, '$2'" ],
[ /hmac \(([^,]+)\, ([^,]+)\, \'(md5)\'/g, 'hmac ($1, $2, hashlib.$3' ],
[ /hmac \(([^,]+)\, ([^,]+)\, \'(sha[0-9]+)\'/g, 'hmac ($1, $2, hashlib.$3' ],
[ /throw new ([\S]+) \((.*)\)/g, 'raise $1 ($2)'],
[ /Object\.keys\s*\((.*)\)/g, 'list($1.keys())' ],
[ /\[([^\]]+)\]\.join\s*\(([^\)]+)\)/g, "$2.join([$1])" ],
[ /hash \(([^,]+)\, \'(sha[0-9])\'/g, "hash($1, '$2'" ],
[ /hmac \(([^,]+)\, ([^,]+)\, \'(md5)\'/g, 'hmac($1, $2, hashlib.$3' ],
[ /hmac \(([^,]+)\, ([^,]+)\, \'(sha[0-9]+)\'/g, 'hmac($1, $2, hashlib.$3' ],
[ /throw new ([\S]+) \((.*)\)/g, 'raise $1($2)'],
[ /(\s)await(\s)/g, '$1' ],
[ /([\s\(])extend(\s)/g, '$1self.extend$2' ],
[ /\} else if/g, 'elif' ],
[ /if\s+\((.*)\)\s+\{/g, 'if $1:' ],
[ /if\s+\((.*)\)\s*[\n]/g, "if $1:\n" ],
[ /\}\s*else\s*\{/g, 'else:' ],
[ /else\s*[\n]/g, "else:\n" ],
[ /for\s+\(([a-zA-Z0-9_]+)\s*=\s*([^\;\s]+\s*)\;[^\<\>\=]+(?:\<=|\>=|<|>)\s*(.*)\.length\s*\;[^\)]+\)\s*{/g, 'for $1 in range ($2, len ($3)):'],
[ /for\s+\(([a-zA-Z0-9_]+)\s*=\s*([^\;\s]+\s*)\;[^\<\>\=]+(?:\<=|\>=|<|>)\s*(.*)\.length\s*\;[^\)]+\)\s*{/g, 'for $1 in range($2, len($3)):'],
[ /\s\|\|\s/g, ' or ' ],
[ /\s\&\&\s/g, ' and ' ],
[ /\!([^\=])/g, 'not $1'],
[ /([^\s]+)\.length/g, 'len ($1)' ],
[ /\.push\s*\(([\s\S]+?)\);/g, '.append ($1);' ],
[ /([^\s]+)\.length/g, 'len($1)' ],
[ /\.push\s*\(([\s\S]+?)\);/g, '.append($1);' ],
[ /^\s*}\s*[\n]/gm, '' ],
[ /;/g, '' ],
[ /\.toUpperCase/g, '.upper' ],
[ /\.toLowerCase/g, '.lower' ],
[ /JSON\.stringify/g, 'json.dumps' ],
[ /parseFloat\s/g, 'float '],
[ /parseInt\s/g, 'int '],
[ /self\[([^\]+]+)\]/g, 'getattr (self, $1)' ],
[ /\.toUpperCase\s*/g, '.upper' ],
[ /\.toLowerCase\s*/g, '.lower' ],
[ /JSON\.stringify\s*/g, 'json.dumps' ],
[ /parseFloat\s*/g, 'float'],
[ /parseInt\s*/g, 'int'],
[ /self\[([^\]+]+)\]/g, 'getattr(self, $1)' ],
[ /([^\s]+).slice \(([^\,\)]+)\,\s?([^\)]+)\)/g, '$1[$2:$3]' ],
[ /([^\s]+).slice \(([^\)\:]+)\)/g, '$1[$2:]' ],
[ /Math\.floor\s*\(([^\)]+)\)/g, 'int (math.floor ($1))' ],
[ /Math\.abs\s*\(([^\)]+)\)/g, 'abs ($1)' ],
[ /Math\.round\s*\(([^\)]+)\)/g, 'int (round ($1))' ],
[ /Math\.floor\s*\(([^\)]+)\)/g, 'int(math.floor($1))' ],
[ /Math\.abs\s*\(([^\)]+)\)/g, 'abs($1)' ],
[ /Math\.round\s*\(([^\)]+)\)/g, 'int(round($1))' ],
[ /(\([^\)]+\)|[^\s]+)\s*\?\s*(\([^\)]+\)|[^\s]+)\s*\:\s*(\([^\)]+\)|[^\s]+)/g, '$2 if $1 else $3'],
[/ \/\//g, ' #' ],
[ /\.indexOf/g, '.find'],
[ /\strue/g, ' True'],
[ /\sfalse/g, ' False'],
[ /\(([^\s]+)\sin\s([^\)]+)\)/g, '($1 in list ($2.keys ()))' ],
[ /([^\s]+\s*\(\))\.toString \(\)/g, 'str ($1)' ],
[ /([^\s]+)\.toString \(\)/g, 'str ($1)' ],
[ /([^\s]+)\.join\s*\(\s*([^\)\[\]]+?)\s*\)/g, '$2.join ($1)' ],
[ /Math\.(max|min)/g, '$1' ],
[ /console\.log/g, 'print'],
[ /process\.exit\s+\(\)/g, 'sys.exit ()'],
[ /\(([^\s]+)\sin\s([^\)]+)\)/g, '($1 in list($2.keys()))' ],
[ /([^\s]+\s*\(\))\.toString\s+\(\)/g, 'str($1)' ],
[ /([^\s]+)\.toString \(\)/g, 'str($1)' ],
[ /([^\s]+)\.join\s*\(\s*([^\)\[\]]+?)\s*\)/g, '$2.join($1)' ],
[ /Math\.(max|min)\s/g, '$1' ],
[ /console\.log\s/g, 'print'],
[ /process\.exit\s+/g, 'sys.exit'],
[ /\s+\(/g, '(' ], // remove whitespaces before round brackets
]

let phRegex = [
Expand Down Expand Up @@ -234,8 +235,8 @@ while (exchanges = regex.exec (contents)) {
[ /\(([^\s]+)\sin\s([^\)]+)\)/g, '(array_key_exists ($1, $2))' ],
[ /([^\s]+)\.join\s*\(\s*([^\)]+?)\s*\)/g, 'implode ($2, $1)' ],
[ /Math\.(max|min)/g, '$1' ],
[ /console\.log/g, 'var_dump'],
[ /process\.exit\s+\(\)/g, 'exit ()'],
[ /console\.log\s+/g, 'var_dump'],
[ /process\.exit\s+/g, 'exit'],
]

let pyBody = regexAll (body, pyRegex)
Expand Down

0 comments on commit 83394b6

Please sign in to comment.