thanval.py :  » Business-Application » ThanCad » thancad-0.0.9 » p_gtkwid » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » ThanCad 
ThanCad » thancad 0.0.9 » p_gtkwid » thanval.py
#!/usr/bin/python
from Tkinter import *
from p_ggen import thanUnicode
from thanwidstrans import T

##############################################################################
##############################################################################

class ThanValidator:
    "Common validation code."
   
    def __init__(self, title=""):
        "Just set error messages to no error."
        self.thanClearErr()

    def thanValidate(self, v):
        "Validate the value v and return the correct value."
  return v
  
    def thanGet(self, v):
        "Try to return a correct value without validation."
        return v

    def thanSetErr(self, i, t):
        "Set error messages to given error."
        self.thanIerror = i
        self.thanTerror = t
  
    def thanClearErr(self):
        "Set error messages to no error."
        self.thanIerror = 0
  self.thanTerror = ""
  
    def thanGetErr(self):
        "Return the text error message."
        return self.thanTerror
    
    def thanGetIerr(self):
        "Return the code of the error message."
        return self.thanIerror

    
class ThanValUni(ThanValidator):
    "Validate unicode text."
    
    def thanValidate(self, v):
        "Validate the value v and return the correct value."
  return thanUnicode(v)
  
    def thanGet(self, v):
        "Try to return the correct value."
  return thanUnicode(v)


class ThanValBlank(ThanValUni):
    "Validate that (unicode) text is non blank."
    
    def thanValidate(self, v):
        "Validate the value v and return the correct value."
  v = ThanValUni.thanValidate(self, v)
        if len(v.strip()) > 0: return v
        self.thanSetErr(1, "Non blank unicode string was expected.")
        return None


##############################################################################
##############################################################################

class ThanValFloat(ThanValidator):
    "Get and validate a float number."

    def num(self, a): return float(a)
    tnumexp = "A float was expected."
    tnumbounds = "bounds should be a tuple of float values."
    tnumbet = "A float was expected between"
    
    def __init__(self, vmin=None, vmax=None):
        ThanValidator.__init__(self)
  self.thanBounds = vmin, vmax
        if self.thanBounds != (None, None):
            try: self.thanBounds = [self.num(self.thanBounds[i]) for i in (0,1)]
            except ValueError, IndexError: raise ValueError, self.tnumbounds

    def thanValidate(self, v):
        try: v = self.num(v)
        except ValueError:
            self.thanSetErr(1, self.tnumexp)
            return None
        if self.thanBounds != (None, None):
            if v < self.thanBounds[0] or v > self.thanBounds[1]:
          v = [str(self.thanBounds[i]) for i in (0,1)]
    v.insert(0, self.tnumbet)
                self.thanSetErr(1, " ".join(v))
                return None
        self.thanClearErr()
        return v

    def thanGet(self, v):
        try: v = self.num(v)
        except: pass
        return v
    

class ThanValFloatFortran(ThanValFloat):
    "Get and validate a float number and checks for Fortran idiosyngracies."

    def thanValidate(self, v):
        "Check if exponenet is with D instead of E."
        v1 = ThanValFloat.thanValidate(self, v)
  print "fortran:", v1
  if v1 != None: return v1
  try: v+""; v.replace
  except: return None     # Not stringlike enough
  print "TRYING FORTRAN:'", v
  v = v.replace("d", "e")
  v = v.replace("D", "e")
  print "after:", v
  return ThanValFloat.thanValidate(self, v)
  

##############################################################################
##############################################################################

class ThanValInt(ThanValFloat, Entry):
    "Get and validate an integer number."
    
    def num(self, a):
        return int(a)
    tnumexp = "An integer was expected."
    tnumbounds = "bounds should be a tuple of integer values."
    tnumbet = "An integer was expected between"


##############################################################################
##############################################################################

if __name__ == "__main__" and 1:
    def validates():
        print "a=", b.thanValidate(a.thanGet()),
  print b.thanGetErr(), b.thanGetIerr()
  
    root = Tk()
    from p_gtkwid import ThanEntry
    a = ThanEntry(root, bg="green")
    b = ThanValInt(0, 1)
    a.grid()
    but = Button(root, text="Validate", command=validates)
    but.grid()
    root.mainloop()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.