Menu

[r6210]: / trunk / toolkits / basemap / lib / dap / lib.py  Maximize  Restore  History

Download this file

123 lines (91 with data), 3.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from __future__ import division
"""Basic functions concerning the DAP.
These functions are mostly related to encoding data according to the DAP.
"""
from urllib import quote as _quote
__author__ = 'Roberto De Almeida <rob@pydap.org>'
__version__ = (2,2,6,2) # module version
__dap__ = (2,0) # protocol version
# Constants that used to live in __init__.py but had to be moved
# because we share the namespace with plugins and responses.
USER_AGENT = 'pydap/%s' % '.'.join([str(_) for _ in __version__])
INDENT = ' ' * 4
VERBOSE = False
CACHE = None
TIMEOUT = None
def isiterable(o):
"""Tests if an object is iterable.
>>> print isiterable(range(10))
True
>>> print isiterable({})
True
>>> def a():
... for i in range(10): yield i
>>> print isiterable(a())
True
>>> print isiterable('string')
False
>>> print isiterable(1)
False
"""
# We DON'T want to iterate over strings.
if isinstance(o, basestring): return False
try:
iter(o)
return True
except TypeError:
return False
def to_list(L):
if hasattr(L, 'tolist'): return L.tolist() # shortcut for numpy arrays
elif isiterable(L): return [to_list(item) for item in L]
else: return L
def quote(name):
"""Extended quote for the DAP spec.
The period MUST be escaped in names (DAP spec, item 5.1):
>>> quote("White space")
'White%20space'
>>> _quote("Period.")
'Period.'
>>> quote("Period.")
'Period%2E'
"""
return _quote(name).replace('.', '%2E')
def encode_atom(atom):
r"""Atomic types encoding.
Encoding atomic types for the DAS. Integers should be printed using the
base 10 ASCII representation of its value:
>>> encode_atom(42)
'42'
Floating point values are printed using the base 10 ASCII
representation of its value, conforming to ANSI C's description of
printf using the %g format and precision 6.
>>> encode_atom(1/3)
'0.333333'
String and URLs are printed in ASCII, escaped according to escape().
>>> encode_atom('This is a "string".')
'"This is a \\"string\\"."'
"""
return {basestring: lambda s: escape(s),
unicode : lambda s: escape(s),
str : lambda s: escape(s),
float : lambda f: '%.6g' % f,
long : lambda f: '%.6g' % f,
int : lambda i: repr(i),
}.get(type(atom), lambda obj: '%s' % obj)(atom)
def escape(s):
r"""Escape a string.
Enclose strings with double quotes, escape double quotes and backslashes:
>>> escape('String')
'"String"'
>>> escape('This is a "test".')
'"This is a \\"test\\"."'
"""
s = s.replace(r'\\', r'\\\\')
s = s.replace(r'"', r'\"')
s = '"%s"' % s
return s
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.