vbfunctions.py :  » Language-Interface » VB-to-Python-Converter » vb2py-0.2 » 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 » Language Interface » VB to Python Converter 
VB to Python Converter » vb2py 0.2 » vbfunctions.py
# Created by Leo from: C:\Development\Python22\Lib\site-packages\vb2py\vb2py.leo

"""
Functions to mimic VB intrinsic functions or things
"""

from __future__ import generators

from vbclasses import *
from vbconstants import *
import math
import sys
import fnmatch # For Like
import glob    # For Dir
import os

# << Error classes >>
class VB2PYCodeError(Exception): """An error occured executing a vb2py function"""

class VB2PYNotSupported(VB2PYCodeError): """The requested function is not supported"""



class VB2PYFileError(VB2PYCodeError): """Some kind of file error"""
class VB2PYEndOfFile(VB2PYFileError): """Reached the end of file"""
# -- end -- << Error classes >>
# << VBFunctions >> (1 of 26)
class VBMissingArgument:
  """A generic class to represent an argument omitted from a call"""

  _missing = 1
# << VBFunctions >> (2 of 26)
def vbForRange(start, stop, step=1):
  """Mimic the range in a for statement

  VB's range is inclusive and can include non-integer elements so
  we use an generator. 

  """
  num_repeats = (stop-start)/step
  if num_repeats < 0:
    raise StopIteration
  current = start
  while num_repeats >= 0:
    yield current
    current += step
    num_repeats -= 1
# << VBFunctions >> (3 of 26)
def vbObjectInitialize(size, objtype, preserve=None):
  """Return a new object with the given size and type"""
  #
  # Create the object
  def getObj():
    if len(size) == 1:
      return objtype()
    else:
      return vbObjectInitialize(size[1:], objtype)
  ret = VBArray(size[0], getObj)
  #
  # Preserve the old values if needed
  if preserve is not None:
    preserve.__copyto__(ret)
  return ret
# << VBFunctions >> (4 of 26)
def Left(text, number):
  """Return the left most characters in the text"""
  return text[:number]
# << VBFunctions >> (5 of 26)
def Right(text, number):
  """Return the right most characters in the text"""
  return text[-number:]
# << VBFunctions >> (6 of 26)
def Mid(text, start, num=None):
  """Return some characters from the text"""
  if num is None:
    return text[start-1:]
  else:
    return text[(start-1):(start+num-1)]
# << VBFunctions >> (7 of 26)
def Sgn(num):
  """Return the sign of a number"""
  n = float(num)
  if n < 0:
    return -1
  elif n == 0:
    return 0
  else:
    return 1
# << VBFunctions >> (8 of 26)
def CBool(num):
  """Return the boolean version of a number"""
  n = float(num)
  if n:
    return 1
  else:
    return 0
# << VBFunctions >> (9 of 26)
def Int(num):
  """Return the int of a value"""
  n = float(num)
  if -32767 <= n <= 32767: 
    return int(n)
  else:
    raise ValueError("Out of range in Int (%s)" % n)

def CByte(num):
  """Return the closest byte of a value"""
  n = round(float(num))
  if 0 <= n <= 255: 
    return int(n)
  else:
    raise ValueError("Out of range in CByte (%s)" % n)

def CInt(num):
  """Return the closest int of a value"""
  n = round(float(num))
  if -32767 <= n <= 32767: 
    return int(n)
  else:
    raise ValueError("Out of range in Int (%s)" % n)

def CLng(num):
  """Return the closest long of a value"""
  return long(round(float(num)))
# << VBFunctions >> (10 of 26)
def Sqr(num):
  """Return the square root of a value"""
  return math.sqrt(float(num))

def Sin(num):
  """Return the sin of a value"""
  return math.sin(float(num))

def Cos(num):
  """Return the cosine of a value"""
  return math.cos(float(num))

def Tan(num):
  """Return the tangent of a value"""
  return math.tan(float(num))

def Atn(num):
  """Return the arc-tangent of a value"""
  return math.atan(float(num))
# << VBFunctions >> (11 of 26)
def Log(num):
  """Return the log of a value"""
  return math.log(float(num))

def Exp(num):
  """Return the log of a value"""
  return math.exp(float(num))
# << VBFunctions >> (12 of 26)
def Oct(num):
  """Return the oct of a value"""
  n = CInt(num)
  if n == 0:
    return "0"
  else:
    return oct(n)[1:]
