First commit

This commit is contained in:
Vivek Teega 2019-02-15 16:35:51 +05:30
commit 78633551f6
11 changed files with 453 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea/
__pycache__/

44
app.py Normal file
View File

@ -0,0 +1,44 @@
from flask import Flask
from flask import render_template, flash, redirect, g
from flask_wtf import Form
from wtforms import StringField
from wtforms.validators import DataRequired
from wtforms.widgets import TextArea
import parse_incorp
import sqlite3
class MyForm(Form):
flodata = StringField('FLO Data', validators=[DataRequired()])
class ReportError(Form):
comments = StringField('Comments', widget=TextArea())
conn = sqlite3.connect('errors.db')
conn.execute('''CREATE TABLE IF NOT EXISTS errorlogs (id INTEGER PRIMARY KEY AUTOINCREMENT, flodata TEXT, comments TEXT);''')
conn.close()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'Teega'
@app.route("/", methods=['GET', 'POST'])
def textparse():
form = MyForm()
errorform = ReportError()
if form.validate_on_submit():
g.parsed_data = parse_incorp.parse_flodata(form.flodata.data)
return render_template('index.html', form=form, parsed_data= g.parsed_data, errorform=errorform)
if errorform.validate_on_submit():
#conn = sqlite3.connect('test.db')
#sqlquery = 'INSERT INTO errorlogs (flodata, comments) VALUES ({},{})'.format( g.parsed_data['flodata'], errorform.comments.data)
#conn.execute(sqlquery)
print(g.parsed_data)
#conn.close()
return render_template('index.html', form=form, parsed_data= g.parsed_data, errorform=errorform)
return render_template('index.html', form=form, errorform=errorform)
app.run(debug=True)

BIN
errors.db Normal file

Binary file not shown.

17
flo.py Normal file
View File

