giovedì 8 aprile 2021

PYTHON - Light database from json file

 A very light textual database, very simple and basic. 

Download the two files maker.py and lightdb.py in the same directory. Run lightdb.py to build the database on a json file. Then there is the possibility to add data directly from lightdb or you can build a .py file that will serve to manage the database. There is also the possibility to transform the json file into an excel file.

 You can download  the file from repo on github: lightdb

sabato 3 aprile 2021

PYTHON - Make and read JSON file with JSON library

Simple example for to show python power
import json
import sys
#Write and read json file with json library and dict
def makeJSON(jsFile):
    dict={}
    while True:
        chiave=input("Chiave> ")
        if chiave=='#e':
            break
        valori=input("Valori> ")
        lista=valori.split(' ')
        dict[chiave]=lista
    with open(jsFile,"w") as json_file:
        json.dump(dict, json_file)

def readJSON(jsFile):
    dict={}
    with open(jsFile,"r") as json_file:
        dict=json.load(json_file)
    return dict

def understandvalues(dict):
    n=0
    f=0.0
    l=[]
    d={}
    klist=list(dict.keys())
    for i in range(len(klist)):
        val=dict[klist[i]]
        if type(val)==type(l):
            print("Lista")
        elif type(val)==type(n) or type(val)==type(f):
            print("numero")
        elif type(val)==type(d):
            print("dizionario")

if __name__ == "__main__":
    comandi="Ho to use: ej3.py (r or w) (file name) \nr read json file\nw write json file"
    try:
        if sys.argv[1]=='r':
            d=readJSON(sys.argv[2])
            understandvalues(d)
            print(d)
        elif sys.argv[1]=='w':
            makeJSON(sys.argv[2])
        else:
            print(comandi)
    except:
        print(comandi)

PYTHON - Building a memory based on external stimuli

This script parses the incoming phrase with the keys contained in the json file, if it finds the key it processes a response, otherwise it adds the key and rewrites the json file.
import json
#Da stimoli esterni il software allarga le proprie conoscenze

def caricaCoscienza(nomeCoscienza):
    with open(nomeCoscienza,"r") as json_file:
        dictFromJSon=json.load(json_file)
    return dictFromJSon

def memorizza(valori,dict,nomeCoscienza):
    chiave=valori[0]
    lista=[]
    for i in range(len(valori)):
        if i>0:
            lista.append(valori[i])
    dict[chiave]=lista
    with open(nomeCoscienza,"w") as json_file:
            json.dump(dict, json_file)
    with open(nomeCoscienza,"r") as json_file:
        dictFromJSon=json.load(json_file)
    return dictFromJSon

def elabora(valori,ch,keys,dict):
    chiave=keys[ch]
    risposta=dict[chiave]
    return risposta

def ricerca(ingresso,dict,nomeCoscienza):
    keys=list(dict.keys())
    valori=ingresso.split(' ')
    trovato=False
    r=''
    for i in range(len(keys)):
        for c in range(len(valori)):
            if keys[i]==valori[c]:
                trovato=True
                ch=i
    if not(trovato):
        dict=memorizza(valori,dict,nomeCoscienza)
    else:
        r=elabora(valori,ch,keys,dict)
    return r


if __name__ == "__main__":
    dict=caricaCoscienza("result.json")
    dato=input(">")
    print(ricerca(dato,dict,"result.json"))

PYTHON - FROM DICTIONARIES to JSON

Python dictionaries: a power; python dictionaries + JSON: super power.
import json
#HOW TO MANAGE DICTIONARY
#+-+-+-+-+-+-+-+-+-+-+-+#
d = {
  "Chiave": "Codice",
  "Costo": 1.23,
  "Ricarico": 30,
  "Listino": 3.00
}
c = d.items()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
c = d.keys()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
c=d.values()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
nd = {
    "dizionario":{"subDict":["ch1","ch2","ch3"],
                "other":"otherSub"
                },
    "numero":2,
    "codice":"codice"
}
c=nd.items()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
c = nd.keys()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
c=nd.values()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
newD=lista[0]
c=newD.values()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
nuovaLista=lista[0]
for i in range(len(nuovaLista)):
    print(nuovaLista[i])
print("Costruzione JSON")
with open("result.json", "w") as json_file:
    json.dump(nd, json_file)
json_string = json.dumps(nd)
print(json_string)
with open("result.json","r") as json_file:
    dictFromJSon=json.load(json_file)
print(dictFromJSon)

giovedì 1 aprile 2021

PYTHON - DICTIONARIES: A POWER

Python dictionaries: a power. Find the name of the keys, the values. Dictionaries can, linked to JSON, become a powerful lightweight database.
#HOW TO MANAGE DICTIONARY
#+-+-+-+-+-+-+-+-+-+-+-+#
d = {
  "Chiave": "Codice",
  "Costo": 1.23,
  "Ricarico": 30,
  "Listino": 3.00
}
c = d.items()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
c = d.keys()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
c=d.values()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
nd = {
    "dizionario":{"subDict":["ch1","ch2","ch3"],
                "other":"otherSub"
                },
    "numero":2,
    "codice":"codice"
}
c=nd.items()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
c = nd.keys()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
c=nd.values()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
newD=lista[0]
c=newD.values()
print(c)
lista=list(c)
for i in range(len(lista)):
    print(lista[i])
