Συζήτηση
Γεια χαρά, Επισκέπτης
Όνομα χρήστη: Κωδικός: Να με θυμάσαι

ΘΕΜΑ: Άσκηση Δομής Δεδομένων Γνωστή Ως Inverted FIle

Ζητώ τη βοήθειά σας για το qtcosmos 10 Χρόνια 3 Μήνες πριν #2223

  • malos
  • Το Άβαταρ του/της malos
  • Αποσυνδεμένος
  • py____
  • Δημοσιεύσεις: 23
Γεια σας. Χρόνια πολλά σε όλους με υγεία :cheer:
Θέλω να φτιάξω ένα scriptaki που να εκτελέσει κάτι σε python. Αλλά ας τα πω από την αρχή.
Έχω kubuntu και κάποια μέρα έψαχνα για τυχαία plasmoids για προσθέσω στην οθόνη μου.
Έπεσε το μάτι μου σε ένα που το λέγανε mykosmos plasmoid. Όποιος έχει kde μπορεί να το εγκαταστηει με δεξί κλικ στην οθόνη του και "Ξεκλείδωμα συστατικών". Ξανά δεξί κλικ και "Προσθήκη συστατικών". Κάπου εκεί υπάρχει και η Αναζήτηση για νέα συστατικά.

Μου άρεσε η ιδέα να μπορώ να στέλνω SMS από τον υπολογιστή μου και αφού είμαι και χρήστης cosmote
το εγκατέστησα. Υπάρχει μία διαδικασία πρώτα όμως. Στέλνεις από το κινητό σου το αγγλικό E (κεφαλαίο) στο 54000. Σου στέλνουν μετά με μήνυμα τον κωδικό σου για την χρηση της υπηρεσίας.
Τρέχεις το προγραματάκι και βάζεις για username τον αριθμό του κινητού σου απλά και για κωδικό αυτόν που σου έστειλαν και τέλος. Μπορείς να στέλνεις αμέτρητα μηνύματα και πολύ πολύ φτηνότερα.

Όμως δεν θυμάμαι πως προέκυψε και βρήκα ένα παρόμοιο το qtcosmos. Το χρησιμοποιώ και σε gnome και παντού. Δεν είναι plaσmoid αυτό.

Παραθέτω τον κώδικα του qtcosmos.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# qtCosmos v0.6, based on pycosmos
# kaddressbook contacts' loader based on pysmssend
# by dkarnout. Last version: 10/07/09
 
from os import path, environ
from sys import argv, exit
from urllib import urlencode
from httplib import HTTPConnection
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QLabel, QComboBox, QPlainTextEdit, QPushButton, QIcon
from PyQt4.QtGui import QListWidget, QLineEdit, QInputDialog, QMessageBox, QApplication
import os
os.system('clear')
 
def isvd(dsn):
  try:
    testdst = str(int(dsn))
  except: return "!"
  dsn = str(dsn).strip()
  if dsn[:2] == "69": dsn = "0030" + dsn
  if dsn[:1] == "+": dsn = "00" + dsn[1:]
  if not dsn[:2] == "00" or not len(dsn) == 14: return "!"
  return dsn
 
def httpc(url,cookie="",body=None,he='set-cookie',glen=3000):
  if cookie:
    headers={
      "accept-language": "el", "accept-encoding": "gzip, deflate", "cache-control": "no-cache", "cookie": cookie,
      "user-agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 3.51; SV1; .NET CLR 2.0.50727)", 
      "content-type": "application/x-www-form-urlencoded", "connection": "Keep-Alive", "host": "mail.mycosmos.gr",
      "accept": "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
    }
  else: headers={}
  if bool(body): headers['content-length'] = str(len(body))
  try:
    h=HTTPConnection("mail.mycosmos.gr",80)
  except: return "!"
  h.request(["GET","POST"][bool(body)],"/mycosmos/%s.aspx" % url,body,headers)
  resp=h.getresponse()
  if not bool(body):
    try:
      p.gvs=resp.read(glen).split('VIEWSTATE')[1]
      p.gvs=p.gvs.split('"')[2]
    except: p.gvs="!"
  h.close()
  if glen==3000: return resp.getheader(he)
 
