ATLAS Offline Software
Tools
PyUtils
python
fprint.py
Go to the documentation of this file.
1
# Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
2
35
36
37
from
__future__
import
print_function
38
import
string
39
40
41
def
_formatFloat
(x):
42
"""Format a value using py2 float formatting.
43
44
>>> print (_formatFloat ('asd'))
45
asd
46
>>> print (_formatFloat (1.234567801234567))
47
1.23456780123
48
>>> print (_formatFloat (2.0))
49
2.0
50
>>> print (_formatFloat (2e30))
51
2e+30
52
>>> print (_formatFloat (float('nan')))
53
nan
54
>>> print (_formatFloat (float('inf')))
55
inf
56
"""
57
# No change if it's not a float.
58
if
not
isinstance (x, float):
59
return
x
60
# default formatting for floats in py2 was effectively the g format
61
# with 12 significant figures --- except that the g format removes
62
# a trailing `.0' while py2 preserves it.
63
s =
'%.12g'
% x
64
if
s.find(
'.'
) < 0
and
s.find(
'e'
) < 0
and
s[-1]
in
string.digits:
65
return
s +
'.0'
66
return
s
67
68
69
def
_formatFloats
(l):
70
"""Convert an argument list to use py2 formatting.
71
>>> print (_formatFloats ((1, 'asd', 3.0)))
72
(1, 'asd', '3.0')
73
"""
74
return
tuple ([
_formatFloat
(x)
for
x
in
l])
75
76
77
def
fprint
(f, *args):
78
"""Print a string with no terminating newline.
79
80
Compatible with the python 2 print statement.
81
82
>>> import sys
83
>>> fprint (sys.stdout, 'a'); fprint (sys.stdout, 'b')
84
a b
85
>>> fwrite (sys.stdout, '\\n')
86
<BLANKLINE>
87
>>> fprint (sys.stdout, 'a'); fprint (sys.stdout, 2.0, '\\n'); fprint (sys.stdout, 'b')
88
a 2.0
89
b
90
"""
91
if
getattr (f,
'softspace'
, 0):
92
f.write (
' '
)
93
print
(*_formatFloats (args), file=f, end=
''
)
94
if
len(args) > 0
and
isinstance (args[-1], str)
and
args[-1].endswith(
'\n'
):
95
f.softspace = 0
96
else
:
97
f.softspace = 1
98
return
99
100
101
def
fprintln
(f, *args):
102
"""Print a string with a terminating newline.
103
104
Compatible with the python 2 print statement.
105
106
>>> import sys
107
>>> sys.stdout.softspace = 0
108
>>> fprint (sys.stdout, 'a'); fprintln (sys.stdout, 'b'); fprintln (sys.stdout, 'c'); fprint (sys.stdout, 'd')
109
a b
110
c
111
d
112
"""
113
if
getattr (f,
'softspace'
, 0):
114
f.write (
' '
)
115
print
(*
_formatFloats
(args), file=f)
116
f.softspace = 0
117
return
118
119
120
def
fwrite
(f, *args):
121
"""Write a string to a file.
122
123
Compatible with the python 2 print statement space handling.
124
>>> import sys
125
>>> sys.stdout.softspace = 0
126
>>> fprint (sys.stdout, 'a'); fwrite (sys.stdout, '\\n'); fprint (sys.stdout, 'c')
127
a
128
c
129
"""
130
f.write (*args)
131
f.softspace = 0
132
return
133
134
135
if
__name__ ==
"__main__"
:
136
print
(
'PyUtils/fprint.py'
)
#pragma: NO COVER
137
from
PyUtils
import
coverage
#pragma: NO COVER
138
c =
coverage.Coverage
(
'PyUtils.fprint'
)
#pragma: NO COVER
139
c.doctest_cover()
#pragma: NO COVER
python.coverage.Coverage
Definition:
coverage.py:149
python.fprint._formatFloats
def _formatFloats(l)
Definition:
fprint.py:69
python.fprint.fprintln
def fprintln(f, *args)
Definition:
fprint.py:101
python.fprint._formatFloat
def _formatFloat(x)
Definition:
fprint.py:41
python.fprint.fwrite
def fwrite(f, *args)
Definition:
fprint.py:120
python.fprint.fprint
def fprint(f, *args)
Definition:
fprint.py:77
Generated on Thu Nov 7 2024 21:15:30 for ATLAS Offline Software by
1.8.18