You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Ignore Manifest.toml files
* Add import statement in install instructions
* Use single backtick for monospace
Double backtick names get passed on to the
(La/Ka)TeX renderer
* Combine markdown lists
Repeated newlines are interpreted as separate lists
* Use python 3 for reference
* Make package name a header
Copy file name to clipboardexpand all lines: README.md
+35-36
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ This package offers Python-style general formatting and c-style numerical format
37
37
This package is pure Julia. Setting up this package is like setting up other Julia packages:
38
38
39
39
```julia
40
-
Pkg.add("Format")
40
+
import Pkg; Pkg.add("Format")
41
41
```
42
42
or
43
43
```julia
@@ -59,92 +59,91 @@ This package depends on Julia of version 1.4 or above, and. The package is MIT-l
59
59
60
60
#### Types to Represent Formats
61
61
62
-
This package has two types ``FormatSpec`` and ``FormatExpr`` to represent a format specification.
62
+
This package has two types `FormatSpec` and `FormatExpr` to represent a format specification.
63
63
64
-
In particular, ``FormatSpec`` is used to capture the specification of a single entry. One can compile a format specification string into a ``FormatSpec`` instance as
64
+
In particular, `FormatSpec` is used to capture the specification of a single entry. One can compile a format specification string into a `FormatSpec` instance as
65
65
66
66
```julia
67
67
fspec =FormatSpec("d")
68
68
fspec =FormatSpec("<8.4f")
69
69
```
70
-
Please refer to [Python's format specification language](http://docs.python.org/2/library/string.html#formatspec) for details.
70
+
Please refer to [Python's format specification language](http://docs.python.org/3/library/string.html#formatspec) for details.
71
71
72
72
73
-
``FormatExpr`` captures a formatting expression that may involve multiple items. One can compile a formatting string into a ``FormatExpr`` instance as
73
+
`FormatExpr` captures a formatting expression that may involve multiple items. One can compile a formatting string into a `FormatExpr` instance as
74
74
75
75
```julia
76
76
fe =FormatExpr("{1} + {2}")
77
77
fe =FormatExpr("{1:d} + {2:08.4e} + {3|>abs2}")
78
78
```
79
-
Please refer to [Python's format string syntax](http://docs.python.org/2/library/string.html#format-string-syntax) for details.
79
+
Please refer to [Python's format string syntax](http://docs.python.org/3/library/string.html#format-string-syntax) for details.
80
80
81
81
82
82
**Note:** If the same format is going to be applied for multiple times. It is more efficient to first compile it.
83
83
84
84
85
85
#### Formatted Printing
86
86
87
-
One can use ``printfmt`` and ``printfmtln`` for formatted printing:
87
+
One can use `printfmt` and `printfmtln` for formatted printing:
88
88
89
89
-**printfmt**(io, fe, args...)
90
90
91
91
-**printfmt**(fe, args...)
92
92
93
-
Print given arguments using given format ``fe``. Here ``fe`` can be a formatting string, an instance of ``FormatSpec`` or ``FormatExpr``.
Print given arguments using given format `fe`. Here `fe` can be a formatting string, an instance of `FormatSpec` or `FormatExpr`.
107
94
108
-
**Notes**
95
+
**Examples**
109
96
110
-
If the first argument is a string, it will be first compiled into a ``FormatExpr``, which implies that you can not use specification-only string in the first argument.
printfmt("{1:d}", 10) # OK, "{1:d}" can be compiled into a FormatExpr instance
114
-
printfmt("d", 10) # Error, "d" can not be compiled into a FormatExpr instance
115
-
# such a string to specify a format specification for single argument
101
+
fs =FormatSpec("#04x")
102
+
printfmt(fs, 12) # --> print("0x0c")
116
103
117
-
printfmt(FormatSpec("d"), 10) # OK
118
-
printfmt(FormatExpr("{1:d}", 10)) # OK
119
-
```
104
+
fe =FormatExpr("{} = {:#04x}")
105
+
printfmt(fe, "abc", 12) # --> print("abc = 0x0c")
106
+
```
120
107
108
+
**Notes**
109
+
110
+
If the first argument is a string, it will be first compiled into a `FormatExpr`, which implies that you can not use specification-only string in the first argument.
111
+
112
+
```julia
113
+
printfmt("{1:d}", 10) # OK, "{1:d}" can be compiled into a FormatExpr instance
114
+
printfmt("d", 10) # Error, "d" can not be compiled into a FormatExpr instance
115
+
# such a string to specify a format specification for single argument
116
+
117
+
printfmt(FormatSpec("d"), 10) # OK
118
+
printfmt(FormatExpr("{1:d}", 10)) # OK
119
+
```
121
120
122
121
-**printfmtln**(io, fe, args...)
123
122
124
123
-**printfmtln**(fe, args...)
125
124
126
-
Similar to ``printfmt`` except that this function print a newline at the end.
125
+
Similar to `printfmt` except that this function print a newline at the end.
127
126
128
127
#### Formatted String
129
128
130
-
One can use ``pyfmt`` to format a single value into a string, or ``format`` to format one to multiple arguments into a string using an format expression.
129
+
One can use `pyfmt` to format a single value into a string, or `format` to format one to multiple arguments into a string using an format expression.
131
130
132
131
-**pyfmt**(fspec, a)
133
132
134
-
Format a single value using a format specification given by ``fspec``, where``fspec`` can be either a string or an instance of ``FormatSpec``.
133
+
Format a single value using a format specification given by `fspec`, where `fspec` can be either a string or an instance of `FormatSpec`.
135
134
136
135
-**format**(fe, args...)
137
136
138
-
Format arguments using a format expression given by ``fe``, where``fe`` can be either a string or an instance of ``FormatSpec``.
137
+
Format arguments using a format expression given by `fe`, where `fe` can be either a string or an instance of `FormatSpec`.
139
138
140
139
141
140
#### Difference from Python's Format
142
141
143
142
At this point, this package implements a subset of Python's formatting language (with slight modification). Here is a summary of the differences:
144
143
145
-
- In terms of argument specification, it supports natural ordering (e.g. ``{} + {}``), explicit position (e.g. ``{1} + {2}``). It hasn't supported named arguments or fields extraction yet. Note that mixing these two modes is not allowed (e.g. ``{1} + {}``).
144
+
- In terms of argument specification, it supports natural ordering (e.g. `{} + {}`), explicit position (e.g. `{1} + {2}`). It hasn't supported named arguments or fields extraction yet. Note that mixing these two modes is not allowed (e.g. `{1} + {}`).
146
145
147
-
- The package provides support for filtering (for explicitly positioned arguments), such as ``{1|>lowercase}`` by allowing one to embed the ``|>`` operator, which the Python counter part does not support.
146
+
- The package provides support for filtering (for explicitly positioned arguments), such as `{1|>lowercase}` by allowing one to embed the `|>` operator, which the Python counter part does not support.
Copy file name to clipboardexpand all lines: src/Format.jl
+50-36
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
"""
2
-
Format.jl
2
+
# Format.jl
3
3
4
4
This package provides various functions to provide formatted output,
5
5
either in a fashion similar to C printf or Python format strings.
@@ -8,18 +8,22 @@ either in a fashion similar to C printf or Python format strings.
8
8
9
9
#### Types to Represent Formats
10
10
11
-
This package has two types ``FormatSpec`` and ``FormatExpr`` to represent a format specification.
11
+
This package has two types `FormatSpec` and `FormatExpr` to represent a
12
+
format specification.
12
13
13
-
In particular, ``FormatSpec`` is used to capture the specification of a single entry. One can compile a format specification string into a ``FormatSpec`` instance as
14
+
In particular, `FormatSpec` is used to capture the specification of a single
15
+
entry. One can compile a format specification string into a `FormatSpec`
16
+
instance as
14
17
15
18
```julia
16
19
fspec = FormatSpec("d")
17
20
fspec = FormatSpec("<8.4f")
18
21
```
19
-
Please refer to [Python's format specification language](http://docs.python.org/2/library/string.html#formatspec) for details.
22
+
Please refer to [Python's format specification language](http://docs.python.org/3/library/string.html#formatspec) for details.
20
23
21
24
22
-
``FormatExpr`` captures a formatting expression that may involve multiple items. One can compile a formatting string into a ``FormatExpr`` instance as
25
+
`FormatExpr` captures a formatting expression that may involve multiple items.
26
+
One can compile a formatting string into a `FormatExpr` instance as
23
27
24
28
```julia
25
29
fe = FormatExpr("{1} + {2}")
@@ -33,67 +37,77 @@ Please refer to [Python's format string syntax](http://docs.python.org/2/library
33
37
34
38
#### Formatted Printing
35
39
36
-
One can use ``printfmt`` and ``printfmtln`` for formatted printing:
40
+
One can use `printfmt` and `printfmtln` for formatted printing:
37
41
38
42
- **printfmt**(io, fe, args...)
39
43
40
44
- **printfmt**(fe, args...)
41
45
42
-
Print given arguments using given format ``fe``. Here ``fe`` can be a formatting string, an instance of ``FormatSpec`` or ``FormatExpr``.
Print given arguments using given format `fe`. Here `fe` can be a formatting
47
+
string, an instance of `FormatSpec` or `FormatExpr`.
56
48
57
-
**Notes**
49
+
**Examples**
58
50
59
-
If the first argument is a string, it will be first compiled into a ``FormatExpr``, which implies that you can not use specification-only string in the first argument.
printfmt("{1:d}", 10) # OK, "{1:d}" can be compiled into a FormatExpr instance
63
-
printfmt("d", 10) # Error, "d" can not be compiled into a FormatExpr instance
64
-
# such a string to specify a format specification for single argument
55
+
fs = FormatSpec("#04x")
56
+
printfmt(fs, 12) # --> print("0x0c")
65
57
66
-
printfmt(FormatSpec("d"), 10) # OK
67
-
printfmt(FormatExpr("{1:d}", 10)) # OK
68
-
```
58
+
fe = FormatExpr("{} = {:#04x}")
59
+
printfmt(fe, "abc", 12) # --> print("abc = 0x0c")
60
+
```
69
61
62
+
**Notes**
63
+
64
+
If the first argument is a string, it will be first compiled into a `FormatExpr`,
65
+
which implies that you can not use specification-only string in the first argument.
66
+
67
+
```julia
68
+
printfmt("{1:d}", 10) # OK, "{1:d}" can be compiled into a FormatExpr instance
69
+
printfmt("d", 10) # Error, "d" can not be compiled into a FormatExpr instance
70
+
# such a string to specify a format specification for single argument
71
+
72
+
printfmt(FormatSpec("d"), 10) # OK
73
+
printfmt(FormatExpr("{1:d}", 10)) # OK
74
+
```
70
75
71
76
- **printfmtln**(io, fe, args...)
72
77
73
78
- **printfmtln**(fe, args...)
74
79
75
-
Similar to ``printfmt`` except that this function print a newline at the end.
80
+
Similar to `printfmt` except that this function print a newline at the end.
76
81
77
82
#### Formatted String
78
83
79
-
One can use ``pyfmt`` to format a single value into a string, or ``format`` to format one to multiple arguments into a string using an format expression.
84
+
One can use `pyfmt` to format a single value into a string, or `format` to
85
+
format one to multiple arguments into a string using an format expression.
80
86
81
87
- **pyfmt**(fspec, a)
82
88
83
-
Format a single value using a format specification given by ``fspec``, where ``fspec`` can be either a string or an instance of ``FormatSpec``.
89
+
Format a single value using a format specification given by `fspec`, where
90
+
`fspec` can be either a string or an instance of `FormatSpec`.
84
91
85
92
- **format**(fe, args...)
86
93
87
-
Format arguments using a format expression given by ``fe``, where ``fe`` can be either a string or an instance of ``FormatSpec``.
94
+
Format arguments using a format expression given by `fe`, where `fe` can be
95
+
either a string or an instance of `FormatSpec`.
88
96
89
97
90
98
#### Difference from Python's Format
91
99
92
-
At this point, this package implements a subset of Python's formatting language (with slight modification). Here is a summary of the differences:
100
+
At this point, this package implements a subset of Python's formatting language
101
+
(with slight modification). Here is a summary of the differences:
93
102
94
-
- In terms of argument specification, it supports natural ordering (e.g. ``{} + {}``), explicit position (e.g. ``{1} + {2}``). It hasn't supported named arguments or fields extraction yet. Note that mixing these two modes is not allowed (e.g. ``{1} + {}``).
103
+
- In terms of argument specification, it supports natural ordering (e.g.
104
+
`{} + {}`), explicit position (e.g. `{1} + {2}`). It hasn't supported named
105
+
arguments or fields extraction yet. Note that mixing these two modes is not
106
+
allowed (e.g. `{1} + {}`).
95
107
96
-
- The package provides support for filtering (for explicitly positioned arguments), such as ``{1|>lowercase}`` by allowing one to embed the ``|>`` operator, which the Python counter part does not support.
108
+
- The package provides support for filtering (for explicitly positioned
109
+
arguments), such as `{1|>lowercase}` by allowing one to embed the `|>`
110
+
operator, which the Python counter part does not support.
0 commit comments