pyNastran  0.5.0
pyNastran BDF Reader/Writer, OP2 Parser, and GUI
general.py
Go to the documentation of this file.
00001 ## GNU Lesser General Public License
00002 ## 
00003 ## Program pyNastran - a python interface to NASTRAN files
00004 ## Copyright (C) 2011-2012  Steven Doyle, Al Danial
00005 ## 
00006 ## Authors and copyright holders of pyNastran
00007 ## Steven Doyle <mesheb82@gmail.com>
00008 ## Al Danial    <al.danial@gmail.com>
00009 ## 
00010 ## This file is part of pyNastran.
00011 ## 
00012 ## pyNastran is free software: you can redistribute it and/or modify
00013 ## it under the terms of the GNU Lesser General Public License as published by
00014 ## the Free Software Foundation, either version 3 of the License, or
00015 ## (at your option) any later version.
00016 ## 
00017 ## pyNastran is distributed in the hope that it will be useful,
00018 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 ## GNU General Public License for more details.
00021 ## 
00022 ## You should have received a copy of the GNU Lesser General Public License
00023 ## along with pyNastran.  If not, see <http://www.gnu.org/licenses/>.
00024 ## 
00025 
00026 from __future__ import division, print_function
00027 import os
00028 from numpy import matrix
00029 
00030 def printBadPath(path):
00031     msg = ''
00032     fullpath = os.path.abspath(path)
00033     while fullpath:
00034         if os.path.exists(fullpath):
00035             msg += 'passed:  %s\n' %(fullpath)
00036         else:
00037             msg += 'failed:  %s\n' %(fullpath)
00038         fullpath = os.path.dirname(fullpath)
00039     return msg
00040 
00041 def getFilesOfType(dirname,extension='.txt',maxSize=100.):
00042     """
00043     gets all the files in the specified directory with a given extension
00044     @param dirname the directory name
00045     @param extension list of filetypes to get (default='.txt')
00046     @param maxSize size in MB for max file size
00047     """
00048     files = os.listdir(dirname)
00049     files2 = []
00050     for fname in files:
00051         (f,ext) = os.path.splitext(fname)
00052         #print f
00053         #print ext
00054         if extension in ext:
00055             f = os.path.join(dirname,fname)
00056             if os.path.getsize(f)/(1024.*1024.) <= maxSize: # convert to MB
00057                 files2.append(f)
00058             ###
00059         ###
00060     ###
00061     return files2
00062 
00063 def deObscure(num):
00064     """
00065     unpacks an "obscured" number...similar to binary, but base 52
00066     A base 52 value takes up a fewer characters than a base 10 number
00067     which helps to do Mat12345678 when there's an 8 character limit on variable names.
00068     """
00069     print("***")
00070     print("type(num) = ",type(num))
00071     num.reverse()
00072     vals = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
00073             'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',]
00074     #vals = ['0','1']
00075     dictA = {}
00076     n = len(vals)
00077     for i in xrange(n):
00078         dictA[vals[i]] = i
00079 
00080     print("n = ",n)
00081     val = 0
00082     for i,letter in enumerate(reversed(num)):
00083         print("letter = ",letter)
00084         val += dictA[letter]*n**i
00085         print("factor = ",dictA[letter]*n**i)
00086     print("val = ",val)
00087     return val
00088 
00089 def obscure(num):
00090     """
00091     takes a large number and shrinks it down...similar to binary, but base 52
00092     A base 52 value takes up a fewer characters than a base 10 number
00093     which helps to do Mat12345678 when there's an 8 character limit on variable names.
00094     """
00095     lenNum = len(str(num))
00096     vals = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
00097             'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',]
00098     #vals = ['0','1']
00099             #'0','1','2','3','4','5','6','7','8','9']  # 0,1,2,...
00100     dictA = {}
00101     n = len(vals)
00102     for i in xrange(n):
00103         dictA[i] = vals[i]
00104 
00105     pack = []
00106     i = 0
00107     while num > 0:
00108         print("num = %s" % (num))
00109         print("factor = %s" % (num%n))
00110         var = dictA[num%n]
00111         num = num/n
00112         pack.append(var)
00113         i += 1
00114         if i == 100:
00115             break
00116     print(pack)
00117     print('')
00118     print("%s > %s" % (lenNum, len(pack)))
00119     return pack
00120     
00121 
00122 def is_binary(filename):
00123     """
00124     Return true if the given filename is binary.
00125     @raise EnvironmentError: if the file does not exist or cannot be accessed.
00126     @attention: found @ http://bytes.com/topic/python/answers/21222-determine-file-type-binary-text on 6/08/2010
00127     @author: Trent Mick <TrentM@ActiveState.com>
00128     @author: Jorge Orpinel <jorge@orpinel.com>
00129     @warning this may not work for str...
00130     """
00131     fin = open(filename, 'rb')
00132     try:
00133         CHUNKSIZE = 1024
00134         while 1:
00135             chunk = fin.read(CHUNKSIZE)
00136             if b'\0' in chunk: # found null byte
00137                 return True
00138             if len(chunk) < CHUNKSIZE:
00139                 break # done
00140     finally: # no need for ???
00141         fin.close()
00142 
00143     return False
00144 
00145 def ListPrint(listA):
00146     """
00147     Prints a list, numpy array, or numpy matrix in an abbreviated format.
00148     Useful for debugging.
00149     @param listA list, numpy array, or numpy matrix
00150     @retval msg the clean string representation of the object
00151     """
00152     if len(listA) == 0:
00153         return '[]'
00154     ###
00155 
00156     msg = '['
00157     if isinstance(listA, matrix):
00158         (nrows, ncols) = listA.shape
00159         for irow in xrange(nrows):
00160             msg += '['
00161             for icol in xrange(ncols):
00162                 msg += '%-10g,' %(listA[irow, icol])
00163             msg = msg[:-1]
00164             msg += '],\n '
00165         msg = msg[:-1]
00166         msg += ']'
00167 
00168     else:
00169         for a in listA:
00170             #print "a = ", a, type(a)
00171             if isinstance(a, str):
00172                 msg += ' %s,' % (str(a))
00173             elif a is None:
00174                 msg += ' None,'
00175             elif isinstance(a, float):
00176                 msg += ' %-4.2f,' % (a)
00177             elif isinstance(a, int):
00178                 msg += ' %g,' % (a)
00179             else:
00180                 try:
00181                     msg += ' %g,' % (a)
00182                 except TypeError:
00183                     print("a = |%s|" % (a))
00184                     raise
00185                 ###
00186             ###
00187         ###
00188         msg = msg[:-1]
00189         msg += ' ]'
00190     ###
00191     return msg
00192 
00193 if __name__=='__main__':
00194     n = 99999999
00195     o = obscure(n)
00196     print(''.join(o))
00197     deObscure(o)
 All Classes Namespaces Files Functions Variables