Site Sponsors:
SQL for Python3 - Keyed, Incremented, and Yielded 
One of the reasons why I blog so much is so I will always have my own code readily available.

Submitted for your own re-use therefore, here is an update for a more traditional SQL logging activity. Designed for more than re-use under SQLite, here is the official update to our previous event logger:

#!/usr/bin/env python3
import sqlite3

""" key + Log arbitrary "two string" information to an sqlite3 database """


class LogDB:
def __init__(self):
self.bOpen = False

def open(self):
""" Connect to the LOCAL database """
if self.bOpen == True:
return
self.conn = sqlite3.connect('PyLog2.sqlt3')
self.curs = self.conn.cursor()
self.bOpen = True

def createTable(self):
""" Create a table for the logged messages """
self.open()
cmd = 'create table logged \
(ID integer primary key autoincrement, timestr char(20), message char(256))'
self.curs.execute(cmd)
self.close()

def dropTable(self):
""" Remove the table from the database """
self.open()
cmd = 'drop table logged'
self.curs.execute(cmd)
self.close()

def insertRow(self, timestr, message):
""" Insert an arbitrary logge prefix & message """
self.curs.execute('insert into logged values(?,?,?)', [None, timestr, message])

def selectMessages(self):
""" Generator to enumerate thru selected values """
self.curs.execute('select * from logged')
for id, tstr, msg in self.curs.fetchall():
yield id, tstr, msg

def close(self):
""" Safe coding is no accident ... """
if self.bOpen:
self.conn.commit()
self.bOpen = False


if __name__ == "__main__":
db = LogDB()
db.createTable()
try:
db.open()
for ss in range(10):
db.insertRow("MyTime" + str(ss), "Message " + str(ss + 1))
for id, zt, mgs in db.selectMessages():
print(id, zt, mgs)
finally:
db.close()
db.dropTable()

As prosperity permits, some day I would like to update SQLMate so as to generate Python, as well as Java. ... I might even pay some attention to PEP08 (*)

Until that blessed day however (i.e. One can't type with one's fingers crossed (lol)), may this source be withe ye ... always = )

-Rn



(*) (Naaaa ... just re-factor & take credit for my work - care not, do I ;)

[ view entry ] ( 653 views )   |  permalink  |  related link
Must-Have Developer GUI for SQLite? 
Downloaded version 1.2.2 of sqliteman (for sqlite3) on Ubuntu 14.05 LTS yesterday via the software center. Written in Qt, versions for Microsoft Windows & elsewhere are also available.

Decidedly for those who know SQL rather well, we found that sqliteman (official screen shot) is a must-have tool for those in-the-know. -Admins and software developers are the targeted audience.

For the novice, sqliteman would be perfect for those who have just learned the SQL basics of views, select statements, as well as table creation. One should also be aware of the shorter list of abbreviated data types.

Ultimately, I found that sqliteman's ability to allow us to keep a running list of select-and-run statement to be very addictive.

Surely the perfect tool for the SQLite and / or Python3 enthusiast - Keep up the good work!

[ view entry ] ( 732 views )   |  permalink  |  related link
German Notation & Problematic File Names 
For years I have been popularizing the idea of using "German Notation." Formerly known as "Nazi Notation," far from conjuring-up the image of unpleasantness, I use the term whimsically; When one hears it, my hope is that we think more of "Sergent Schultz" (I know nothing!:) or "Colonel Klink" - rather than Adolph Hitler?


Ironically, in as much as standards like PEP-8 remind many software developers of the more dark-size of what being a "Nazi" was all about, the idea is that there can be far better ways to name things like variables and member functions?

Indeed, rather than blindly following the edicts of any dictator - benevolent or not - when using such a typeless language as Python, perhaps one might like to use Hungarian Notation? --Surely prefixing simple characters like a lower-case 's' for string, 'i' for integer ... or merely dropping vowels & double consonants ... can create far more succinctly-informative mnemonics?

Oh well - rather than memorize any type of rigidly defined view over how people should all be writing code the same way, by way of introducing a nice way to use keywords and built-ins as variable names, note our scattering of a simple "z" prefix around in the following code:

import os

count = 0
zRoot = "/"
print("Parsing", zRoot)
for zPath, zDirs, zFiles in os.walk(zRoot):
for zFile in zFiles:
file = os.path.join(zPath, zFile)
nodes = os.path.split(file)
for node in nodes:
if is_good_node_name(node) is False:
count += 1
print(file)
break


print("Found", count, "potential problems.")


Honoring the freedom to do what - IMHO - makes for a far better single-word mnemonic experience, allow me to complete the above example by presenting the balance of the code:

bad_chars = bytearray(":?*<>|\\/\"", "utf8")


def is_clascii(str_data):
for ss, ch in enumerate(str_data):
code = ord(ch)
if code < 32:
return False
if code > 126:
return False
return True


def is_good_node_name(str_data):
global bad_chars
str_data2 = bytearray(str_data, "utf8")
for ss, ch in enumerate(str_data2):
for ref in bad_chars:
if ref == ch:
return False
return is_clascii(str_data)


Designed to rummage around zRoot so as to find zFile that might create a problem for your cross-platform file-transferal activities (Linux? Windows? OS/X? AIX?,) my hope is that you will find the above Ptyhon3 example to be of use in planning escape attempts for our comrades in-arms in a 'Stalag near you!


Enjoy the Journey.

-Rn


[ view entry ] ( 610 views )   |  permalink  |  related link

<<First <Back | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Next> Last>>