# << VBFunctions >> (13 of 26)
def Hex(num):
  """Return the hex of a value"""
  return hex(CInt(num))[2:].upper()
# << VBFunctions >> (14 of 26)
def Instr(*args):
  """Return the location of one string in another"""
  if len(args) == 2:
    text, subtext = args
    return text.find(subtext)+1
  else:
    start, text, subtext = args
    pos = text[start-1:].find(subtext)
    if pos == -1:
      return 0
    else:
      return pos + start
# << VBFunctions >> (15 of 26)
def Val(text):
  """Return the value of a string

  This function finds the longest leftmost number in the string and
  returns it. If there are no valid numbers then it returns 0.

  The method chosen here is very poor - we just keep trying to convert the 
  string to a float and just use the last successful as we increase
  the size of the string. A Regular expression approach is probably 
  quicker.

  """
  best = 0
  for idx in range(len(text)):
    try:
      best = float(text[:idx+1])
    except ValueError:
      pass
  return best
# << VBFunctions >> (16 of 26)
def IsNumeric(text):
  """Return true if the string contains a valid number"""
  try:
    dummy = float(text)
  except ValueError:
    return 0
  else:
    return 1
# << VBFunctions >> (17 of 26)
def Like(text, pattern):
  """Return true if the text matches the pattern

  The pattern is a string containing wildcards
    * = any string of characters
    ? = any one character

  Fortunately, the fnmatch library module does this for us!

  """
  return fnmatch.fnmatch(text, pattern)
# << VBFunctions >> (18 of 26)
def Seek(channel):
  """Return the current 'cursor' position in the specified channel"""
  return VBFiles.getFile(Int(channel)).tell()+1 # VB starts at 1
# << VBFunctions >> (19 of 26)
_last_files = []

def Dir(path=None):
  """Recursively return the contents of a path matching a certain pattern

  The complicating part here is that when you first call Dir it return the
  first file. Subsequent calls to Dir with no parameters return the other
  files. When all the files are exhausted, we return an empty string.

  Since we need to remember the original path we have to use a global variable
  which is a bit ugly.

  """
  global _last_files
  if path:
    _last_files = glob.glob(path)
  if _last_files:
    return os.path.split(_last_files.pop(0))[1] # VB just returns the filename, not full path
  else:
    return ""
# << VBFunctions >> (20 of 26)
def Input(length, channelid):
  """Return the given number of characters from the given channel"""
  return VBFiles.getChars(channelid, length)
# << VBFunctions >> (21 of 26)
def LCase(text):
  """Return the lower case version of a string"""
  return text.lower()

def UCase(text):
  """Return the lower case version of a string"""
  return text.upper()
# << VBFunctions >> (22 of 26)
def String(num=None, text=None):
  """Return a repeated number of string items"""
  if num is None and text is None:
    return str()
  else:
    return text[:1]*CInt(num)

def Space(num):
  """Return a repeated number of spaces"""
  return String(num, " ")
# << VBFunctions >> (23 of 26)
def Trim(text):
  """Strip spaces from the text"""
  return text.strip()

def Ltrim(text):
  """Strip spaces from the left of the text"""
  return text.lstrip()

def Rtrim(text):
  """Strip spaces from the right of the text"""
  return text.rstrip()
# << VBFunctions >> (24 of 26)
def UBound(obj, dimension=1):
  """Return the upper bound for the index"""
  try:
    return obj.__ubound__(dimension)
  except AttributeError:
    raise ValueError("UBound called for invalid object")


def LBound(obj, dimension=1):
  """Return the lower bound for the index"""
  try:
    return obj.__lbound__(dimension)
  except AttributeError:
    raise ValueError("LBound called for invalid object")
# << VBFunctions >> (25 of 26)
def CreateObject(classname, ipaddress=None):
  """Try to create an OLE object

  This only works on windows!

  """
  if ipaddress:
    raise VB2PYNotSupported("DCOM not supported")
  import win32com.client
  return win32com.client.Dispatch(classname)
# << VBFunctions >> (26 of 26)
Abs = abs
Asc = AscB = AscW = ord
Chr = ChrB = ChrW = chr
Fix = Int
CStr = str
CSng = CDbl = float
Len = len
StrComp = cmp
Round = round

True = 1
False = 0

#
# Command line parameters are retrieved as a whole
Command = " ".join(sys.argv[1:])
# -- end -- << VBFunctions >>
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.