Hints and Tips and How-Tos


Prettyprint

Note pprint takes only one argument

use the following link until I get round to writing a summary

Prettyprint

Miscellaneous


1.1  use Del to backspace delete

1.2  !LineEdit is a "must have" for using the command line shell

1.3  Ctrl-D to quit Help mode

1.4  comments start with hash # and continue to end of line

1.5  use 4 spaces per indentation level

1.6  Limit all lines to a maximum of 79 characters.

1,7  Use CamelCase for classes and
     lower_case_with_underscores for functions and methods

1.8  "Dunder names" eg, __all__
     (Dunder is Double Underscore)

1.9  module sys defines  prompts, path, etc
     >>>sys.ps1 = '--> '
     -->
     --> print(sys.platform)
     --> riscos

1.A  To recall last expression output  use underscore  _
     eg
     >>>2+2
     4
     >>>_+2
     6
     >>>

2.1  Allow StrongED to copy and paste into a TaskWindow
     load the TaskWindow ModeFile
     change:
       cs-V  ToTask
     to:
       cs-V  ReleaseShiftCtrl CB_Paste

     change:
       cs-C  ToTask
     to:
       cs-V  ReleaseShiftCtrl CB_Copy

2.2  StrongED search tips
     ctrl-E,ctrl-D    search directory
     ctrl-shft-F5     internet dictionary
     ctrl-shft-F11    search directory

3.1  use dot for slash inside Python shell
     but use slash for dot in Python script
     eg
        import sqlite3
        con = sqlite3.connect('eg/db')

3.2  to reference parent directory - use ^.filename

3.3  CWD Current Working Directory
     >>> import os
     >>> cwd=os.getcwd()
     >>> print(cwd)
     SCSI::SSD.$.JR.Programz.PYTHON.eg_test.tkinter

     You can change the current working directory

     import os
     os.chdir(path)
     ....
     Changing the current working directory in a subprocess
     does not change the  current working directory in the
     parent process.
     This is true of the Python interpreter as well.
     You cannot use  os.chdir() to change the CWD of
     the calling process.


4.1  print output goes to standard out
      sys.stdout (2)
     error output goes to standard error
      sys.stderr (1)
     eg python3 mypgm.py 1>a 2>b
     eg Dir 
        python3 .!RunImage 1>^.stdout
        (puts output in same dir as app)

4.2  There is a bug in RISC OS 3.8.python which
     sometimes leads to print output appearing in the
     wrong order. Use the print flush method to force
     immediate printing

     sys.stdout.flush()


5.0  Functions

5.1   how to define a function -
      def function_name(parameters):
       """docstring"""
       statement(s)

5.2  Trigonometric functions
     import math
     opp = 65
     adj = 216
     rad = math.atan2(opp,adj)
     deg = math.degrees(rad)
     print(rad, deg)

5.3  Date functions
     >>>from datetime import date
     >>>pr = date.today()  ;  print(pr)
     2021-07-30
     
     >>>from datetime import datetime
     >>>print(datetime.now())
     2021-07-30 18:10:18.590000
     
6.0  More syntax

6.1  A String cannot be changed via indexed assignment

6.2  Python has No CASE statement - if if,elif instead

6.3  For with range
     import math
     adj = 216
     for opp in range(12,90,12):
        rad = math.atan2(opp,adj)
        deg = math.degrees(rad)
        print(opp, deg)

6.4  a, b = b, a
     swaps without needing an intermediate variable

6.5  hex(512)
     '0x200'

7.0  Classes

7.1  example of a class

     class BackDrop:
         """manage screen background picture"""
         def __init__(self):       # name self is conventional
             self.pic = "pix.b01"

7.2  Is Class same as Type?  -  see eg below

     rent = (
         "r03",192,
         "r02",682)
     print(rent)
     print(type(rent))

     ('r03', 192, 'r02', 682)
     <class 'tuple'>

q.   Use the swi module to issue system commands

     # example RISC OS SWI commands
     import swi
     swi.swi("OS_Write0","s","Hello RISC OS World")  # this works
     swi.swi("OS_WriteC","i",10)                     # line feed
     swi.swi("OS_WriteC","i",13)                     # carrier return
     swi.swi("OS_Byte","i",0)                        # Os version

     From Chris Johns Python Release Notes
     RISC OS additions
     -----------------
     os.get_filetype(object) returns the file type of a given object.
     os.set_filetype(object,type) will set it.

     swi.swi(arg1,arg2,arg3) allows Python code to call RISC OS SWIs.
      arg1 is the SWI number or name,
      arg2 is a format string for arg3,
      arg3 is the rest of the arguments.
     eg: swi.swi("OS_Write0","s","Hello RISC OS World")

     stat will also return st_loadaddr (Load Address), st_execaddr (Exex Address), st_filetype (Filetype), and st_objtype (Object type)

r.   Use os.system to issue * (star)  commands
     # example RISC OS * - star commands
     import os
     os.system('fx 0')                               # Os version
     os.system('cat')
    
     showparm = "*path*"
     pr = os.system("show "+ showparm)  ;  print(pr)


© 2021 JR