testing code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: testing code (/thread-299.html) |
testing code - Skaperen - Oct-05-2016 i am testing indents and stuff: from sys import argv def main(): from sys import argv if len(argv) < 2: print 'no argument was given' return if argv[1] == 'x': print 'the argument is x' return else: print 'the argument is not x' return print 'we are done' return main()something is appending 2 empty lines (3 newlines) when i paste in some code. but the indents look right so far. looks good in preview, so i will try bigsqrt.py (again): #!/usr/bin/python # -*- coding: ISO-8859-1 -*- #----------------------------------------------------------------------------- # Copyright � 2009 - Philip Howard - All rights reserved # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #----------------------------------------------------------------------------- # program sqrt_big # # purpose Calculate lots of digits for the square root of a given number # using a scaled integer adaptation of Newton's method. # # usage sqrt_big [number [digits]] # # note The number defaults to zero. Since the square root of zero # is not defined, this program calculates the Golden Ratio as # a special case. # # note The number of digits defaults to zero. If it is zero, this # program keeps running until something stops it, like user # intervention or out of memory. #----------------------------------------------------------------------------- import sys import math stderr = sys.stderr stdout = sys.stdout def main(): write_limit = 10000 # Get 0 or 1 or 2 arguments (defaults: 0 and 0) this_arg = '0' digits_max = 0 if len(sys.argv) > 1: this_arg = sys.argv[1] if len(sys.argv) > 2: digits_max = int(sys.argv[2]) num_parts = this_arg.split('.') if len(num_parts) == 1: num_parts = num_parts + ['0'] if len(num_parts) != 2: stderr.write( "Number needs 0 or 1 '.'\n" ) return 1 if len(num_parts[0]) < 1: num_parts[0] = '0' if len(num_parts[1]) < 8: num_parts[1] = (num_parts[1] + '00000000')[0:8] num_whole = int( num_parts[0] ) num_fract = int( num_parts[1] ) num_scale = 10 ** len( num_parts[1] ) if num_whole < 0 or num_fract < 0: stderr.write( "Number is negative\n" ) return 1 if num_whole == 0 and num_fract == 0: goldratio = 1 num = 125000000 num_scale = 100000000 else: goldratio = 0 num = num_whole * num_scale + num_fract # Use floating point sqrt() function to make an estimate. num_float = float(num) num_float /= float(num_scale) num_float = math.sqrt(num_float) num_float *= float(num_scale) root = int(num_float) root_scale = num_scale root_str = str(root)[0:1] # Do 12 cycles with short precision growth. for n in range( 0, 12 ): root = ( ( num * root_scale * root_scale + num_scale * root * root ) * root_scale ) / ( root + root ) root /= root_scale root_scale *= num_scale root_str = str(root) digits = 1 + len(str(root)) - len(str(root_scale)) if goldratio: # Fake the addition of 0.5 for the Golden Ratio. stdout.write( '1.6' ) digits_done = 2 elif digits > 0: # Output just the whole digits to get the fraction point in there. stdout.write( root_str[0:digits] ) stdout.write( '.' ) digits_done = digits elif digits <= 0: # Output enough 0 digits to align the fraction. stdout.write( '0.' ) stdout.write( '0'*(-digits) ) digits_done = 0 # Do remaining cycles with long precision growth. for n in range( 0, 60 ): root = ( ( num * root_scale * root_scale + num_scale * root * root ) * root_scale ) / ( root + root ) root_scale = root_scale * root_scale * num_scale root_str = str(root) digits = len(root_str) * 7 / 8; # Output the digits, with a limit per write, up to the maximum. while digits_done < digits: count = digits - digits_done if count > write_limit: count = write_limit if digits_max > 0: remain = digits_max - digits_done if count > remain: count = remain stdout.write( root_str[ digits_done : digits_done + count ] ) digits_done += count if digits_max > 0 and digits_done >= digits_max: break if digits_max > 0 and digits_done >= digits_max: break stdout.write( '\n' ) return 0 main()yep the same append of 3 newlines happened (does not happen when i copy&paste within a term window). and the "return" on line 57 appears to be unindented at this point before i code wrap it. so this narrows down the problem. bad JS code handling the paste-in? but what conditions are triggering this? the minimal case worked OK. edit: lines 67 and 68 are also unindented. this is bad. edit2: line 107 is unindented. looks like random corruption. edit3: you can compare the code with http://stratusrelay.com/phil/bigsqrt.py.txt RE: testing code - wavic - Oct-05-2016 I am looking the source code of the page. For line 106 the code is: <div class="line number106 index105 alt1"> <code class="python spaces"> </code> <code class="python plain">stdout.write( </code> <code class="python string">'0.'</code> <code class="python plain">)</code> </div>In line 107 this one looks like that: <div class="line number107 index106 alt2"> <code class="python spaces"> </code> <code class="python plain">stdout.write( </code> <code class="python string">'0'</code> <code class="python keyword">*</code> <code class="python plain">(</code> <code class="python keyword">-</code> <code class="python plain">digits) )</code> </div>I can't tell what is causing it. RE: testing code - Skaperen - Oct-05-2016 (Oct-05-2016, 06:26 AM)wavic Wrote: I am looking the source code of the page. For line 106 the code is: it is happening before it hits the server. so no surprise that the HTML code varies in number of spaces. here is a post of the code with manual edits to fix unindents i see (not a valid workaround since i may not see all of them). oh dear, something bad is going on, i can't add indents in some cases and i can't unindent where i went too far. there is defenitely something wrong in the JS code for MyBB or in my Firefox (version 49.0 on Ubuntu 16.04.1 LTS) #!/usr/bin/python # -*- coding: ISO-8859-1 -*- #----------------------------------------------------------------------------- # Copyright � 2009 - Philip Howard - All rights reserved # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #----------------------------------------------------------------------------- # program sqrt_big # # purpose Calculate lots of digits for the square root of a given number # using a scaled integer adaptation of Newton's method. # # usage sqrt_big [number [digits]] # # note The number defaults to zero. Since the square root of zero # is not defined, this program calculates the Golden Ratio as # a special case. # # note The number of digits defaults to zero. If it is zero, this # program keeps running until something stops it, like user # intervention or out of memory. #----------------------------------------------------------------------------- import sys import math stderr = sys.stderr stdout = sys.stdout def main(): write_limit = 10000 # Get 0 or 1 or 2 arguments (defaults: 0 and 0) this_arg = '0' digits_max = 0 if len(sys.argv) > 1: this_arg = sys.argv[1] if len(sys.argv) > 2: digits_max = int(sys.argv[2]) num_parts = this_arg.split('.') if len(num_parts) == 1: num_parts = num_parts + ['0'] if len(num_parts) != 2: stderr.write( "Number needs 0 or 1 '.'\n" ) return 1 if len(num_parts[0]) < 1: num_parts[0] = '0' if len(num_parts[1]) < 8: num_parts[1] = (num_parts[1] + '00000000')[0:8] num_whole = int( num_parts[0] ) num_fract = int( num_parts[1] ) num_scale = 10 ** len( num_parts[1] ) if num_whole < 0 or num_fract < 0: stderr.write( "Number is negative\n" ) return 1 if num_whole == 0 and num_fract == 0: goldratio = 1 num = 125000000 num_scale = 100000000 else: goldratio = 0 num = num_whole * num_scale + num_fract # Use floating point sqrt() function to make an estimate. num_float = float(num) num_float /= float(num_scale) num_float = math.sqrt(num_float) num_float *= float(num_scale) root = int(num_float) root_scale = num_scale root_str = str(root)[0:1] # Do 12 cycles with short precision growth. for n in range( 0, 12 ): root = ( ( num * root_scale * root_scale + num_scale * root * root ) * root_scale ) / ( root + root ) root /= root_scale root_scale *= num_scale root_str = str(root) digits = 1 + len(str(root)) - len(str(root_scale)) if goldratio: # Fake the addition of 0.5 for the Golden Ratio. stdout.write( '1.6' ) digits_done = 2 elif digits > 0: # Output just the whole digits to get the fraction point in there. stdout.write( root_str[0:digits] ) stdout.write( '.' ) digits_done = digits elif digits <= 0: # Output enough 0 digits to align the fraction. stdout.write( '0.' ) stdout.write( '0'*(-digits) ) digits_done = 0 # Do remaining cycles with long precision growth. for n in range( 0, 60 ): root = ( ( num * root_scale * root_scale + num_scale * root * root ) * root_scale ) / ( root + root ) root_scale = root_scale * root_scale * num_scale root_str = str(root) digits = len(root_str) * 7 / 8; # Output the digits, with a limit per write, up to the maximum. while digits_done < digits: count = digits - digits_done if count > write_limit: count = write_limit if digits_max > 0: remain = digits_max - digits_done if count > remain: count = remain stdout.write( root_str[ digits_done : digits_done + count ] ) digits_done += count if digits_max > 0 and digits_done >= digits_max: break if digits_max > 0 and digits_done >= digits_max: break stdout.write( '\n' ) return 0 main() |