def letsend():
  i=0; dst = nlist.currentText().split(";")
  while i < len(dst):
    d2 = nlist.findText(str(dst[i]).strip())
    if d2 > -1:
      dst[i] = p.pl[d2]
    else:
      dsv = isvd(dst[i])
      if dsv == "!":
	noti(dst[i]+" is not a valid mobile number!")
	del dst[i]; i=i-1
      else: dst[i]=dsv
    i=i+1
  if len(dst)==0: return 1
 
  i = len(tsms.toPlainText()); msgl = []
  for j in range(i/140 + bool(i % 140)):
    msgl.append(tsms.toPlainText()[j*140:(j+1)*140])
 
  noti("Getting mycosmos page cookies...")
 
  try:
    test = p.asid
    test = p.vs1
  except:
    h=httpc("login")
    if h=="!": noti("Message not sent. Net connection error."); return 1
    p.asid=h.split()[0].split(';')[0]
    p.vs1=p.gvs
 
  for k in range(len(dst)):
    for j in range(len(msgl)):
      nc = 0; tpr = "%s/%s " % (str(j+1),str(len(msgl)))
      if len(msgl) == 1: tpr = "SMS "
      while True:
	if p.ck == "": getck()
	if not p.vs2 == "!":
	  h=httpc("SMS_Send",p.ck,urlencode({"__VIEWSTATE": p.vs2, "txtMobile": dst[k], "txtMessage": unicode(msgl[j]).encode('utf-8'), "btnSend": "Αποστολή"}),'location')
	  try:
	    if h.split('Success=')[1].split('&')[0] == "True":
	      noti(tpr + "sent successfully!"); os.system('notify-send "Το SMS στάλθηκε επιτυχώς" '); os.system('espeak -v el -p 35 -s 100 -k 1 "Το πήρε! το μύνημα" '); nc = 0; break
	    else:
	      nc = nc + 1; noti(tpr + "not sent. Maybe limit is reached.")
	  except:
	    nc = nc + 1; noti(tpr + "not sent. Unknown error.")
	else:
	  nc = nc + 1; noti("Error. Possibly not a valid account.")
	p.ck = ""
	if nc <= len(p.al) and len(p.al) > 1:
	  a1 = p.al.pop(0); p.al.append(a1)
	  try:
	    lacc.takeItem(0); lacc.addItem(a1)
	  except: pass
	  a1 = p.cl.pop(0); p.cl.append(a1)
	if (len(p.al)==1 and nc==2) or (nc==len(p.al) and len(p.al)>1): return 1
 
def getck():
  noti("Getting cookie for %s account..." % p.al[0])
 
  h1=httpc("login",p.asid,urlencode({"__VIEWSTATE": p.vs1, "tbUsername": p.al[0], "tbPassword": p.cl[0], "btLogin": "Σύνδεση", "rbSecurityPub": "rbSecurityPub"}))
  if h1=="!": p.vs2="!"; return 1
  p.ck = p.asid+"; "+h1.split()[0]+" "+h1.split()[2].split(';')[0]
 
  noti("Getting mycosmos SMS viewstate...")
 
  if httpc("SMS_Send",p.ck,glen=6000)=="!": p.vs2="!"; return 1
  p.vs2=p.gvs
 
class pprop():
  def __init__(self):
    self.al, self.cl, self.pl = [], [], []
    self.ck, self.vs2, self.gvs = "", "", ""
 
class QtCosmos(QDialog):
  def __init__(self):
 
    global nlist, tsms, bsend, bconf, slab, label, mpar
    QDialog.__init__(self,None)
    self.setWindowTitle('qtCosmos 0.6')
    self.setWindowIcon(QIcon("%s48.png" % path.abspath(argv[0]).split(".py")[0]))
    label = QLabel(u'Αριθμός του μαλάκα:', self); nlist = QComboBox(self)
    nlist.setEditable(True); nlist.setInsertPolicy(0)
    nlist.setWhatsThis("  Type here the name (if in phonebook) or the number of the reciever. You may set a list of recievers with '<b>;</b>' among them.")
    label1 = QLabel(u'Γράψε εδώ τις μαλακίες:', self); mpar = QLabel('', self)
    tsms = QPlainTextEdit(self); tsms.setTabChangesFocus(True)
    tsms.setWhatsThis("  Type here your message. Messages longer than <b>140</b> characters will be split.\n  <b>10</b> messages per account per day are allowed.")
    bsend = QPushButton(u'Στείλε', self); bsend.setDefault(True)
    bconf = QPushButton(u'Λογαριασμοί', self)
    slab = QLabel('', self) 
    self.setMinimumSize(255+bconf.sizeHint().width(),149)
 
    label.move(5, 5+nlist.height()/2-label.sizeHint().height()/2); label1.move(5, label.sizeHint().height()+16)
    mpar.move(label1.width()+12, label1.y()); tsms.move(5, mpar.y()+mpar.sizeHint().height()+1)
 
    self.connect(bsend, SIGNAL('clicked()'), letsend)
    self.connect(bconf, SIGNAL('clicked()'), conf)
    self.connect(tsms, SIGNAL('textChanged()'), mpw)
 
  def resizeEvent(self,a):
    p.toSave=True
    i=label.sizeHint().width()
    nlist.setGeometry(i+10, 5, a.size().width()-(i+15), nlist.height())
    tsms.setGeometry(5, tsms.y(), self.width()-bconf.sizeHint().width()-15, self.height()-tsms.y()-label.sizeHint().height()-3)
    bsend.setGeometry(self.width()-bconf.sizeHint().width()-5,tsms.y()+bsend.sizeHint().height()+5,bconf.sizeHint().width(),bconf.sizeHint().height())
    bconf.move(bsend.x(), tsms.y())
    slab.setGeometry(1, tsms.height()+tsms.y()+1, a.size().width(), label.sizeHint().height())
 
  def closeEvent(self,a):
    if not p.toSave: return 0
    try:
      af = open(path.expanduser('~') + "/.qtcosmosrc","w")
      noti("Saving settings...")
      af.write("[Geometry]\n")
      af.write("%s,%s,%s,%s\n" % (self.x(),self.y(),self.width(),self.height()))
      if len(p.al) > 0:
	af.write("[Accounts]\n")
	for i in range(len(p.al)):
	  af.write(p.al[i] + "," + p.cl[i] + "\n")
      af.close()
      noti("Done.")
    except IOError:
      noti("I/O Error. Cannot save accounts and settings.")
 