@ -0,0 +1,17 @@
from hashlib import sha256
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
def decode_base58(bc, length):
n = 0
for char in bc:
n = n * 58 + digits58.index(char)
return n.to_bytes(length, 'big')
def check_bc(bc):
try:
bcbytes = decode_base58(bc, 25)
return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]
except Exception:`
return False
print(check_bc('FN93RGsz25vUPMwoyzQvrM95G5M42PGNWS'))

102
parse-mod.py Normal file
View File

@ -0,0 +1,102 @@
import re
import testcases
marker=None
operation=None
address=None
amount=None
def extractMarkers(text):
returnval = None
text = text.lower()
text = ' '.join(text.split())
textlst = text.split(' ')
for part in textlst:
if part[-1] == '#' and len(part)>1:
if returnval is not None:
return 'od'
returnval = part
return returnval
def extractOperation(text, operationList):
count = 0
returnval = None
text = text.lower()
for operation in operationList:
operation = operation.lower()
count = count + text.count(operation)
if count > 1:
return 'od'
if count == 1 and (returnval is None):
returnval = operation
return returnval
def extractAmount(text):
count = 0
returnval = None
text = text.lower()
splitText = re.split("\W+", text)
for word in splitText:
word = word.replace('rmt', '')
try:
float(word)
count = count + 1
returnval = float(word)
except ValueError:
pass
if count > 1:
return 'od'
return returnval
def isIncorp(text):
wordlist = ['incorporate','create','start']
cleantext = re.sub(' +', ' ',text)
textList = cleantext.split(' ')
for word in wordlist:
if word in textList:
return True
return False
def extractIncMarker(text):
cleantext = re.sub(' +', ' ',text)
textList = cleantext.split(' ')
for word in textList:
if word[-1] == '#':
return word
return False
def extractInitTokens(text):
base_units = {'thousand':10**3 , 'million':10**6 ,'billion':10**9, 'trillion':10**12}
cleantext = re.sub(' +', ' ',text)
textList = cleantext.split(' ')
for idx,word in enumerate(textList):
try:
result = float(word)
if textList[idx+1] in base_units:
return result*base_units[textList[idx+1]]
return res
except:
continue
# Combine test
def parse_flodata(string):
if not isIncorp(string):
operationList = ['send', 'transfer', 'give']
marker = extractMarkers(string)
operation = extractOperation(string, operationList)
amount = extractAmount(string)
parsed_data = {'type': 'transfer', 'flodata': string, 'marker': marker, 'operation': operation,
'amount': amount}
else:
incMarker = extractIncMarker(string)
initTokens = extractInitTokens(string)
parsed_data = {'type': 'incorporation', 'flodata': string, 'marker': marker, 'initTokens': initTokens}
return parsed_data

121
parse.py Normal file
View File

@ -0,0 +1,121 @@
import re
import testcases
marker=None
operation=None
address=None
amount=None
def extractMarkers(text, markerList):
count = 0
returnval = None
text = text.lower()
for marker in markerList:
if marker[-1] != '#':
marker = marker + '#'
marker = marker.lower()
count = count + text.count(marker)
if count > 1:
return 'od'
if count == 1 and (returnval is None):
returnval = marker
return returnval
def extractOperation(text, operationList):
count = 0
returnval = None
text = text.lower()
for operation in operationList:
operation = operation.lower()
count = count + text.count(operation)
if count > 1:
return 'od'
if count == 1 and (returnval is None):
returnval = operation
return returnval
def extractAmount(text):
count = 0
returnval = None
text = text.lower()
splitText = re.split("\W+", text)
for word in splitText:
word = word.replace('rmt','')
try:
float(word)
count = count + 1
returnval = float(word)
except ValueError:
pass
if count > 1:
return 'od'
return returnval
# Combine test
operationList = ['send','transfer','give']
markerList = ['ranchimall','rmt']
for string in testcases.testStrings:
marker = extractOperation(string, markerList)
operation = extractOperation(string, operationList)
amount = extractAmount(string)
print('text - ' + string)
print('Marker - '+str(marker))
print('Operation - '+str(operation))
print('Amount - '+str(amount)+'\n\n')
# Marker test
'''markerList = ['ranchimall','rmt']
for string in testcases.testStrings:
returnval = extractMarkers(string, markerList)
if returnval is not None:
if returnval == 'od':
print('text - ' + string)
print('Transaction reject\nMore than one marker present\n\n')
else:
print('text - ' + string)
print('Marker - '+str(returnval)+'\n\n')
else:
print('text - ' + string)
print('Marker not found\n\n')
# Operator test
operationList = ['send','transfer','give']
for string in testcases.testStrings:
returnval = extractOperation(string, operationList)
if returnval is not None:
if returnval == 'od':
print('text - ' + string)
print('Transaction reject\nMore than one operation present\n\n')
else:
print('text - ' + string)
print('Operation - '+str(returnval)+'\n\n')
else:
print('text - ' + string)
print('Operation not found\n\n')'''
'''
GRAVEYARD
----------
def extractAddress(text):
count = 0
returnval = None
for operation in operationList:
operation = operation.lower()
count = count + text.count(operation)
if count > 1:
return 'od'
if count == 1 and (returnval is None):
returnval = operation
return returnval'''

111
parse_incorp.py Normal file
View File

@ -0,0 +1,111 @@
import re
import testcases
marker=None
operation=None
address=None
amount=None
def extractMarkers(text):
returnval = None
text = text.lower()
textlst = text.split(' ')
for part in textlst:
if part[-1] == '#' and len(part)>1:
if returnval is not None:
return 'od'
returnval = part
return returnval
def extractOperation(text, operationList):
count = 0
returnval = None
text = text.lower()
for operation in operationList:
operation = operation.lower()
count = count + text.count(operation)
if count > 1:
return 'od'
if count == 1 and (returnval is None):
returnval = operation
return returnval
def extractAmount(text):
count = 0
returnval = None
text = text.lower()
splitText = re.split("\W+", text)
for word in splitText:
word = word.replace('rmt','')
try:
float(word)
count = count + 1
returnval = float(word)
except ValueError:
pass
if count > 1:
return 'od'
return returnval
def isIncorp(text):
wordlist = ['incorporate','create','start']
cleantext = re.sub(' +', ' ',text)
cleantext= cleantext.lower()
textList = cleantext.split(' ')
for word in wordlist:
if word in textList:
return True
return False
def extractIncMarker(text):
cleantext = re.sub(' +', ' ',text)
textList = cleantext.split(' ')
for word in textList:
if word[-1] == '#':
return word
return False
def extractInitTokens(text):
base_units = {'thousand':10**3 , 'million':10**6 ,'billion':10**9, 'trillion':10**12, 'lakh':10**5, 'crore':10**7}
cleantext = re.sub(' +', ' ',text)
textList = cleantext.split(' ')
for idx,word in enumerate(textList):
try:
result = float(word)
if textList[idx+1] in base_units:
return result*base_units[textList[idx+1]]
return result
except:
continue
# Combine test
def parse_flodata(string):
if string[0:5] == 'text:':
string = string.split('text:')[1]
string = string.lower()
if not isIncorp(string):
operationList = ['send', 'transfer', 'give']
marker = extractMarkers(string)
operation = extractOperation(string, operationList)
amount = extractAmount(string)
parsed_data = {'type': 'transfer', 'flodata': string, 'marker': marker, 'operation': operation,
'amount': amount}
else:
incMarker = extractIncMarker(string)
initTokens = extractInitTokens(string)
parsed_data = {'type': 'incorporation', 'flodata': string, 'marker': incMarker, 'initTokens': initTokens}
return parsed_data

41
templates/index.html Normal file
View File

@ -0,0 +1,41 @@
<html>
<body>
<h1>Incorporation and Transfer comment tester</h1>
<form action="" method="post" name="input">
{{ form.hidden_tag() }}
<p>
Enter FLO data:<br>
{{ form.flodata(size=80) }}<br>
</p>
<p><input type="submit" value="Parse"></p>
</form>
{% if parsed_data %}
{% if parsed_data['type'] == 'transfer' %}
<p>type : {{ parsed_data['type'] }}</p>
<p>flodata : {{ parsed_data['flodata'] }}</p>
<p>marker : {{ parsed_data['marker'] }}</p>
<p>operation : {{ parsed_data['operation'] }}</p>
<p>amount : {{ parsed_data['amount'] }}</p>
{% endif %}
{% if parsed_data['type'] == 'incorporation' %}
<p>type : {{ parsed_data['type'] }}</p>
<p>flodata : {{ parsed_data['flodata'] }}</p>
<p>marker : {{ parsed_data['marker'] }}</p>
<p>initTokens : {{ parsed_data['initTokens'] }}</p>
{% endif %}
{% endif %}
------------------
<form action="" method="post" name="reporterror">
{{ form.hidden_tag() }}
Ps - If you find an anamoly, please report the error below
<p>
Comments on the error:<br>
{{ errorform.comments(cols="35", rows="10") }}<br>
</p>
<p><input type="submit" value="Report error"></p>
</form>
</body>
</html>

0
test.db Normal file
View File

15
testcases.py Normal file
View File

@ -0,0 +1,15 @@
testStrings = [
'create 21000000 tokens with token transfer marker being rmt#',
'start 21 thousand coins with token transfer marker being rohit#',
'incorporate 10 billion coins with marker being vivek#',
'create 100 coins with marker being abc#',
'start 10 tokens pqr#',
'100000 mnq#',
'start 100000 mnq#',
'mnq# start 10 billion',
'mnq# start 10 crore',
'create 2 crore tokens with marker being lnq#'
]

BIN
testcases.pyc Normal file

Binary file not shown.