{"id":14966113,"url":"https://github.com/eatingtomatoes/inline-python3","last_synced_at":"2026-01-20T22:36:26.193Z","repository":{"id":90603967,"uuid":"167991994","full_name":"eatingtomatoes/Inline-Python3","owner":"eatingtomatoes","description":"Use Python3 Code and Libraries in Perl6 World","archived":false,"fork":false,"pushed_at":"2019-05-01T09:23:02.000Z","size":106,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-15T09:35:23.741Z","etag":null,"topics":["inline","perl6","python3","tool"],"latest_commit_sha":null,"homepage":"","language":"Perl 6","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"artistic-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eatingtomatoes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-28T16:11:09.000Z","updated_at":"2019-05-01T09:23:04.000Z","dependencies_parsed_at":"2023-03-23T23:32:53.865Z","dependency_job_id":null,"html_url":"https://github.com/eatingtomatoes/Inline-Python3","commit_stats":{"total_commits":53,"total_committers":2,"mean_commits":26.5,"dds":0.07547169811320753,"last_synced_commit":"35d44938fb6915eb2b461583609def8a0b6ebcb4"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eatingtomatoes%2FInline-Python3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eatingtomatoes%2FInline-Python3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eatingtomatoes%2FInline-Python3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eatingtomatoes%2FInline-Python3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eatingtomatoes","download_url":"https://codeload.github.com/eatingtomatoes/Inline-Python3/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248060712,"owners_count":21041214,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["inline","perl6","python3","tool"],"created_at":"2024-09-24T13:35:50.558Z","updated_at":"2026-01-20T22:36:26.153Z","avatar_url":"https://github.com/eatingtomatoes.png","language":"Perl 6","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Description\n\nModule for executing Python3 code and accessing Python libraries from Perl 6.\n\nNote that this library is largely based on the Inline::Python.\n\n# Usage\n\nFirst, import necessary modules and get a reference to the \\__main__ module.\n```\nuse Inline::Python3;\nuse Inline::Python3::PyModule;\n\n# initialize the environment\nstart-python;\n\nmy $main = PyModule('__main__');\n```\nLet's start from some simple code:\n\n```\nsay $main.run('5', :eval);\nsay $main.run(\"Python\"', :eval); \n```\n\nThe keyword \"eval\" asks the python interpreter to calculate a simple expression and return its value.\n\nYou can also use the keyword \"file\", which means several statements.  No keyword is OK, which defaults to the \"file\".\n\nThen, let's define a simple function in python world:\n\n```\n$main.run(q:to/PYTHON/);\ndef plus_int(a, b):\n\treturn a + b\nPYTHON\n```\nThen you can call it in perl6 code like this:\n```\n$main.plus_int(2, 1);\n```\n\nNote that you cannot directly call a function/method whose name conflicts with that of any method of the perl6 class \"Any\". For example, if you define a function/method with the name \"sum\", then you cannot do this: $main.sum(x, y). The perl6 compiler will treat it as a method call of the class \"Any\".\n\n There exists  a way to bypass the problem:\n\n```\nsay $main\u003csum\u003e(1, 2);\n```\n\nNote that \"$main.sum(...)\" is invoking a function defined in $main, while \"$main\\\u003csum\u003e\" is accessing a $main's attribute, which is a callable object. \n\nHere follows a bit more complex example:\n\n```\n$main.run(q:to/PYTHON/);\nclass Foo:\n    def __init__(self, value):\n        self.value = value\n  \n    def some_method(self, a, b):\n        return a + b\n        \n\t@staticmethod\n    def some_static_method():\n    \treturn 666\n\t\ndef dump_foo(foo):\n    return foo.value\nPYTHON\n```\n\nYou can instantiate the python class like this:\n\n```\nmy $foo = $main.Foo(1);\n```\n\nAccessing its attribute or method is quite easy:\n\n```\nsay $foo.value \n\nsay $main.Foo(1).value;\n\nsay $main.Foo(1).some_method(3, 1);\n\nsay $main\u003cFoo\u003e.some_static_method;\n\nsay $main.dump_foo($main.Foo(6))\n```\n\nSo far,  all functions and class are defined in the \\__main__ module. But you can  use other modules too.\n\n```\nmy $string = PyModule('string');\n```\n\nThen you can use the string module happily:\n\n```\nsay $string.capwords('foo bar')\n```\n\nYou may also use python's operators:\n\n```\nuse Inline::Python3::PyOperators;\nmy $pyobject = ... ;\nmy $pylist = ... ;\nsay $pyobject ?in $pylist;\n```\n\nBasically, unary python operators start with `??` while binary ones start with `?`. There are a few of exceptions. See the comments in PyOperator.pm6.\n\nSee more usage in t/*.t files.\n\n# A Complete Example\n\nFollowing is a runnable example, which use keras to train a cnn on the mnist dataset.\n\nThe code is based on the [example](https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py) from keras document.\n\n```\n#! /usr/bin/env perl6\n\nuse Inline::Python3;\nuse Inline::Python3::PyModule;\n\nstart-python;\n\nsub np { PyModule('numpy') }\nsub keras { PyModule('keras') }\n\nmy $batch-size = 128;\nmy $num-classes = 10;\nmy $epochs = 12;\n\nmy @image-dim = do {\n    keras.backend.image_data_format eq 'channels_first' ??\n    (1, 28, 28) !! (28, 28, 1)\n}\n\nmy ($train, $test) = keras.datasets.mnist.load_data.list.map: -\u003e $/ {\n    np.true_divide($0.reshape($0.shape[0], |@image-dim).astype('float32'), 255),\n    keras.utils.to_categorical($1, $num-classes)\n}\n\nmy $inputs = keras.layers.Input(shape =\u003e @image-dim);\nmy $outputs = do given PyModule('keras.layers') {\n    $inputs\n    ==\u003e .Conv2D(32, (3, 3), :activation\u003crelu\u003e)()\n    ==\u003e .Conv2D(64, (3, 3), :activation\u003crelu\u003e)()\n    ==\u003e .MaxPooling2D(pool_size =\u003e (2,2))()\n    ==\u003e .Dropout(0.25)()\n    ==\u003e .Flatten()()\n    ==\u003e .Dense(128, :activation\u003crelu\u003e)()\n    ==\u003e .Dropout(0.5)()\n    ==\u003e .Dense($num-classes, :activation\u003csoftmax\u003e)()\n}\n\ngiven keras.models.Model(:$inputs, :$outputs) {\n    .compile(\n\tloss =\u003e keras.losses\u003ccategorical_crossentropy\u003e,\n\toptimizer =\u003e keras.optimizers.Adadelta,\n\tmetrics =\u003e ['accuracy']\n    );\n\n    .fit(\n\t|$train, batch_size =\u003e $batch-size,\n\tepochs =\u003e $epochs, verbose =\u003e 1,\n\tvalidation_data =\u003e $test\n    );\n\n    .evaluate(|$test, verbose =\u003e 0);\n}\n```\n\n# Build\n\nYou will need a Python built with the -fPIC option (position independent code). Most distributions build their Python that way. \n\nYou can choose which python to be used by settinng the environment variable PYTHON_CONFIG to the path to python3.*-config.  If you don't specify one, the builder will use the first result of shell command 'locate python3-config'.\n\n\nWith a python in your path, then build:\n\n```\nperl6 configure.pl6\nmake test\n# Sorry, I don't know how to implement the \"make install\" :(\nmake install\n```\n\nOr you can use the zef:\n\n```\nzef install .\n```\n\nNote that at present this library cannot be built with python interpreter provided by anaconda.\n\n# Reference\n\n- [Inline::Python](https://github.com/niner/Inline-Python)\n\n- [Perl6 Documents](https://docs.perl6.org)\n\n- [Perl6 Specification](https://design.perl6.org/)\n\n- [Extending and Embedding the Python Interpreter](https://docs.python.org/3/extending/index.html)\n\n- [Python/C API Reference Manual](https://docs.python.org/3/c-api/index.html)\n\n- [Porting Python2 Extension Modules to Python 3](https://docs.python.org/3.6/howto/cporting.html?highlight=pymodinit_func)\n\n\n\n# Contact\n\nschwaa@outlook.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Featingtomatoes%2Finline-python3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Featingtomatoes%2Finline-python3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Featingtomatoes%2Finline-python3/lists"}