Skip to content

Commit

Permalink
Added output to examples (#1021)
Browse files Browse the repository at this point in the history
* Added output to examples

* Adjusted example to generate the same output each time

* Static analyze fixes
  • Loading branch information
norberttech authored Mar 23, 2024
1 parent 6a28fd8 commit 3c45ad3
Show file tree
Hide file tree
Showing 91 changed files with 984 additions and 274 deletions.
13 changes: 3 additions & 10 deletions examples/topics/aggregations/average/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{average, df, from_rows, int_entry, ref, row, rows, to_output};
use function Flow\ETL\DSL\{average, data_frame, from_rows, int_entry, ref, row, rows, to_stream};

require __DIR__ . '/../../../autoload.php';

df()
data_frame()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
Expand All @@ -15,12 +15,5 @@
row(int_entry('a', 400))
)))
->aggregate(average(ref('a')))
->write(to_output(false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

// +-------+
// | a_avg |
// +-------+
// | 240 |
// +-------+
// 1 rows
6 changes: 6 additions & 0 deletions examples/topics/aggregations/average/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+-------+
| a_avg |
+-------+
| 240 |
+-------+
1 rows
13 changes: 3 additions & 10 deletions examples/topics/aggregations/first/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{df, first, from_rows, int_entry, ref, row, rows, to_output};
use function Flow\ETL\DSL\{data_frame, first, from_rows, int_entry, ref, row, rows, to_stream};

require __DIR__ . '/../../../autoload.php';

df()
data_frame()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
Expand All @@ -15,12 +15,5 @@
row(int_entry('a', 400))
)))
->aggregate(first(ref('a')))
->write(to_output(false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

// +-----+
// | a |
// +-----+
// | 100 |
// +-----+
// 1 rows
6 changes: 6 additions & 0 deletions examples/topics/aggregations/first/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+-----+
| a |
+-----+
| 100 |
+-----+
1 rows
12 changes: 2 additions & 10 deletions examples/topics/aggregations/group_by/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_array, ref, to_output};
use function Flow\ETL\DSL\{data_frame, from_array, ref, to_stream};

require __DIR__ . '/../../../autoload.php';

Expand All @@ -20,13 +20,5 @@
['id' => 10, 'group' => 'B'],
]))
->groupBy(ref('group'))
->write(to_output(truncate: false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

// +-------+
// | group |
// +-------+
// | A |
// | B |
// +-------+
// 2 rows
7 changes: 7 additions & 0 deletions examples/topics/aggregations/group_by/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
+-------+
| group |
+-------+
| A |
| B |
+-------+
2 rows
12 changes: 2 additions & 10 deletions examples/topics/aggregations/group_by_sum/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_array, ref, sum, to_output};
use function Flow\ETL\DSL\{data_frame, from_array, ref, sum, to_stream};

require __DIR__ . '/../../../autoload.php';

Expand All @@ -21,13 +21,5 @@
]))
->groupBy(ref('group'))
->aggregate(sum(ref('value')))
->write(to_output(truncate: false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

// +-------+-----------+
// | group | value_sum |
// +-------+-----------+
// | A | 1800 |
// | B | 460 |
// +-------+-----------+
// 2 rows
7 changes: 7 additions & 0 deletions examples/topics/aggregations/group_by_sum/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
+-------+-----------+
| group | value_sum |
+-------+-----------+
| A | 1800 |
| B | 460 |
+-------+-----------+
2 rows
13 changes: 3 additions & 10 deletions examples/topics/aggregations/last/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{df, from_rows, int_entry, last, ref, row, rows, to_output};
use function Flow\ETL\DSL\{data_frame, from_rows, int_entry, last, ref, row, rows, to_stream};

require __DIR__ . '/../../../autoload.php';

df()
data_frame()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
Expand All @@ -15,12 +15,5 @@
row(int_entry('a', 400))
)))
->aggregate(last(ref('a')))
->write(to_output(false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

// +-----+
// | a |
// +-----+
// | 400 |
// +-----+
// 1 rows
6 changes: 6 additions & 0 deletions examples/topics/aggregations/last/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+-----+
| a |
+-----+
| 400 |
+-----+
1 rows
13 changes: 3 additions & 10 deletions examples/topics/aggregations/max/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{df, from_rows, int_entry, max, ref, row, rows, to_output};
use function Flow\ETL\DSL\{data_frame, from_rows, int_entry, max, ref, row, rows, to_stream};

require __DIR__ . '/../../../autoload.php';

df()
data_frame()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
Expand All @@ -15,12 +15,5 @@
row(int_entry('a', 400))
)))
->aggregate(max(ref('a')))
->write(to_output(false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

// +-------+
// | a_max |
// +-------+
// | 400 |
// +-------+
// 1 rows
6 changes: 6 additions & 0 deletions examples/topics/aggregations/max/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+-------+
| a_max |
+-------+
| 400 |
+-------+
1 rows
13 changes: 3 additions & 10 deletions examples/topics/aggregations/min/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{df, from_rows, int_entry, min, ref, row, rows, to_output};
use function Flow\ETL\DSL\{data_frame, from_rows, int_entry, min, ref, row, rows, to_stream};

require __DIR__ . '/../../../autoload.php';

df()
data_frame()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
Expand All @@ -15,12 +15,5 @@
row(int_entry('a', 400))
)))
->aggregate(min(ref('a')))
->write(to_output(false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

// +-------+
// | a_min |
// +-------+
// | 100 |
// +-------+
// 1 rows
6 changes: 6 additions & 0 deletions examples/topics/aggregations/min/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+-------+
| a_min |
+-------+
| 100 |
+-------+
1 rows
6 changes: 3 additions & 3 deletions examples/topics/aggregations/sum/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{df, from_rows, int_entry, ref, row, rows, sum, to_output};
use function Flow\ETL\DSL\{data_frame, from_rows, int_entry, ref, row, rows, sum, to_stream};

require __DIR__ . '/../../../autoload.php';

df()
data_frame()
->from(from_rows(rows(
row(int_entry('a', 100)),
row(int_entry('a', 100)),
Expand All @@ -15,5 +15,5 @@
row(int_entry('a', 400))
)))
->aggregate(sum(ref('a')))
->write(to_output(false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();
6 changes: 6 additions & 0 deletions examples/topics/aggregations/sum/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+-------+
| a_sum |
+-------+
| 1200 |
+-------+
1 rows
6 changes: 3 additions & 3 deletions examples/topics/data_frame/cache/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{config_builder, df, from_cache, ref, to_output};
use function Flow\ETL\DSL\{config_builder, data_frame, from_cache, ref, to_stream};
use Flow\ETL\Adapter\Http\DynamicExtractor\NextRequestFactory;
use Flow\ETL\Adapter\Http\PsrHttpClientDynamicExtractor;
use Flow\ETL\Cache\PSRSimpleCache;
Expand Down Expand Up @@ -41,7 +41,7 @@ public function create(?ResponseInterface $previousResponse = null) : ?RequestIn
)
);

df(config_builder()->cache($adapter))
data_frame(config_builder()->cache($adapter))
->read(
from_cache(
id: 'github_api',
Expand All @@ -55,5 +55,5 @@ public function create(?ResponseInterface $previousResponse = null) : ?RequestIn
->renameAll('unpacked.', '')
->drop('unpacked')
->select('name', 'html_url', 'blog', 'login', 'public_repos', 'followers', 'created_at')
->write(to_output(false))
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();
6 changes: 6 additions & 0 deletions examples/topics/data_frame/cache/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+----------+----------+---------------------+--------------+-----------+-----------------------------+----------------------+
| login | name | blog | public_repos | followers | html_url | created_at |
+----------+----------+---------------------+--------------+-----------+-----------------------------+----------------------+
| flow-php | Flow PHP | http://flow-php.com | 30 | 83 | https://github.com/flow-php | 2020-10-26T18:40:27Z |
+----------+----------+---------------------+--------------+-----------+-----------------------------+----------------------+
1 rows
10 changes: 4 additions & 6 deletions examples/topics/data_frame/data_frame/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{array_entry, array_expand, df, from_rows, int_entry, ref, row, rows, to_output};
use function Flow\ETL\DSL\{array_entry, array_expand, data_frame, from_rows, int_entry, ref, row, rows, to_output, to_stream};

// flow.phar run examples/topics/phar/data_frame/code.php
// when executing data processing pipeline through phar make sure to not use any trigger, like ->run();
// this is handled by the phar internally.
return df()
data_frame()
->read(from_rows(rows(
row(int_entry('id', 1), array_entry('array', ['a' => 1, 'b' => 2, 'c' => 3])),
)))
->write(to_output(false))
->withEntry('expanded', array_expand(ref('array')))
->write(to_output(false));
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();
8 changes: 8 additions & 0 deletions examples/topics/data_frame/data_frame/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+----+---------------------+----------+
| id | array | expanded |
+----+---------------------+----------+
| 1 | {"a":1,"b":2,"c":3} | 1 |
| 1 | {"a":1,"b":2,"c":3} | 2 |
| 1 | {"a":1,"b":2,"c":3} | 3 |
+----+---------------------+----------+
3 rows
17 changes: 7 additions & 10 deletions examples/topics/data_frame/overwrite/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
declare(strict_types=1);

use function Flow\ETL\Adapter\CSV\{from_csv, to_csv};
use function Flow\ETL\DSL\{df, overwrite};
use function Flow\ETL\DSL\{data_frame, overwrite, to_stream};

require __DIR__ . '/../../../autoload.php';

df()
data_frame()
->read(from_csv(__DIR__ . '/input/file.csv'))
->saveMode(overwrite())
->write(to_csv(__DIR__ . '/output/file.csv'))
->collect()
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

df()
data_frame()
->read(from_csv(__DIR__ . '/output/file.csv'))
->saveMode(overwrite())
->drop('name')
->write(to_csv(__DIR__ . '/output/file.csv'))
->collect()
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();

// content of /output/file.csv:
// id
// 1
// 2
// 3
// 4
9 changes: 9 additions & 0 deletions examples/topics/data_frame/overwrite/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
+----+
4 rows
5 changes: 3 additions & 2 deletions examples/topics/data_source/array/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_array, to_output};
use function Flow\ETL\DSL\{data_frame, from_array, to_stream};

require __DIR__ . '/../../../autoload.php';

Expand All @@ -14,5 +14,6 @@
['id' => 4],
['id' => 5],
]))
->write(to_output(false))
->collect()
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();
10 changes: 10 additions & 0 deletions examples/topics/data_source/array/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+----+
5 rows
Loading

0 comments on commit 3c45ad3

Please sign in to comment.