Skip to content

Commit

Permalink
task 1 and 2 added. Also a sqlalchemy tutorial..
Browse files Browse the repository at this point in the history
  • Loading branch information
effyhuihui committed Dec 4, 2014
1 parent 7e0d8f9 commit 3b373bc
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 4 deletions.
10 changes: 6 additions & 4 deletions effy/0001/0001.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env
'''
Self-defined activation code
Type A:
1. has length of 10
2. mixture of letters and numbers
3. unique throughtout 200 combos
Pros:
total of 3656158440062976 combos, randomly generation of 200 is almost
guaranteed to be unique; Confident level if high without look up opeations
guaranteed to be unique; Confident level if high without look up opeations.
And without look up, it's super fast :) The more codes needed, the effect
is more obvious.
Cons:
Without back tracking, small chance would occur of duplication.
Expand All @@ -21,7 +23,7 @@ def digit(raw):
l = len(raw)
return raw[random.randrange(l)]

def typeACodeGen(n):
def codeGen(n):
codes_pool = []
for i in range(n-1):
code = ""
Expand All @@ -30,4 +32,4 @@ def typeACodeGen(n):
codes_pool.append(code)
return codes_pool

codes = typeACodeGen(200)
codes = codeGen(200)
109 changes: 109 additions & 0 deletions effy/0002/0002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env
'''
DO NOT RUN BEFORE READING THE WHOLE THING!!
Pre-request :
1. A live and running DB of your choice (Mine is Postgres for convenience)
2. A table created for storage use
3. ORM API of your choice (Mine is Sqlalchemy,to install : $ sudo pip install sqlalchemy)
ABOUT Sqlalchemy:
a. A good ref http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html
b. A dummy tutorial http://stackoverflow.com/questions/2048084/sql-alchemy-what-is-wrong
http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html
http://docs.sqlalchemy.org/en/rel_0_9/core/metadata.html
http://stackoverflow.com/questions/14307238/updating-row-in-sqlalchemy-orm
'''
from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker,scoped_session
from ActivationCodeGen import codeGen

'''
Step 1:
start a live connection with DBApi
'''
## activate echo option will print out actual SQL statement at each step.
## to connect to postgresql, create_engine('dialect://username:password@local:port/DBname')
db = create_engine('postgresql://username:password@localhost:5432/dbname', echo=True)

'''
Step 2:
Define and create table/metadata (Alternative 1), or use existing table (Alternative 2)
'''
metadata = MetaData(db)

'''
Alternative 1:
The following code block is of one time use, after initilization of the table
"codes", it should be ignored and should not be run again.
'''
###########################################################
# codes = Table('codes', metadata, #
# Column('id', Integer, primary_key=True), #
# Column('code', String(10), nullable=False #
# ,unique=True) #
# ) #
# codes.create() #
###########################################################

'''
Alternative 2:
This code block is to be used if table "codes" already
exists -- then the only work is to fetch the definition
of the table from db, like this:
'''
###################################################
codes = Table('codes', metadata, autoload=True) #
###################################################

## this specific command flushes out all the pending request for table creation, it
## actually create all the tables that does not exist in the db yet.
metadata.create_all(db)

'''
Step 3:
define ORM
CAUION: STEP 3, 4, 5 CAN NOT EXCHANGE AMONGST EACH OTHER. THE SEQUENCE IS DEFINITE
'''
class Codes(object):
__table__ = "codes"
def __init__(self, id, code):
self.id = id
self.code = code
def __repr__(self):
return "The {0}th activation code: {1}".format(self.id, self.code)

'''
Step 4:
map between object and relational table.
'''
mapper(Codes, codes)
metadata.create_all(db)


'''
Step 5:
Create instances of Codes class
'''

raw_codes = codeGen(200)
code_list = []
for i in range(len(raw_codes)):
code_list.append(Codes(i+1, raw_codes[i]))


'''
Step 6:
start session. Add records and commit.
'''
sm = sessionmaker(bind=db, autocommit=False, expire_on_commit=True)
session = scoped_session(sm)
for code in code_list:
#add will stage all the transactions
session.add(code)

session.commit()



35 changes: 35 additions & 0 deletions effy/0002/ActivationCodeGen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env
'''
Self-defined activation code
1. has length of 10
2. mixture of letters and numbers
3. unique throughtout 200 combos
Pros:
total of 3656158440062976 combos, randomly generation of 200 is almost
guaranteed to be unique; Confident level if high without look up opeations.
And without look up, it's super fast :) The more codes needed, the effect
is more obvious.
Cons:
Without back tracking, small chance would occur of duplication.
'''
import random

codeSeedA = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def digit(raw):
l = len(raw)
return raw[random.randrange(l)]

def codeGen(n):
codes_pool = []
for i in range(n-1):
code = ""
for i in range(10):
code += digit(codeSeedA)
codes_pool.append(code)
return codes_pool

codes = codeGen(200)

0 comments on commit 3b373bc

Please sign in to comment.