class QtConf(QDialog):
  def __init__(self):
 
    global lacc, laa, lap, bau, bad, baa, bar
    QDialog.__init__(self,qc)
    self.move(qc.x()+220, qc.y()+99); self.setMinimumSize(295, 150); self.setWindowTitle('qtCosmos Accounts...')
    label2 = QLabel('Accounts:', self); lacc = QListWidget(self)
    laa = QLineEdit(self); laa.setMaxLength(10); lap = QLineEdit(self); lap.setEchoMode(2)
    bau = QPushButton('U&p', self); bad = QPushButton('D&own', self)
    baa = QPushButton('&Add', self); bar = QPushButton('&Remove', self)
 
    print lap.EchoMode()
    label2.move(5, 5)
 
    self.connect(lacc, SIGNAL('itemSelectionChanged()'), asel)
    self.connect(laa, SIGNAL('editingFinished()'), claa)
    self.connect(lap, SIGNAL('editingFinished()'), clap)
 
    self.connect(bau, SIGNAL('clicked()'), cbau)
    self.connect(bad, SIGNAL('clicked()'), cbad)
    self.connect(baa, SIGNAL('clicked()'), cbaa)
    self.connect(bar, SIGNAL('clicked()'), cbar)
 
  def resizeEvent(self,a):
    laa.setGeometry(self.width()/2+2, label.height()+5, self.width()/2-10, laa.sizeHint().height())
    lap.setGeometry(laa.x(), laa.y()+laa.height()+5, laa.width(), laa.height())
    baa.setGeometry(laa.x()+55, lap.y()+lap.height()+5, 80, baa.sizeHint().height())
    bar.setGeometry(baa.x(), baa.y()+baa.height()+4, baa.width(), baa.height())
    bad.setGeometry(laa.x(), bar.y(), 50, baa.height())
    bau.setGeometry(bad.x(), baa.y(), bad.width(), baa.height())
    lacc.setGeometry(10, label.height()+5, self.width()/2-15, self.height()-label.height()-12)
 
def inpD(ask,echomode = 0):
  ask, ok = QInputDialog.getText(q2, 'qtCosmos', ask, echomode)
  return ask
 
def cbaa():
  cr = lacc.currentRow()+1
  while True:
    kacc = inpD("Enter new account name:")
    if len(kacc) == 0: return 1
    try:
      tst = str(int(kacc))
    except: tst = "~INT"
    if not (kacc[:2] == "69" or len(kacc) == 10) or tst == "~INT":
      QMessageBox.warning(q2,'qtCosmos', "This is not a valid account.")
    else: break
  kpas = inpD("Enter account's password:",2)
  if len(kpas) == 0: return 1
  p.al.insert(cr,kacc); p.cl.insert(cr,kpas); lacc.insertItem(cr,kacc)
  lacc.setCurrentRow(cr); p.toSave = True
 
def cbau():
  cr = lacc.currentRow()
  if cr < 1: return 1
  aa=p.al.pop(cr); p.al.insert(cr-1,aa)
  cc = p.cl.pop(cr); p.cl.insert(cr-1,cc)
  lacc.takeItem(cr); lacc.insertItem(cr-1,aa)
  lacc.setCurrentRow(cr-1); p.toSave = True
 
def cbad():
  cr = lacc.currentRow()
  if cr == -1 or cr == len(p.al)-1: return 1
  aa=p.al.pop(cr); p.al.insert(cr+1,aa)
  cc = p.cl.pop(cr); p.cl.insert(cr+1,cc)
  lacc.takeItem(cr); lacc.insertItem(cr+1,aa)
  lacc.setCurrentRow(cr+1); p.toSave = True
 
