Skip to content

Instantly share code, notes, and snippets.

@mttjhn
Last active February 16, 2018 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mttjhn/b56b2a2a5d9b9a64d04e0d9a93dc5ede to your computer and use it in GitHub Desktop.
Save mttjhn/b56b2a2a5d9b9a64d04e0d9a93dc5ede to your computer and use it in GitHub Desktop.
A quick Pythonista script (with UI elements) to calculate the percentage complete on a book with irregular page numbering.
import ui
import console
def calcPercent(total, start, current):
pct = (current - start + 1) / (total - start + 1)
return round(pct * 100, 1)
def buttonClick(sender):
total = 0
start = 1
current = 0
for ctl in v.subviews:
if ctl.name == 'tfTotalPages':
total = int(ctl.text)
if ctl.name == 'tfFirstPage':
start = int(ctl.text)
if ctl.name == 'tfCurrentPage':
current = int(ctl.text)
test = calcPercent(total, start, current)
console.alert(str(test) + '%')
v = ui.load_view()
v.present('sheet')
[
{
"nodes" : [
{
"nodes" : [
],
"frame" : "{{6, 6}, {150, 31}}",
"class" : "Label",
"attributes" : {
"font_size" : 18,
"frame" : "{{45, 104}, {150, 32}}",
"uuid" : "6DEE7310-0FAA-42D2-A1CD-761AB89408EE",
"class" : "Label",
"alignment" : "left",
"text" : "Total Pages",
"name" : "label1",
"font_name" : "<System>"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{6, 45}, {200, 32}}",
"class" : "TextField",
"attributes" : {
"font_size" : 17,
"frame" : "{{20, 104}, {200, 32}}",
"spellchecking_type" : "default",
"class" : "TextField",
"uuid" : "299139CB-B076-4467-A2A8-FD6D7C8ECCDE",
"alignment" : "left",
"autocorrection_type" : "default",
"name" : "tfTotalPages",
"font_name" : "<System>"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{6, 165}, {150, 32}}",
"class" : "Label",
"attributes" : {
"font_size" : 18,
"frame" : "{{45, 104}, {150, 32}}",
"uuid" : "BC6DD854-502B-4EE9-B75F-BEA13A9258FD",
"class" : "Label",
"alignment" : "left",
"text" : "Current Page",
"name" : "label2",
"font_name" : "<System>"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{6, 202}, {200, 32}}",
"class" : "TextField",
"attributes" : {
"font_size" : 17,
"frame" : "{{20, 104}, {200, 32}}",
"spellchecking_type" : "default",
"class" : "TextField",
"uuid" : "D5A9508D-DD47-4574-8C3B-538D8908E6AA",
"alignment" : "left",
"autocorrection_type" : "default",
"name" : "tfCurrentPage",
"font_name" : "<System>"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{6, 85}, {150, 32}}",
"class" : "Label",
"attributes" : {
"font_size" : 18,
"frame" : "{{45, 104}, {150, 32}}",
"uuid" : "CF04F9F7-802F-4C2D-B9C1-B3A5DEF8AF48",
"class" : "Label",
"alignment" : "left",
"text" : "First Page",
"name" : "label3",
"font_name" : "<System>"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{6, 125}, {200, 32}}",
"class" : "TextField",
"attributes" : {
"name" : "tfFirstPage",
"frame" : "{{20, 104}, {200, 32}}",
"spellchecking_type" : "default",
"class" : "TextField",
"uuid" : "A6B04FE7-90CC-4AB5-94D2-3DF4E3834513",
"alignment" : "left",
"text" : "1",
"autocorrection_type" : "default",
"font_size" : 17,
"font_name" : "<System>"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{6, 242}, {164, 32}}",
"class" : "Button",
"attributes" : {
"action" : "buttonClick",
"border_width" : 1,
"frame" : "{{80, 132}, {80, 32}}",
"title" : "Calculate Percentage",
"uuid" : "AA6D7021-B72E-4516-85DB-B019839D08FA",
"class" : "Button",
"corner_radius" : 2,
"name" : "button1",
"font_size" : 15
},
"selected" : true
}
],
"frame" : "{{0, 0}, {240, 357}}",
"class" : "View",
"attributes" : {
"enabled" : true,
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"selected" : false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment