 Hi, my name is Marcus. This talk is rapping go in Python. Hi, everybody So first start off with a few introductions. My name is Marcus, which we all established my My favorite frictional character is Batman and Currently my favorite music quote is Even the boxing critics know that if I get off to a rocky start, I'll always have a rocky finish Eminem. Thank you Another thing I want to say about this talk is that it is all on github including the code and the slides So if you guys wanted to follow along as well, you guys could just go to this link I'll leave it up there for a little bit just so you guys can get to it and my social handle is crazecom everywhere. So Twitter email Google plus and Anything else that that exists. All right. I'm moving on So we have the talk overview just to make sure everybody's in the right place I'm going to go over building shared objects and go calling object files from Python Going over the possibilities that exist and then ending with some concluding remarks Sound good All right section one. It can be done We're going to write a go program make a shared object pro shared object file out of that program And then call it from Python. So what is a shared object file? Shared objects you consider that you can't consider them shared libraries because they're a source code that other Programming languages and programs can pull in and use at will shared libraries are that are Libraries that are loaded by programs when they start in Terms of convention these files usually kept in a special place and given special names You can read them more about them at the link For our purposes, we didn't know that when you build a shared object file and go It has an ending of that so just for the most part So let's go build a hello world share file and go can everybody can everybody see this should I make it bigger That big enough a bigger section one So I'm gonna show you the files. These are just like the github repo We're gonna open and look at hello world go So for a GoPro crap program if you guys do not know they have some formal syntax different syntax from Python You start everything off with a package you have your imports after that We have to import C because we're using C to export the go program and make a shared object file So there's the import line Here it looks like a comment and technically through the ghost syntax It is a comment Howard for us to export and be able to use a function We have to label it this way with export and then the name of the function Here's the function hello world It just prints the screen hello world everybody's probably seen a hello world program before so I'm gonna skip over that one So the main magic is being able to build the SO file and to do that we use a go command called build This is the basic syntax of it it starts off with go Build that's oh output pot output file, which is the dot SO the build mode is a C shared Source and then you have the source file afterwards in the repo. I put all the commands in In the command at s8 right there And I'm just gonna run that file which is gonna run this command so we can just build it right now Oh the go command is in here So for people using go I use go version manager to manage my go versions DVM list I have a few different versions one thing that I forgot to mention that is important to note that the Ability to create shared object files exist from go 1.5 and onward and this is in this talk I'm using go 1.6 Let's try this again All right, if everything went smoothly we should have a dot SO file Also, we have a dot H file, which is something that's also created when you build a shared object file so the next part is going into Python and In Python we have to be able to call this that SO file the way we do this by using the C types modules in the standard library now The way is described in the documentation C types is a foreign function Library for Python it provides C compatible data types and allows calling functions in DLLs DLLs or shared libraries it can be used to wrap these libraries in pure Python So we're gonna use that to Call our dot SO file that we just created I'm gonna open the Python file just so we can go through it real quick Everybody's on the same page We have our import C types, which we just talked about In order to import the file we have to know where it's located so we're importing operating system and then creating the path to that file We're then loading it using C types and then the loaded variable has the exported function name of Hello, world, which we exported in the go program and then we're just calling we want to see if this is possible first Thank you, thank you So just a little recap of what we did we use go to create a program We then exported that program to a dot SO file shared object. We then went into Python Dynamically linked that dot SO file called the exported function and then printed something to screen. Hello world just did that Section to the possibilities All right, so an overview of this section Now that I know what now that we know that we can call go at in Python just wrap it I'm curious as to what we can pass back and forth between Python and go and go in Python So I wanted to go through a list of examples of types Experiment let's say because these are all really just experiments so with integers section to integers so the basic format for All of these sections is that I'm gonna have an input file and the output file Well, one just focuses on trying to pass items from Python to go and the other one focuses on trying to pass items from go to Python Let's look at the Python file first. It's pretty similar to the hello world. We have the imports We have the OS find the path load the library The only thing really different in this is that we're taking a user input we're converting it to an integer and then in the lib which is the shared object variable We're calling the exported function and then passing the user input into it, which is now an integer The goal program is pretty simple as well Same as the hello world except for we have a different function name Different export name and we're passing in a variable a number that is an integer and then we're printing it to screen Let's see if this works Anybody have a favorite number integer? 28 came out loud and proud All right, 28 seem to be able to pass so we just took an integer pass it from took it from Python Pass it to go and then print it to screen. So seems as though we can do integers from Python to go. Let's try output Look at the Python file Everything's pretty much the same except for this line We're calling in output passing it into answer and then trying to print answer All right, the go on is a little bit different I wanted to try to print a random integer. So I had to generate the had to start this pseudo random generator What's that's here? What we really care about is this one the exported function In output It sends out an integer and that is a random integer below 10,000 Let's see if it works We have numbers being passed So we just showed here is that we can pass integers from Python to go and go to Python. Okay, what's next? Let's let's look at booleans try booleans next same basic format Input it out for a pile just like explained earlier. Let's look at the Python file so in doing this what I sort of discovered is that when passing booleans back from Python input yes We have this line for user input True if the random integer is Zero if it's one and zero which is falsely in Python. It's going to omit false So we're passing true or false in a random order to the go program and seeing how go handles it And now let's open the go program Takes an em boolean and just prints it Simple enough. All right. We got a false. We got a true. We got a true. We got a true. We got a false We we can pass this we can pass booleans from Python to go Let's check the reverse Thank you, thank you, thank you All magic Let's look at the Python file first Same basic principle come down to this line Calling the function and passing it in to answer and then printing answer Same basic principle is the Python program I'm Creating a random number between zero and one If it's equal to one then I'm returning true if it's equal to zero I'm returning false and I'm passed that into Python. I want to see if it can handle it one One one one one one one one. Hey, there we go. We got a zero So what we see now is that when the booleans are being passed from go to Python They're represented in terms of zero one which is very understandable zero can be false and one can be positive just like in Python That's falsely and truthy That seems reasonable Let's try let's go with strings strings are interesting So as you see there are a lot of files in here because I made a lot of different attempts Not everything would over smoothly just to prepare you guys We have we're gonna go over a string input string input round two and string input round three All right since there are different rounds I have to note that at times I'm calling different SO files just to see if try to match things to see if things work This is taking the user input as a string and then passing the string just a regular string to go Let's look at the go folk program to see what it does Simple takes in a string prints out a string. Let's try it. Anybody have a favorite string It didn't like seven it didn't like seven So one thing I found like I gave you as a warning early is that everything doesn't pass well Early we saw that inch can be passed without being converted, but it seems that for strings Something is missing. So let's look at round two. So around to what I did differently is I Tried a random function that I saw in the C types module now the C type module if you go to the documentation Has a list of different conversion types from Python to C types This one says create string buffer. I'm gonna try to create a string buffer It's sending hello and the buffer has ten allocation of ten and It's going to the same go program. So we don't have to look at the go program game We can just run this and see what happens. It didn't like that Gross slice cap out of range That sounds very cryptic, let's move on string input Round three at round three. We have a different program for go Try something go mixed on things up there and see if it works. We're going to string input C go. All right We're going to mix something up here. We're gonna try C Char P Which I believe creates strings C type strict strings that are in the type of C or however We want to phrase that sentence and We're passing that into the go function All right, so here we also did something different C Char So apparently when I was going through some of the documentation, it's not all that great on the go side, but We have C Char's This is a pointer So we're taking a pointer to a C Char and then they have functions in go that can convert C types to go types So C go string name will convert a string in C to go Let's see what happens. It's clear. There's this kind of That's not clear. There we go Yeah, I'm gonna clap for myself on that one. So you can oh wow All right, so we covered strings that was input. Let's go to string output string output We're running a little low on time. So I'm gonna go skip through some of the rounds String output round two. Let's start there round one is never good for anybody round two So for round two we learn about the C go stuff so that's gonna be in the output of the go program We're gonna use that in the go program and we learned about the C stuff for C types So C Char and we're gonna try to print out the value Not nothing much has changed Here nothing much has changed Just returning the C pointer string round two we were able to pass a string from go to Python It comes out as a byte string Semantics we passed it So just a quick summary of things that you can and cannot pass lists and Dictionaries they don't have a type conversion in go so you can't pass those so we can just skip that section for floats I was able to pass Floats from Python to go, but I haven't figured out how to pass floats from go to Python I'm still working on that So let's be practical if you wrap and go in Python the reason why you're wrapping go in Python is probably because you Want to use some of those features? Let's say go speed or goes concurrency Well, let's see if we can do that. Oh, and there's a question down there since we're low on time I just tell you the answer we can it's the question of can we pass multiple variables an arbitrary amount of variables to go like Args we do in Python The answer is no when you try to compile that it tells you it's not supported yet So hopefully in the future it will be but as of right now we can do it So in section 3 I created a program for primes That just check to see whether numbers prime. It's not complex. It just goes through all the numbers we've seen this before and I'm going to export that and See how fast it is in Python Just a quick overview what the file looks like calling the prime so passing integer seeing what the answer is integer 17 Okay, we know that can find primes 16 One It doesn't like one 16 false. I still want a bigger number One two three four. I forgot a one in the front. Let's put a one in the front Sorry one two three four five False that came back kind of fast Reasonably, so I'm not gonna benchmark it. So let's look at the other program That is oh cool one minute left. That is prime Channels So the way to do concurrent programming and go is to use go routines and channels And I'm not going to go in specific what those are but Here's a quick example To make a go routine. We take a function and put go in front of it. Ta-da For channels what they are in concept is like a tube you pass numbers through one end and they come out the other And you can put channels at between different functions So what this program is going to do prime channel just Python file real quick It's going to print out all the prime. It's going to count the number of primes below a certain number We want to see how fast we can do it and concurrently and go and then to send the answers of Python. Let's start with 40 12 that was kind of fast Start with Go a little bit bigger the algorithm is still using the same algorithm as before the slow one So it should take some time But as you can see it's kind of fast for computing a lot of those numbers That will return eventually in the meantime The conclusion is Conclude okay, the conclusion is that doing this is somewhat difficult However, it's really kind of cool, and I like it also If you don't have to explicitly pass arguments between the two languages, you're golden You can have like an intermediate file a JSON file a config file that you can read from in those Respective languages and you don't have to worry about all this C stuff. That is the end of my talk