def cbar():
  cr = lacc.currentRow()
  if len(p.al) == 0: return 1
  p.al.pop(cr); p.cl.pop(cr); lacc.takeItem(cr); p.toSave = True
 
def claa():
  if lacc.count()==0: laa.setText(""); return 0
  cr = lacc.currentRow(); tt = laa.text()
  if tt == p.al[cr]: return 0
  try:
    tst = str(int(tt))
  except: tst = "~INT"
  if not (tt[:2] == "69" or len(tt) == 10) or tst == "~INT":
    laa.setText(p.al[cr])
    QMessageBox.warning(q2,'qtCosmos', "This is not a valid account.")
    return 1
  p.al.insert(cr,tt); p.al.pop(cr+1); lacc.insertItem(cr,tt); lacc.takeItem(cr+1)
  p.toSave = True
 
def clap():
  if lacc.count()==0: lap.setText(""); return 0
  cr = lacc.currentRow(); tt = lap.text()
  if tt == p.cl[cr]: return 0
  p.cl.insert(cr,tt); p.cl.pop(cr+1); p.toSave = True
 
def asel():
  cr = lacc.currentRow()
  if len(p.al) == 1: cr=0
  if cr == -1:
    laa.setText(""); lap.setText("")
  else:
    laa.setText(p.al[cr]);  lap.setText(p.cl[cr])
 
def noti(txt):
  slab.setText(txt)
  print txt
  return 0
 
def mpw():
  t = tsms.toPlainText()
  i = len(t)+t.count("[")+t.count("]")+t.count("{")+t.count("}")+t.count("|")+t.count("~")+t.count("^")+t.count("\\")+t.count("€")
  i = i/140 + bool(i % 140)
  if i == 1:
    t1 = ")"
  else: t1 = "s)"
  mpar.setText("(%s message" % str(i) + t1)
 
def conf():
  global q2
  q2=QtConf()
  if lacc.count() == 0: lacc.addItems(p.al)
  try: lacc.setCurrentRow(0)
  except: pass
  q2.show()
 
def getcontacts():
  fur=environ["HOME"] + "/.kde%s/share/apps/kabc/std.vcf"
  if path.exists(fur % "4"):
    val="4"
  elif path.exists(fur % ""):
    val=""
  else:
    return 1
  try:  hf = open(fur % val,"r")
  except IOError: return 1
  text = hf.readlines()
  hf.close()
  for i in text:
    if i[0:3] == "FN:":
      nam=i[3:]
    if i[0:14] == "TEL;TYPE=CELL:":
      phon=isvd(i[14:].strip())
      if phon == "!":
	pass
      else:
	nlist.addItem(nam.strip())
	p.pl.append(phon)
  nlist.setEditText("")
 
if __name__ == "__main__":
  app = QApplication(argv)
  global qc, p
  p = pprop(); qc = QtCosmos()
  try:
    afile = open("%s/.qtcosmosrc" % path.expanduser('~'),"r")
    while afile:
      buf = afile.readline()
      if len(buf) == 0: break
      if buf[:4] == "[Geo":
	ltyp="geo"
      elif buf[:4] == "[Acc":
	ltyp="acc"
      else:
	buf = buf.split(",", [1,4][bool(ltyp=="geo")])
	if ltyp == "geo":
	  qc.setGeometry(int(buf[0]),int(buf[1]),int(buf[2]),int(buf[3]))
	elif ltyp == "acc":
	  p.al.append(buf[0])
	  p.cl.append(buf[1][:len(buf[1])-1])
    afile.close()
    if getcontacts() == 1:
      noti("Could not open/find address book.")
    else:
      noti("Ready.")
  except IOError:
    noti("[!] Did not find accounts and settings.")
  qc.show()
  exit(app.exec_())

Το ζητούμενο μου είναι:

Μπορεί κάποιος να με βοηθήσει να φτιάξω ένα script όπου θα το εκτελώ με κλικ και να στέλνει ένα προκαθορισμένο
κείμενο σε προκαθορισμένο αριθμό; Αντί δηλαδή να ανοίγω το πρόγραμμα qtcomos.py όπου εκεί μου ανοίγει ένα παραθυράκι και έχει πεδία για το νούμερο και το κείμενο καθώς και ένα κουμπάκι Send, να κάνω απλά ένα κλικ στο σκριπτάκι και αυτό να σταλεί το SMS


