From 8362b78759f4b8289b250e33dd4102186a3bf8ce Mon Sep 17 00:00:00 2001 From: Rameshwar Bhaskaran Date: Sun, 15 Oct 2017 00:49:00 +0530 Subject: [PATCH] Add sample Python code --- hello-buck-python/.buckconfig | 3 +++ hello-buck-python/.gitignore | 12 ++++++++++++ hello-buck-python/BUCK | 12 ++++++++++++ hello-buck-python/README.md | 10 ++++++++++ hello-buck-python/calc.py | 28 ++++++++++++++++++++++++++++ hello-buck-python/hello_buck.py | 9 +++++++++ 6 files changed, 74 insertions(+) create mode 100644 hello-buck-python/.buckconfig create mode 100644 hello-buck-python/.gitignore create mode 100644 hello-buck-python/BUCK create mode 100644 hello-buck-python/README.md create mode 100644 hello-buck-python/calc.py create mode 100644 hello-buck-python/hello_buck.py diff --git a/hello-buck-python/.buckconfig b/hello-buck-python/.buckconfig new file mode 100644 index 0000000..2246f49 --- /dev/null +++ b/hello-buck-python/.buckconfig @@ -0,0 +1,3 @@ +[python] + interpreter = /usr/bin/python2.7 + path_to_pex_executer = /bin/bash \ No newline at end of file diff --git a/hello-buck-python/.gitignore b/hello-buck-python/.gitignore new file mode 100644 index 0000000..15fe75a --- /dev/null +++ b/hello-buck-python/.gitignore @@ -0,0 +1,12 @@ +# Buck +/buck-out +/buck-cache +/local.properties +/.buckconfig.local +/.buckd + +# OS X +.DS_Store + +# Python +/*.pyc diff --git a/hello-buck-python/BUCK b/hello-buck-python/BUCK new file mode 100644 index 0000000..d905fe3 --- /dev/null +++ b/hello-buck-python/BUCK @@ -0,0 +1,12 @@ +python_binary( + name = 'hello_buck_python', + main = 'hello_buck.py', + deps = [ + ':calc', + ], +) + +python_library( + name = 'calc', + srcs = glob(['calc.py']), +) diff --git a/hello-buck-python/README.md b/hello-buck-python/README.md new file mode 100644 index 0000000..fbfe3e4 --- /dev/null +++ b/hello-buck-python/README.md @@ -0,0 +1,10 @@ +# Hello Buck - Python + +A simple demo on how to setup Buck to build and execute Python code + +## Running + +``` +buck build //:hello_buck_python +./buck-out/gen/hello_buck_python.pex +``` \ No newline at end of file diff --git a/hello-buck-python/calc.py b/hello-buck-python/calc.py new file mode 100644 index 0000000..1ea8be4 --- /dev/null +++ b/hello-buck-python/calc.py @@ -0,0 +1,28 @@ +from __future__ import division +from __future__ import print_function + +import logging + +logger = logging.getLogger(__name__) + + +def add(a, b): + """Add two numbers.""" + return a+b + + +def divide(a, b): + """"Divide two numbers. Throw exception if division by zero.""" + if b == 0: + raise ZeroDivisionError() + return a/b + + +def subtract(a, b): + """Subtract two numbers""" + return a-b + + +def multiply(a, b): + """Multiply two numbers""" + return a*b diff --git a/hello-buck-python/hello_buck.py b/hello-buck-python/hello_buck.py new file mode 100644 index 0000000..6caeada --- /dev/null +++ b/hello-buck-python/hello_buck.py @@ -0,0 +1,9 @@ +from calc import add, multiply + + +def main(): + print add(1, 2) + print multiply(3, 4) + +if __name__ == '__main__': + main()