Thursday, September 27, 2018

Refcards: The Beginnings of an Interface

- The latest development in the evolution of the Refcards-System is the creation of a primitive interface;
- Now I can add records to my SQLite3 database through an interface;
- The interface has a button called "Add entry" which upon clicking adds the text ("gets" it) from the Entry and Text widgets in the interface and adds them ("INSERTs" them) directly into the database;
- I've been using Tkinter to create the interface ("frontend") and SQLite3 in Python for the database ("backend");
from Tkinter import *
import os
import sqlite3
os.chdir('C:\\FILEPATH')
# TO RUN ON FIRST PASS, TO CREATE OR OPEN DATABASE AND CREATE FIRST TABLE.
# conn = sqlite3.connect("refcards2019b.db")
# cursor = conn.cursor()
# cursor.execute("""CREATE TABLE refcards(date TEXT, note TEXT)""")
# conn.commit()
def insert(date,note):
conn = sqlite3.connect("refcards2019b.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO refcards VALUES (?,?)",(date, note))
conn.commit()
conn.close()
def add_command():
insert(date_text.get(), T.get("1.0","end-1c"))
root = Tk()
date_label = Label(root, text='Date')
date_label.grid(row=0, column=0)
date_text = StringVar()
date_entry = Entry(root, textvariable=date_text)
date_entry.grid(row=0, column=1)
note_label = Label(root, text='Note')
note_label.grid(row=1, column=0)
T = Text(root, height=20, width=20)
T.grid(row=2, column=1)
add_button = Button(root, text="Add entry", width=12, command=add_command)
add_button.grid(row=4, column=3)
root.mainloop()

Wednesday, September 26, 2018

Tkinter Text Widgets in Python

- I have been working hard on my Refcards-System idea, in Python;
- I want it to be a simple database with simple interface where you can add records to a given table and then view your "notes" or "Refcards";
- The idea is to have a GUI to flip through Refcards in the database;


import sqlite3
import os
from Tkinter import *
root = Tk()
S = Scrollbar(root)
T = Text(root, height=8, width=75)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
os.chdir('C:\\FILEPATH')
conn = sqlite3.connect("refcards2019.db")
cursor = conn.cursor()
sql = "SELECT * FROM refcards"
cursor.execute(sql)
# note = cursor.fetchone()[2]
note = cursor.fetchall()[1][2]
T.insert(END, note)
mainloop( )
conn.close()



Tuesday, September 25, 2018

The Return of The Refcards-System

- As you know, I've been working on a project in Python called The Refcards-System, or "Refcard-Project" if you will;
- I have been incrementally improving the system;
- Now I have a working version that incorporates an SQLite database in Python;
- The next step is to have a kind of small text editor open up to view records in the database;


import sqlite3
import os
import time
# Choose the filepath you want the database to reside in.
os.chdir('C:\\FILEPATH')
# Creates the database on the first run, otherwise just connects to it.
conn = sqlite3.connect("refcards2019.db")
cursor = conn.cursor()
# To be used on the first "run" to create the first TABLE in the database.
# cursor.execute("""CREATE TABLE refcards(id INTEGER PRIMARY KEY AUTOINCREMENT,
# date TEXT, note TEXT)""")
# conn.commit()
# A timestamp in a useful format.
t = time.strftime('%Y%m%d%H%M')
# Prompt to write the actual note.
c = raw_input('Write note: ')
# Inserts timestamp and note into the refcards TABLE as a record.
cursor.execute("""INSERT INTO refcards(date, note)
VALUES(?,?)""", (t,c))
# Operations on database tables must be "committed".
conn.commit()
# I use this to print the contents of the TABLE,
# just to see that it worked properly.
# This section can be skipped.
sql = "SELECT * FROM refcards"
cursor.execute(sql)
print cursor.fetchall()
# Close connection to database.
conn.close()
view raw refcards2019.py hosted with ❤ by GitHub

Thursday, June 21, 2018

The Refcards-System Redux

18:52 2018-06-21

- As you may know, I have been working on my "Refcards-System" for years now. I haven't made much progress, but I still have made some progress;
- This latest iteration is all about "writing" to a .csv file; the idea is that a .csv file can be "imported" into a relational database or else opened up as an "Excel" file, making it rather interoperable;

import csv
import time
t = time.strftime('%Y%m%d%H%M')
with open(t+'.csv', 'wb') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Time', 'Context', 'Notes', 'References'])
writer.writerow([time.asctime(), raw_input('Context: '), raw_input('Notes: '), raw_input('References: ')])
csvfile.close()
view raw RefcardsCSV.py hosted with ❤ by GitHub
- So what we have is simple. First I create the "filename" using some magical "time" module tricks. I want the filename to basically be the current timestamp, + the .csv file ending;
- Then basically I create the first "row" which are the "fieldnames" in the .csv file, and then I write the second "row" which is my content;
- Notice that I am using "raw_input", in Python that allows me to enter text directly into the file, with a prompt;
- The point is really to have content in .csv format, which is portable, interoperable, and lightweight as well. It's also a format that has withstood the test of time.

Sunday, March 11, 2018

Idiomatic Random Thoughts

I've been practicing my Python again. I am working on a Python version of an old boardgame called Stock Ticker. I had a breakthrough after thinking about it for 2 years. Right now, though, I'm just goofing off. This is imaginary code for the use of a random_thoughts module.

import random_thoughts
def current_thoughts():
'My current thoughts'
return random_thoughts.current()
def idiomatic_python_thoughts():
'Turns thoughts into idiomatic Python'
return random_thoughts.idiomatic_python()

I've been watching videos of Python masters teaching their great wisdom. Of note is the work of Raymond Hettinger. I've learned a lot from watching him talk about writing beautiful Python code. I am only 7 years or so into my Python practice, so I'm not yet an expert. I am entering into the "intermediary" territory, though, because I can read almost any Python code and pretty much know what it's doing, and I can write code to solve problems I encounter every day. I still can't write a packaged app, a full-blown executable, with interface and everything. But I am beginning to truly understand Python, which is still a big accomplishment for me.

My dream is to think Pythonic thoughts. I want to turn my thoughts into mathematical expressions, and be able to execute them using Python as a programming language. Hence the thought experiment on the random_thoughts module. It's not really good Python code, but it's a start. The idea is that there would be a function I could use to turn my thoughts directly into idiomatic Python.