Ξέρω είναι γαϊδουριά αλλά μην νομίζετε. Το ψάχνω κι εγώ. Προσανατολιζομαι κοντά στις γραμμές που λέει Send όπου κάπου εκεί θα είναι η μεταβλητή για το κείμενο και τον αριθμό τηλεφώνου καθώς και κάποιο if όπου θα ορίζει αν πατήθηκε το κουμπί Send.
Από αυτά ελπίζω ότι θα πάρω χαμπάρι κάτι και θα το φτιάξω, αλλά προς στιγμήν είναι θολό το τοπίο.

Ευχαριστώ.
Πρέπει να είστε εγγεγραμμένο μέλος του Φόρουμ για να κάνετε μια δημοσίευση.

Άσκηση Δομής Δεδομένων Γνωστή Ως Inverted FIle 8 Χρόνια 4 Εβδομάδες πριν #3557

  • babaliaris1
  • Το Άβαταρ του/της babaliaris1
  • Αποσυνδεμένος
  • python
  • Δημοσιεύσεις: 445
  • Ληφθείσες Ευχαριστίες 75
GitHub Project: Link

Το πρόγραμμα μπορεί να πάρει αρχεία και να δημιουργήσει μια δομή δεδομένων για πολύ γρήγορη αναζήτηση λέξεων σε αυτά τα κείμενα. Η αναζήτηση επιστρέφει τα ονόματα των αρχείων και σε ποια θέση σε κάθε αρχείο υπάρχει μια συγκεκριμένη λέξη. Έχω κάνει και ένα απλό user interface για δοκιμή. Επίσης στο project στο GitHub υπάρχουν και δοκιμαστικά αρχεία κειμένου.


Υπάρχουν μικρά προβλήματα που δεν τα έχω λύση. Για παράδειγμα υπάρχει περίπτωση να αναζητάς μια
λέξη που υπάρχει σίγουρα μέσα στα αρχεία κειμένων, αλλά ο αλγόριθμός σε λέει ότι η λέξη δεν
βρέθηκε. Επίσης καμιά φορά δημιουργήτε κάποιο infinity loop και το πρόγραμμα κολλάει :p
Κατά τα άλλα είναι μια χαρά.

Θα ποστάρω και εδώ τους κώδικες σε περίπτωση που απλώς θέλετε να τους ρίξετε μια ματιά.


Κλάση WordReader:
import sys, os
 
 
#This class can create objects which can be used to read words from files.
 
class WordReader:
 
    def __init__(this, filename):
 
        try:
            this.file = open(filename, "r")
 
        except FileNotFoundError:
            print("Error: File \""+filename+"\" not found.")
            sys.exit(0)
 
        this.position = 0 #The current position into the file.
        this.realSize = 0 #The real size(including unwanted symbols) of the nextWord.
 
        #Unwanted Symbols.
        this.unwanted = ["\"", "\n", ";", ",","'", "(", ")", "[", "]",
                         ":", "\t", "-", "_", "+", "-"]
 
        #Symbols that ends a word.
        this.wordEnd  = [" ", ".", "!", "?"]
 
        #Size of the file.
        this.fileSize = os.path.getsize(filename)
 
        #How many words i have read.
        this.words    = 0
 
 
 
    #==========================This method returns the next word into the file==========================#
    def nextWord(this):
 
        word = ""
        this.realSize = 0
 
        while True:
            add  = True
 
            char = this.file.read(1)
            this.position += 1
            this.realSize += 1
 
            #Checking for loop break.
            for c in this.wordEnd:
                if char == c and word != "":
                    this.words += 1
                    return word
 
                elif char == c and word == "":
                    return None
 
            #Check for unwanted symbols.
            for c in this.unwanted:
                if char == c:
                    add = False
                    break
 
            #Add the char to the word string.
            if add:
                word += char
    #==========================This method returns the next word into the file==========================#
 
 
    #This method seek a position into the file to start reading from there.
    def seek(this, byte):
        this.file.seek(byte)
        this.potision = byte
 
    #This returns true of the file is not over.
    def hasNext(this):
        return not this.fileSize <= this.position+1
 
    #This method closes the file.
    def close(this):
        this.file.close()
 


Κλάση DataBase:
from WordReader import WordReader
 
 
 
#This class contains the methods to create the data structure.
 