nuovaLista=lista[0]
for i in range(len(nuovaLista)):
    print(nuovaLista[i])

PYTHON - JSON MAKER

Maker JSON Files
Writing a json file can be boring, with this very simple script it's boring all the same, but at least in the end everything is correct.
########################################
# json files editor to use as database #
########################################

import sys,os
import curses
import json

def interpreta(o,file,next,cont):
    if next=='#k' and cont==0:
        file=file+'\"'+o+'\":'
        next=''
        cont+=1
    elif next=='#k' and cont>0:
        file=file+',\"'+o+'\":'
        next=''
    elif next=='#v':
        file=file+'\"'+o+'\"'
        next=''
    elif next=='#vl':
        c=o.split(',')
        file=file+'['
        for i in range(len(c)):
            file=file+'\"'+c[i]+'\"'
            if i!=len(c)-1:
                file=file+','
        file=file+']'
        next=''
    if o=='#k' and cont==0:
        file='{'
        next='#k'
    if o=='#k' and cont>0:
        next='#k'
    if o=='#v':
        next='#v'
    elif o=='#vl':
        next='#vl'
    elif o=='#vd':
        next='#vd'
    return file,next,cont

def main():
    print("JSON maker")
    print("commands: #k new key - #v new value - #vl new list value - #e save and exit")
    file=''
    cont=0
    next=''
    nomeFile=''
    while True:
        o=input(">")
        if o=='#e':
            file=file+"}"
            nomeFile=input("Nome del file >")
            f=open(nomeFile,"w")
            f.write(file)
            f.close()
            sys.exit()
        file,next,cont=interpreta(o,file,next,cont)


if __name__ == "__main__":
    main()

domenica 28 marzo 2021

PYTHON - MAKE A JSON FILE FROM DICT

Practical example of how to transform a dictionary into a json file.
The filename is updated using a variable contained in "count.py". Each file is added to the "listalogiche.dat" file.
From here we start for artificial intelligence.
import json
import conteggio
import sys

lista2={}
while True:
v1=input('Parola ')
if v1=='fine':
break
elif v1=='annulla':
sys.exit()
else:
v2=input('comando ')
lista2[v1]=v2
nomefile="comandi"+str(conteggio.conta)+".json"
#save lista2 as .json file
with open(nomefile, "w") as json_file:
json.dump(lista2, json_file)
#aggiornamento dei file di configurazione
f=open("conteggio.py","w")
n=conteggio.conta
n+=1
f.write('conta='+str(n))
f.close()
f=open("listalogiche.dat","a")
f.write(nomefile)
f.close()

domenica 21 marzo 2021

PYTHON and CURSES - Stupid text editor

I needed a light and simple text editor.
Sometimes I need to write micro programs in python or batch, with this little script I solved my problem.
How to use:
py ste.py "filename I want to save"
import sys,os
import curses
#ste : simple(or stupid) text editor

def editor(stdscr):
k = 0
h = 0
cursor_x = 0
cursor_y = 0
stdscr.clear()
stdscr.refresh()
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
while (True):
height, width = stdscr.getmaxyx()
if k == curses.KEY_DOWN:
cursor_y += 1
elif k == curses.KEY_UP:
cursor_y -= 1
elif k == curses.KEY_RIGHT:
cursor_x += 1
elif k == curses.KEY_LEFT:
cursor_x -= 1
elif k == 10: #ENTER
cursor_x=1
cursor_y+=1
h=cursor_y
elif k == 9: #tab
cursor_x = cursor_x +3
elif k == 265: #F1
break
elif k == 266: #
stringa=''
outstr=''
f=open(sys.argv[1],"wb+")
for i in range(h+1):
outstr=stdscr.instr(i, 1) #Read the screen
f.write(outstr)
acapo='\n'
f.write(acapo.encode())
f.close()
else:
stdscr.attron(curses.color_pair(2))
curses.echo()
cursor_x = cursor_x+1
cursor_x = max(0, cursor_x)
cursor_x = min(width-1, cursor_x)
cursor_y = max(0, cursor_y)
cursor_y = min(height-1, cursor_y)
statusbarstr = "Press 'F1' to exit | 'F2' to save | https://danieleporcaripython.blogspot.com"
stdscr.attron(curses.color_pair(1))
stdscr.addstr(height-1, 0, statusbarstr)
stdscr.addstr(height-1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
stdscr.attroff(curses.color_pair(1))
stdscr.move(cursor_y, cursor_x)
stdscr.refresh()
stdscr.attron(curses.color_pair(2))
k = stdscr.getch()

def main():
curses.wrapper(editor)

if __name__ == "__main__":
main()

martedì 16 marzo 2021

Python - AI for TA

 Specialized websites provide indicator-based signals, but they are not valid signals. 

The TA of my software is based on AI and crosses prices and indicators. The power of Python also allows me to search for graphic patterns by "visually" comparing the graph with sample patterns. The calculation of the pivot point is performed automatically, the software chooses the candle to be used and then builds supports and resistances with which to compare the price. Once the analysis is completed, a pdf file is built. The pdf file thus constructed is sent via email. It does not provide long or short entry signals of my choice. The convenience of this software is all in the speed of analysis and "reasoning". The alpha version is ready, anyone wishing to join can write me on Twitter or reply to this post.