ATLAS Offline Software
Functions | Variables
python.fprint Namespace Reference

Functions

def _formatFloat (x)
 
def _formatFloats (l)
 
def fprint (f, *args)
 
def fprintln (f, *args)
 
def fwrite (f, *args)
 

Variables

 c
 

Function Documentation

◆ _formatFloat()

def python.fprint._formatFloat (   x)
private
Format a value using py2 float formatting.

>>> print (_formatFloat ('asd'))
asd
>>> print (_formatFloat (1.234567801234567))
1.23456780123
>>> print (_formatFloat (2.0))
2.0
>>> print (_formatFloat (2e30))
2e+30
>>> print (_formatFloat (float('nan')))
nan
>>> print (_formatFloat (float('inf')))
inf

Definition at line 40 of file fprint.py.

40 def _formatFloat (x):
41  """Format a value using py2 float formatting.
42 
43 >>> print (_formatFloat ('asd'))
44 asd
45 >>> print (_formatFloat (1.234567801234567))
46 1.23456780123
47 >>> print (_formatFloat (2.0))
48 2.0
49 >>> print (_formatFloat (2e30))
50 2e+30
51 >>> print (_formatFloat (float('nan')))
52 nan
53 >>> print (_formatFloat (float('inf')))
54 inf
55 """
56  # No change if it's not a float.
57  if not isinstance (x, float):
58  return x
59  # default formatting for floats in py2 was effectively the g format
60  # with 12 significant figures --- except that the g format removes
61  # a trailing `.0' while py2 preserves it.
62  s = '%.12g' % x
63  if s.find('.') < 0 and s.find('e') < 0 and s[-1] in string.digits:
64  return s + '.0'
65  return s
66 
67 

◆ _formatFloats()

def python.fprint._formatFloats (   l)
private
Convert an argument list to use py2 formatting.
>>> print (_formatFloats ((1, 'asd', 3.0)))
(1, 'asd', '3.0')

Definition at line 68 of file fprint.py.

68 def _formatFloats (l):
69  """Convert an argument list to use py2 formatting.
70 >>> print (_formatFloats ((1, 'asd', 3.0)))
71 (1, 'asd', '3.0')
72 """
73  return tuple ([_formatFloat(x) for x in l])
74 
75 

◆ fprint()

def python.fprint.fprint (   f,
args 
)
Print a string with no terminating newline.

Compatible with the python 2 print statement.

>>> import sys
>>> fprint (sys.stdout, 'a'); fprint (sys.stdout, 'b')
a b
>>> fwrite (sys.stdout, '\\n')
<BLANKLINE>
>>> fprint (sys.stdout, 'a'); fprint (sys.stdout, 2.0, '\\n'); fprint (sys.stdout, 'b')
a 2.0 
b

Definition at line 76 of file fprint.py.

76 def fprint (f, *args):
77  """Print a string with no terminating newline.
78 
79 Compatible with the python 2 print statement.
80 
81 >>> import sys
82 >>> fprint (sys.stdout, 'a'); fprint (sys.stdout, 'b')
83 a b
84 >>> fwrite (sys.stdout, '\\n')
85 <BLANKLINE>
86 >>> fprint (sys.stdout, 'a'); fprint (sys.stdout, 2.0, '\\n'); fprint (sys.stdout, 'b')
87 a 2.0
88 b
89 """
90  if getattr (f, 'softspace', 0):
91  f.write (' ')
92  print (*_formatFloats (args), file=f, end='')
93  if len(args) > 0 and isinstance (args[-1], str) and args[-1].endswith('\n'):
94  f.softspace = 0
95  else:
96  f.softspace = 1
97  return
98 
99 

◆ fprintln()

def python.fprint.fprintln (   f,
args 
)
Print a string with a terminating newline.

Compatible with the python 2 print statement.

>>> import sys
>>> sys.stdout.softspace = 0
>>> fprint (sys.stdout, 'a'); fprintln (sys.stdout, 'b'); fprintln (sys.stdout, 'c'); fprint (sys.stdout, 'd')
a b
c
d

Definition at line 100 of file fprint.py.

100 def fprintln (f, *args):
101  """Print a string with a terminating newline.
102 
103 Compatible with the python 2 print statement.
104 
105 >>> import sys
106 >>> sys.stdout.softspace = 0
107 >>> fprint (sys.stdout, 'a'); fprintln (sys.stdout, 'b'); fprintln (sys.stdout, 'c'); fprint (sys.stdout, 'd')
108 a b
109 c
110 d
111 """
112  if getattr (f, 'softspace', 0):
113  f.write (' ')
114  print (*_formatFloats(args), file=f)
115  f.softspace = 0
116  return
117 
118 

◆ fwrite()

def python.fprint.fwrite (   f,
args 
)
Write a string to a file.

Compatible with the python 2 print statement space handling.
>>> import sys
>>> sys.stdout.softspace = 0
>>> fprint (sys.stdout, 'a'); fwrite (sys.stdout, '\\n'); fprint (sys.stdout, 'c')
a
c

Definition at line 119 of file fprint.py.

119 def fwrite (f, *args):
120  """Write a string to a file.
121 
122 Compatible with the python 2 print statement space handling.
123 >>> import sys
124 >>> sys.stdout.softspace = 0
125 >>> fprint (sys.stdout, 'a'); fwrite (sys.stdout, '\\n'); fprint (sys.stdout, 'c')
126 a
127 c
128 """
129  f.write (*args)
130  f.softspace = 0
131  return
132 
133 

Variable Documentation

◆ c

python.fprint.c

Definition at line 137 of file fprint.py.

python.fprint._formatFloats
def _formatFloats(l)
Definition: fprint.py:68
python.fprint.fprintln
def fprintln(f, *args)
Definition: fprint.py:100
python.fprint._formatFloat
def _formatFloat(x)
Definition: fprint.py:40
python.fprint.fwrite
def fwrite(f, *args)
Definition: fprint.py:119
python.fprint.fprint
def fprint(f, *args)
Definition: fprint.py:76