class DataBase:
 
    def __init__(this, filenames):
 
        this.filenames  = filenames
        this.pages      = 0
        this.dictionary = []
        this.index      = []
        this.aditionalBuffers = []
 
 
    #=======================This method creates the dictionary and the index structure in main memory=======================#
    def createOnMemory(this):
 
        pages = 0
 
 
        #Looping through all given files.
        print("Please wait for data base creation...\n")
        for filename in this.filenames:
            reader = WordReader(filename) #Opening Current File.
 
            print("\tThe program now reads:",this.nameFromPath(filename))
 
            #Getting all the words from the file.
            while (reader.hasNext()):
 
                #Reading the next word.
                word = reader.nextWord()
 
                #If it's a proper world.
                if word != None:
 
                    #Calculating the start position of the word into the file.
                    pos = reader.position - reader.realSize
 
                    #Adding the very first word into the dictionary.
                    if len(this.dictionary) == 0:
 
                        this.dictionary.append(word+";"+str(pages)) #Adding the word and the page(pointer) in the dictionary.
                        this.index.append(this.nameFromPath(filename)+","+str(pos)+";") #Adding the index page with the filename and the position of the word.
 
                    #ELse
                    else:
 
                        found = False
 
                        #Checking to see if the word already exists in the dictionary.
                        for string in this.dictionary:
 
                            #If exists, then just update the index.
                            if this.substring(string) == word:
                                found = True
 
                                ind = int(this.substring(string, False))
                                dataString = this.index[ind]
                                dataString += this.nameFromPath(filename)+","+str(pos)+";"
                                this.index[ind] = dataString
                                break
 
                        #If not exist, then add the word in the dict, and create a new index page.
                        if not found:
                            pages += 1
                            this.dictionary.append(word+";"+str(pages))
                            this.index.append(this.nameFromPath(filename)+","+str(pos)+";")
 
            reader.close()
            print("\tDone reading!\n")
 
        this.pages = pages
 
        print("\nThe data base has been created successfuly!")
    #=======================This method creates the dictionary and the index structure in main memory=======================#
 
 
 
    #===================================This method saves the structures into two files=====================================#
    #I currently working on it!
    def saveOnDisk(this):
 
        #Create the dictionary.        
        this.createDictionary()
 
 
        #---------------------Creating the index----------------------#
        #Opening a file.
        file = open("index.dat",
                    "wb")
 
        #Looping through all index of main memory.
        for s in this.index:
            s = list(s)
 
            #Saving it in a file.
            this.createIndex(s, file, call_back = False)
 
 
        #Writting aditional pages into the file.
        for buff in this.aditionalBuffers:
            for c in buff:
                file.write(str.encode(c))
        #---------------------Creating the index----------------------#
 
        #Process ended!!!
        print("Data base has been saved successfuly!")
    #===================================This method saves the structures into two files=====================================#
 
 
 
 
 
 
 
 
 
 
    #--------------------------------------------------Private Methods--------------------------------------------------#
 
 
 
    #I havent finish this method yet. Im trying to create the index file.
    def createIndex(this, string,  file, call_back = False):
 
        #Creating the bufffer.................#
        buffer = []
        index  = 0
        for i in range(0, 128):
            buffer.append(' ')
 
 
        #If the string fits the buffer-1
        if len(string) <= 128 - 1:
 
            #Copy the characters into the buffer.
            for c in string:
                buffer[index] = c
                index += 1
 
            #Close the buffer.
            buffer[index] = '\n'
 
 
            #If this is the first call of this function save this page.
            if not call_back:
                for c in buffer:
                    file.write(str.encode(c))
 
            #Else add it to the aditionalBuffers list.
            else:
                this.aditionalBuffers.append(buffer)
 
 
        #The string can't fit into the fuffer-1.
        else:
 
            #Create a new page.
            this.pages += 1
 
            #Save the first 120 characters in the buffer.
            for i in range(0, 120):
                buffer[index] = string[0]
                string.remove(string[0])
                index += 1
 
            #Start the pointer number,
            buffer[index] = '?'
            index += 1
 
 
            #Save into the buffer the number that points to the new page that i will create.
            for c in list(str(this.pages)):
                buffer[index] = c
                index += 1
 
            #Close the pointer number.
            buffer[index] = '?'
 
 
            #If this is the first call of the function the write this buffer into the file.
            if not call_back:
                for c in buffer:
                    file.write(str.encode(c))
 
            #Else add it to the aditionalBuffers list.
            else:
                this.aditionalBuffers.append(buffer)
 
 
            #Call this function again and again and again until finish writing all the character of the string.
            this.createIndex(string,  file,  call_back = True)
 
 
 
 
 
 
 
 
 
 
 
 
 
    #This method creates the dictionray file.
    def createDictionary(this):
 
        this.dictionary.sort()
 
        file = open("dictionary.dat",
                    "wb")
 
        #====================================Creating The Dictionary File====================================#
        #Creating the buffer.
        size   = 0
        buffer = []
        a = 0
        for i in range(0, 128):
            buffer.append(' ')
 
        for s in this.dictionary:
 
            s = list(s)
 
            if len(s)+1 <= 128 - size - 1: #-1 for end of page character.
                for c in s:
                    buffer[size] = c
                    size += 1
 
                #Closing the string.
                buffer[size] = '\0'
                size += 1
 
            else:
                buffer[size] = '\n' # End of page.
 
                #Write the buffer us bytes.
                for c in buffer:
                    file.write(str.encode(c))
                a += 128
 
                #Empty the buffer.
                size = 0
                buffer = []
                for i in range(0, 128):
                    buffer.append(' ')
 
                #Write the string which could not fit in the previous buffer.
                for c in s:
                    buffer[size] = c
                    size += 1
 
                #Closing the string.
                buffer[size] = '\0'
                size += 1
 
        #Last page.
        buffer[size] = '\n' # End of page.
 
        #Write the buffer us bytes.
        for c in buffer:
            file.write(str.encode(c)) #Write the buffer us bytes.
        a += 128
        file.close()
        print(a)
        #====================================Creating The Dictionary File====================================#
 
 
 
    #This method gives a substring from a string with this syntax: "word;page_number"
    #if before = True then substring = "word" else substring = "page_number".
    def substring(this, string, before = True):
 
        string = list(string)
        sub    = ""
 
        if before:
            for char in string:
                if char == ";":
                    return sub
 
                sub += char
 
        else:
            start = False
            for char in string:
                if start:
                    sub += char
 
                if char == ";":
                    start = True
 
            return sub
 
 
    #This method takes a path and returns the name of a file.
    #For example if path = "/home/user_name/downloads/file.txt" -----> file.txt
    def nameFromPath(this, path):
 
        path = list(path)
        slashes = 0
        name = ""
 
        for c in path:
            if c == '/':
                slashes += 1
 
 
        while(slashes > 0):
            if path[0] == '/':
                slashes -= 1
 
            path.remove(path[0])
 
 
        for c in path:
            name += c
 
        return name
    #--------------------------------------------------Private Methods--------------------------------------------------#
 


