ATLAS Offline Software
fprint.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
35 
36 
37 import string
38 
39 
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 
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 
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 
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 
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 
134 if __name__ == "__main__":
135  print ('PyUtils/fprint.py') #pragma: NO COVER
136  from PyUtils import coverage #pragma: NO COVER
137  c = coverage.Coverage ('PyUtils.fprint') #pragma: NO COVER
138  c.doctest_cover() #pragma: NO COVER
python.coverage.Coverage
Definition: coverage.py:144
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