Skip to content

Commit

Permalink
Add several examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Jun 28, 2024
1 parent 610a9d9 commit 52c6871
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
15 changes: 6 additions & 9 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

This folder contains basic recipes for various common tasks:

- `pil.py` demonstrates a basic workflow for manipulating images and pictures using PIL.
- `numbers.py` has common operations on numeric types.
- `programs.py` has common operations on programs.
- `numpy.py` demonstrates a basic workflow for working with lists and matrices using NumPy.
- `pil.py` demonstrates a basic workflow for manipulating images and pictures using PIL.
- `misc.py` has other common actions, including:
- Detokenizing programs
- Unprotecting programs
- TI-OS Reals `<->` python floats
- TI-OS Exact Types `->` python floats
- Ungrouping Groups
- Packaging TI-OS Reals into a list

<!-- items should be split out of misc.py into specialized files wherever reasonable -->
- Packaging TI-OS reals into a list
- Ungrouping groups
- Modifying a GDB
21 changes: 3 additions & 18 deletions examples/misc.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
from tivars import *


# Detokenize a program
my_program = TIProgram.open("EXAMPLE.8xp")
code = my_program.string()


# Unprotect a program
my_program = TIProtectedProgram.open("EXAMPLE.8xp")
my_program.unprotect()


# Turn a real number into a float
my_real = TIReal.open("EXAMPLE.8xn")
value = my_real.float()


# Put real numbers into a list
my_reals = TIReal(1), TIReal(2), TIReal(3)
lst = TIRealList(my_reals)
Expand All @@ -30,9 +15,9 @@
my_string = TIString("αβγ")


# Convert an exact number into a real number
my_exact = TIRealRadical.open("EXAMPLE.8xn")
real = TIReal(my_exact.float())
# Add an equation to a GDB
my_gdb = TIFuncGDB()
my_gdb.Y1 = TIEquation("sin(X)")


# Open a flash app
Expand Down
44 changes: 44 additions & 0 deletions examples/numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from decimal import Decimal
from fractions import Fraction

from tivars import *


# Reading numbers
my_real = TIReal.open("EXAMPLE.8xn")

my_float = my_real.float() # Convert to a float (can lose precision)
my_float = float(my_real) # You can use the builtin type constructors too
my_string = my_real.string() # Convert to a string (won't lose precision)
my_decimal = my_real.decimal() # Convert to a Decimal (won't lose precision)


my_real_radical = TIRealRadical.open("EXAMPLE.8xn")

my_float = my_real_radical.float() # Convert to a float (WILL lose precision)
my_string = my_real_radical.string() # Convert to a string (won't lose precision)
my_decimal = my_real_radical.decimal() # Convert to a Decimal (WILL lose precision)


my_real_fraction = TIRealFraction("EXAMPLE.8xn")

my_float = my_real_fraction.float() # Convert to a float (can lose precision)
my_string = my_real_fraction.string() # Convert to a string (won't lose precision)
my_fraction = my_real_fraction.fraction() # Convert to a Fraction (won't lose precision)


# Writing numbers
my_real = TIReal(3.14) # Load a float value
my_complex = TIComplex(2 + 3j) # Load a complex value
pi = TIReal("3.1415926535") # Load a string
e = TIReal(Decimal("2.718281828")) # Load a Decimal object

sqrt2 = TIRealRadical(1.414213562) # Errors; radical types DO NOT support loading floats
sqrt2 = TIRealRadical("sqrt(2)") # Use a string instead
five_fourths = TIRealFraction(1.25) # Fraction types DO support floats
five_fourths = TIRealFraction(Fraction(5, 4)) # As well as Fraction objects


three = TIReal(1) + TIReal(2) # Errors; operations on TIReal objects are NOT supported
float_three = TIReal(1).float() + TIReal(2).float() # Instead, convert to a numeric type first...
three = TIReal(float_three) # ...then load back into a TIReal
17 changes: 17 additions & 0 deletions examples/programs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from tivars import *


# 8xp -> Plaintext
my_program = TIProgram.open("EXAMPLE.8xp")

code = my_program.string() # The program's code
os_version = my_program.get_min_os() # The minimum OS version supported by the program
my_program.unprotect() # Unprotect the program (no-op if it already is)


# Plaintext -> 8xp
my_program = TIProgram()

my_program.load_string("EXAMPLE CODE") # Tokenize some code
my_program.load_string("EXAMPLE CODE", model=TI_84P) # Errors if the code isn't supported by the TI-84+
my_program.protect() # Protect the program

0 comments on commit 52c6871

Please sign in to comment.