Κλάση BinarySearch:
import sys, os
 
class BinarySearch:
 
 
    #=======================Constructor=======================#
    def __init__(this, filename):
 
        #Trying to open the file.
        try:
            this.file = open(filename, "rb")
 
 
        #Failed.
        except FileNotFoundError:
            print("File: \""+filename+"\" could not been found!")
            sys.exit(0)
 
        #Getting the size of the file.
        this.size = os.path.getsize(filename)
 
        #Calculating the amount of the pages.
        this.pages = int(this.size / 128)
        this.pageArray = []
    #=======================Constructor=======================#
 
 
 
 
    #=======================================Binary Search Algorithm=======================================#
    def search(this, word):
 
        low  = 0 
        high = this.pages - 1
 
        while True:
 
            words = []        #This list keeps all the words that are inside the page.
            pagePointers = [] #This keeps all the number pages that its word points to.
 
            #Finding the middle of the file pages.
            halfPage = (low + high) // 2 
 
            #Seeking the page.
            this.file.seek(halfPage * 128)
 
            #Reading the page.
            data = this.file.read(128)
 
            #Creating a character buffer.
            buffer = list(bytes.decode(data))
 
 
            #-----------Getting the data strings from the buffer-----------#
            string = ""
            dataStrings = []
            for c in buffer:
 
                #End of page.
                if c == '\n':
                    break
 
                #End of data string.
                elif c == '\0':
                    dataStrings.append(string)
                    string = ""
 
                else:
                    string += c
            #-----------Getting the data strings from the buffer-----------#
 
 
 
 
            #------------Getting the data from the data strings------------#
            for st in  dataStrings:
                string = ""
                st = list(st)
 
                for c in st:
 
                    #Until this character, its the word.
                    if c == ';':
                        words.append(string)
                        string = ""
 
                    else:    
                        string += c
 
                #After that character is the page number.
                pagePointers.append(int(string))
            #------------Getting the data from the data strings------------#
 
 
 
 
            #If the word is less than the first word of the page
            #then search the left half of the file.
            if word.lower() < words[0].lower():
                high = halfPage - 1
 
            #If the word is greater than the last word of the page
            #then search the right half of the file.
            elif word.lower() > words[ len(words)-1 ].lower():
                low = halfPage + 1
 
 
            #Else the word that i'm looking for is somewhere inside the current page.
            else:
                for i in range(0, len(words)):
                    if words[i].lower() == word.lower():
                        return pagePointers[i]
 
                return None
    #=======================================Binary Search Algorithm=======================================#
 
 
 
    #Closing the object.
    def close(this):
        this.file.close()
 
 



Main File:
# -*- coding: UTF-8 -*-
#!/user/bin/python34
 
