Changeset 7276 for anuga_core/source/anuga/utilities/system_tools.py
- Timestamp:
- Jun 30, 2009, 2:07:41 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/system_tools.py
r7036 r7276 6 6 import sys 7 7 import os 8 import string 8 9 import urllib 9 10 import urllib2 … … 21 22 22 23 fid = open(filename, 'a') 23 if verbose: s24 if verbose: print s 24 25 fid.write(s + '\n') 25 26 fid.close() … … 239 240 print 'Version info stored to %s' %filename 240 241 242 241 243 def safe_crc(string): 242 244 """64 bit safe crc computation. 243 245 244 See Guido's 64 bit fix at http://bugs.python.org/issue1202 246 See http://docs.python.org/library/zlib.html#zlib.crc32: 247 248 To generate the same numeric value across all Python versions 249 and platforms use crc32(data) & 0xffffffff. 245 250 """ 246 251 247 252 from zlib import crc32 248 import os 249 250 x = crc32(string) 251 252 if os.name == 'posix' and os.uname()[4] in ['x86_64', 'ia64']: 253 crcval = x - ((x & 0x80000000) << 1) 254 else: 255 crcval = x 256 257 return crcval 253 254 return crc32(string) & 0xffffffff 258 255 259 256 … … 304 301 # p1 = read_polygon(path) 305 302 306 303 304 ## 305 # @brief Split a string into 'clean' fields. 306 # @param str The string to process. 307 # @param delimiter The delimiter string to split 'line' with. 308 # @return A list of 'cleaned' field strings. 309 # @note Any fields that were initially zero length will be removed. 310 # @note If a field contains '\n' it isn't zero length. 311 def clean_line(str, delimiter): 312 """Split string on given delimiter, remove whitespace from each field.""" 313 314 return [x.strip() for x in str.strip().split(delimiter) if x != ''] 315 316 317 ################################################################################ 318 # The following two functions are used to get around a problem with numpy and 319 # NetCDF files. Previously, using Numeric, we could take a list of strings and 320 # convert to a Numeric array resulting in this: 321 # Numeric.array(['abc', 'xy']) -> [['a', 'b', 'c'], 322 # ['x', 'y', ' ']] 323 # 324 # However, under numpy we get: 325 # numpy.array(['abc', 'xy']) -> ['abc', 326 # 'xy'] 327 # 328 # And writing *strings* to a NetCDF file is problematic. 329 # 330 # The solution is to use these two routines to convert a 1-D list of strings 331 # to the 2-D list of chars form and back. The 2-D form can be written to a 332 # NetCDF file as before. 333 # 334 # The other option, of inverting a list of tag strings into a dictionary with 335 # keys being the unique tag strings and the key value a list of indices of where 336 # the tag string was in the original list was rejected because: 337 # 1. It's a lot of work 338 # 2. We'd have to rewite the I/O code a bit (extra variables instead of one) 339 # 3. The code below is fast enough in an I/O scenario 340 ################################################################################ 341 342 ## 343 # @brief Convert 1-D list of strings to 2-D list of chars. 344 # @param l 1-dimensional list of strings. 345 # @return A 2-D list of 'characters' (1 char strings). 346 # @note No checking that we supply a 1-D list. 347 def string_to_char(l): 348 '''Convert 1-D list of strings to 2-D list of chars.''' 349 350 if not l: 351 return [] 352 353 if l == ['']: 354 l = [' '] 355 356 maxlen = reduce(max, map(len, l)) 357 ll = [x.ljust(maxlen) for x in l] 358 result = [] 359 for s in ll: 360 result.append([x for x in s]) 361 return result 362 363 364 ## 365 # @brief Convert 2-D list of chars to 1-D list of strings. 366 # @param ll 2-dimensional list of 'characters' (1 char strings). 367 # @return A 1-dimensional list of strings. 368 # @note Each string has had right-end spaces removed. 369 def char_to_string(ll): 370 '''Convert 2-D list of chars to 1-D list of strings.''' 371 372 return map(string.rstrip, [''.join(x) for x in ll]) 373 374 ################################################################################ 375 307 376 ## 308 377 # @brief Get list of variable names in a python expression string. … … 396 465 # Get auth info from user if still not supplied 397 466 if httpproxy is None or proxyuser is None or proxypass is None: 398 print '-'* 52467 print '-'*72 399 468 print ('You need to supply proxy authentication information.') 400 469 if httpproxy is None: … … 410 479 else: 411 480 print 'HTTP proxy password was supplied: %s' % '*'*len(proxyuser) 412 print '-'* 52481 print '-'*72 413 482 414 483 # the proxy URL cannot start with 'http://', we add that later … … 482 551 # @param blocksize Size of data blocks to read. 483 552 # @return A hex digest string (16 bytes). 484 # @note Uses MD5 digest .553 # @note Uses MD5 digest if hashlib not available. 485 554 def get_file_hexdigest(filename, blocksize=1024*1024*10): 486 555 '''Get a hex digest of a file.''' … … 501 570 return m.hexdigest() 502 571 503 fd = open(filename, 'r')504 505 572 506 573 ## … … 517 584 fd.close() 518 585 586 519 587 ## 520 588 # @brief Function to return the length of a file.
Note: See TracChangeset
for help on using the changeset viewer.