from DataBase import DataBase
from BinarySearch import BinarySearch
 
 
 
 
#This is just for show :p 
def filesOption():
 
    filenames = []
 
    while True:
        print("\n")
 
        print("==========Add Files==========")
        print("1) From current directory")
        print("2) From full path")
        print("3) Back")
        print("==========Add Files==========\n")
 
        choose = input("Make your choise: ")
 
        if choose == "1":
 
            while True:
 
                filename = input("\nGive filename(/stop): ")
 
                if filename == "/stop":
                    return filenames
 
                filenames.append(filename)
 
 
        elif choose == "2":
 
            while True:
 
                filename = input("\nGive full path(/stop): ")
 
                if filename == "/stop":
                    return filenames
 
                filenames.append(filename)
 
        elif choose == "3":
            return None
 
 
 
 
 
 
#-------------------This function gets the data from the index file.--------------------#
def getData(page):
 
 
    #If page is none, the binary search didnt found the word in the dictionary.
    if (page == None):
        print("\nThe word could not been found!")
        return
 
 
    #_________Variables_________#
    file = open('index.dat', 'rb')
    DataStrings = []
    string = ""
    done   = False
    #_________Variables_________#
 
 
 
    #------In this loop i read all the pages that are connected together------#
    while not done:
 
        file.seek(page * 128) #Seeking current page.
        data = file.read(128) #Reading the page.
        buff = list(bytes.decode(data)) #Creating a list of characters.
 
 
        nextPage  = ""
        startPage = False
 
        for c in buff:
 
            #Create the pointer number that points to the next page.
            if startPage:
 
                #End of creation, now go to the next page.
                if c == "?":
                    page = int(nextPage)
                    break
 
                nextPage += c
 
            #Pages ended. Done!
            elif c == '\n':
                done =True
                break
 
            #After this character, the point number starts.
            elif c == '?':
                startPage = True
 
 
            #End of a data string.
            elif c == ';':
                DataStrings.append(string)
                string = ""
 
            else:
                string += c
    #------In this loop i read all the pages that are connected together------#
 
 
 
    #--------------Getting the data from the data strings--------------#
    filenames = []
    positions = []
    string    = ""
 
    #Looping through all data strings.
    for s in DataStrings:
        s = list(s) #Creating a character list from the data string.
 
        #Looping through the characters.
        for c in s:
 
            #Before this character its the name of the file.
            if c == ',':
                filenames.append(string)
                string = ""
 
            else:
                string += c
 
        #After the character ',' is the number of the position of the word in the file.
        positions.append(int(string))
        string = ""
    #--------------Getting the data from the data strings--------------#
 
 
    #Printing the results.
    print("==================Printing Results==================")
    for i in range(0, len(filenames)):
        print(str(i+1)+") "+filenames[i]+" / "+str(positions[i]))
    print("==================Printing Results==================")
 
 
 
 
    #CLosing the file.
    file.close()
#-------------------This function gets the data from the index file.--------------------#
 
 
 
 
 
 
 
#-----------------This function finds a sequence in a file by position.-----------------#
def getSequence(filename, position):
 
    file = open(filename, 'r')
 
    file.seek(position)
 
    print("\n========================\nSequence: ")
    print("\t",file.read(30))
    print('========================')
 
    file.close()
 
    return
#-----------------This function finds a sequence in a file by position.-----------------#
 
 
 
 
 
#------------------------------User Interface------------------------------#
while True:
    print("\n")
 
    print("========Main Menu========")
    print("1) Create data structure")
    print("2) Search word")
    print("3) See a Sequence")
    print("4) Quit")
    print("========Main Menu========\n")
 
    choose = input("Make your choise: ")
 
    #Create data structure.
    if choose == "1":
        filenames = filesOption()
 
        if (filenames == None):
            continue
 
        base = DataBase(filenames)
        base.createOnMemory()
        base.saveOnDisk()
 
 
    #Search for a word.
    elif choose == "2":
        word = input("Give a key word: ")
        search = BinarySearch("dictionary.dat")
        getData(search.search(word))
        search.close()
 
 
    #See a sequence.
    elif choose == "3":
        filename = input("Give the filename: ")
 
        try:
            pos      = int(input("Give start position: "))
 
        except ValueError:
            print("You have to give an integer.")
            continue
 
        getSequence(filename, pos)
 
    #Close the program.
    elif choose == "4":
        break
#------------------------------User Interface------------------------------#
 
Τελευταία διόρθωση: 8 Χρόνια 4 Εβδομάδες πριν από babaliaris1.
Πρέπει να είστε εγγεγραμμένο μέλος του Φόρουμ για να κάνετε μια δημοσίευση.
Συντονιστές: pmav99
Χρόνος δημιουργίας σελίδας: 0.823 δευτερόλεπτα

Μοιράσου το!

Powered by CoalaWeb

Λίστα Ταχυδρομείου