We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Speak Python with Devices

00:00

Formal Metadata

Title
Speak Python with Devices
Subtitle
How Python can be used in IoT/infrastructure automation tasks
Title of Series
Number of Parts
130
Author
License
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
The talk will be a getting start guide on controlling hardware devices with Python. We know Python users are very keen on multitasking and always wish to know more about how it can be used in different tasks. This talk will help audiences exploring new Python skillset. Audiences may be inspired by this talk and apply it to many scenarios, e.g., IoT and infrastructure automation. Intended audiences include: 1. wish to know how Python can be used beyond data analysis and web dev 2. a Pythonista who interested in craft some touchable things 3. want to acquire something new into your Python skillset Audiences are expected to have basic knowledge about: 1. Python syntax and control flow 2. Computer and operating system (especially UNIX) After this talk, audiences will have: 1. The basic idea of controlling devices with Python 2. Expanding their Python skillset. Know how to use Python in another interesting and useful task besides ML, web scrapping, etc.
61
Thumbnail
26:38
95
106
Right angleInternet der DingeStructural loadPerfect groupComputer animationMeeting/Interview
Task (computing)Computer fontMachine codeInterpreter (computing)ComputerSelf-organizationoutputFunction (mathematics)ModemAnalogyString (computer science)AuthorizationLengthContent (media)Meta elementTask (computing)Game controllerOcean currentoutputSpacetimeReading (process)AreaConfiguration spaceEmailSinc functionPhysicalismComputer filePower (physics)Parameter (computer programming)Computing platformOperator (mathematics)2 (number)Expert systemCodeComputerInformation securityWritingFiber bundleOperating systemAnalog computerRepresentational state transferFile systemMappingMultiplication signMereologyLine (geometry)Physical lawVariety (linguistics)Device driverFormal languageDirectory serviceInternet der DingeComputer hardwareTrailPhysical systemExecution unitWorkstationComputer multitaskingRight angleWeb-DesignerKernel (computing)Open sourceGraph coloringVirtual machineError messageTowerNatural numberSoftware repositoryFunction (mathematics)Software engineeringWeb 2.0Slide ruleCorrespondence (mathematics)Computer animation
Multiplication signInterface (computing)AerodynamicsComputer hardwareError messageComputer animation
outputEmailData conversionModule (mathematics)Modulo (jargon)Extension (kinesiology)Device driverLatent heatMacro (computer science)Physical systemUniverse (mathematics)Functional (mathematics)Macro (computer science)System callGroup actionLine (geometry)Type theoryObject (grammar)Physical systemUniverse (mathematics)Computer fileComputerParameter (computer programming)Tape driveCorrespondence (mathematics)Module (mathematics)Multiplication signExtension (kinesiology)WritingLatent heatCodeDevice driverRight angleElectronic mailing listData typeWordData dictionaryComplex (psychology)InformationoutputModal logicCalculationState of matterData structureCASE <Informatik>Keyboard shortcutIntegrated development environmentSpherical capData storage deviceHypermediaDemoscene2 (number)MassComputer simulationUniqueness quantificationInterior (topology)ResultantTheorySlide ruleInternet service providerSemiconductor memoryBucklingOnline helpMedical imagingLengthAddress spaceArmVideoconferencingNetwork socketError messageSinc functionRepresentational state transferTrigonometric functionsRobotics
Device driverSCSISimulationTape driveRobotStructural loadEinsteckmodulExtension (kinesiology)Function (mathematics)Type theoryModule (mathematics)Position operatorProof theoryData storage deviceWritingResultantCodierung <Programmierung>Table (information)Software testingSystem callFunctional (mathematics)Type theoryLibrary (computing)Computer simulationData typeComputer fileDevice driverTape driveParameter (computer programming)Group actionWorkstationString (computer science)MassBitMultiplication signData dictionaryCASE <Informatik>Extension (kinesiology)CodeWhiteboardTerm (mathematics)Pointer (computer programming)
Maxima and minimaTask (computing)Physical systemComputerMassData storage deviceXML
BitPhysical systemMultiplication signSampling (statistics)Zoom lensData storage devicePerfect groupSoftware development kitComputer animation
Function (mathematics)MereologyCASE <Informatik>Musical ensembleConnected spaceType theoryMeeting/Interview
Transcript: English(auto-generated)
Perfect. Welcome, welcome, welcome. It is day two of EuroPython. I hope everyone is keeping fine. I hope everyone is having loads of fun so far. I have here with me already, Peter Chu,
our first speaker for today. Hello, Peter. Hi. Hello, everyone. Hello, everyone. Peter is from Taiwan, right? Yeah, that's true. It's a sunny afternoon there. It's 7 a.m. for me.
Let's do this. Let's kick this off. Perfect. Peter is going to talk to us today about SpeakPython with Devices, how Python can be used in IoT infrastructure automation desk. It's your turn now. Take it away, Peter. Thank you very much. Hello, everyone. Hello, Europe. I'm Peter. Thank you for
joining me in this session. Let me firstly introduce myself. I'm a software engineer in Taiwan and love Python like all you guys. Besides that, I am a conference scholar. I like to share myself while I learn, and more importantly, learn more from others after the
conference. In fact, I have hosted an open source conference in Taiwan. In fact, we have a conference next week, two days, 14 chats, and 1,000 people in person. I would
like to invite you to come. You know, due to the pandemic, so next time maybe. And even though I cannot join you person today, I'm glad we can still hang out in this way. So now it's your turn. Why are you interested in this session? Apparently, I cannot join with you one more at this time, but I guess most of you are working
on data analysis, machine learning, and web development, things like that. These are the most common ways people use Python. But you know that Python is a very powerful and useful language which can be used in a variety of tasks, not just those I mentioned. So if you wish to acquire something new into your Python skill set, you are in the right
talk. We will see how to use the tower of Python to build some amazing things from a tiny IoT device to a large-scale platform system. This is why exactly we will talk about today. Since this talk is for beginners, we will start with some fundamental concepts of the
operating system. On the other hand, if you are already an expert in this area, I'm sorry, but sorry for your disappointment. Let's begin. In this background, the light grandpa stands for LiOS and the dark grandpa means physical hardware devices.
There are two layers in LiOS, user space and user space respectively. We are present for currently being user space. On the other hand, device drivers are living in total space. We know that Alex Kerou is fine in C language. So notice that if the device you can serve does not have a corresponding driver, sorry, but as a Python user, we can do nothing
at all. So in the following slides, let's assume the driver is ready. Fair enough, the OS will represent our devices as some spatial files in the file system. You can see that in the
slide screenshot. Roughly speaking, those files are located in the dev directory in Linux. So you may have heard that in Unix, everything is a file. That's also true for devices. I emphasize this here because it means that we can manipulate a device like a file.
And the great thing is you already how to manipulate a file. Let's see an example. And if you run a snippet here in a REST verified, you can make an outbound LED blink.
You can see the demonstration on my camera. Take it. You can see here something blinking. And yeah, the blinking light bulb is boring, you might think. It's true. But the interesting thing is the code here just open a device file,
write something, then the behavior of the device is changed. Like magic. Do you think it's trivial? Yeah, it is. But last, now the first story. There are one more operation. Few people to know. And it's our leading actor today. ILCT error. ILCT error means
input output control. As is suggested, the purpose of it is to control devices. We don't need it in the LED example since it's the simplest one. We can manipulate it with merely reading and writing. But for many devices, last, now and last. An example is a model.
The nature of things to do when we receive data by reading and send data by writing. But how can we then get a bit of it? The repo example. This is why and where ILCT error plays for. This is why exactly it looks like it has three parameters, file descriptor, request,
and arguments. Notice how your request is constructed with four more parameters. Here are some more details, but I don't want to go through with them. Instead, an analogy may be more helpful. No matter what you work for, you should be all comfortable with
RESTful API, right? I found that we can have a mapping between a RESTful API request and parameters of ILCT error. For example, as you have known, a RESTful API always an HTTP request has two parts, the header and the body. Then we can say ILCT error also has
header and the body. The second parameters of ILCT error, the request is the header. The third parameter of it, the arguments, is the body. From this mapping, I expect you can
understand all parameters of ILCT error without any more. You can read it in seconds.
Yeah, got it. Indeed, many people feel struggle to understand ILCT error for the very first time. So has me. So has I. But after studying, I found out ILCT error is just like HTTP, a common interface to interact with a resource, but the resource is a hardware
device at this time. Now we know ILCT error, but hey, we are Pythonista. How can we use it in Python? And let's get started for a mini example. In this example, we use ILCT error
to get device names. That's why I have on my computer. To achieve it in Python, there are three common criteria we have to meet. The first is to create ILCT request. If you forgot what it is, the request let the driver
know what do you want to do, like act like a RESTful API header. The second is, since we communicate with drivers and drivers are running C, we have to convert the data types between C and Python. And lastly, we have to manage to access ILCT error
in the Python environment. Then we can call it. There are many ways to meet this criteria and we will see two approaches to that. The first approach, we write a C extension module. We have to write some C code in this approach and it only works on C Python.
On the other hand, the second is a pure Python approach. I will explain both of them step by step. And let's start on the first one. Firstly, we define the ILCT function, the thing you intend to do. Here we define a function called getDeviceNet. It has three
arguments. The first is the device file path. The second answer is an address of a piece of memory and its size. We can carry so far it after we call this function. I think the code is well self-explained instead of the 18th file. Let's have a look at it.
We create an ILCTL request at this line. We can see I use a micro named EVIOCG name here. In general, device drivers will provide open micros like this so you can use that easier.
Another thing is remember an ILCTL request is constructed with four parameters. If you expand the micro here, you can see it exactly take four parameters as shown in the line 17. After the ILCTL function is defined, let's implement a wrap function out of it.
This function consumes a device file path and return a corresponding device name. This function is what we actually accessed in our Python code. You can see we have a Python
and return values are proved by type, by address. Python is one of the most common abstract structure provided by C Python. Most able to implement it are converting the data types between C and Python. You can see I use some type prefix macros to do it.
These macros are also provided by C Python. This is a trivial case since the arguments and return types are just string. It can be a state struct or a Python dictionary in some more
complex example. Okay, in the last step, the last step is to do some packaging works. In this approach, what we actually get is a Python module. So to build this module, we have to provide some necessary information, such as module name,
a list of functions available in this module, things like that. Just follow the C Python spec here. So now our module is ready. We can install it, then import it, and then call it in the
usual Python way as shown in the screenshot. We get a device, then go to file specs. Okay, now let's turn to the second approach. In this approach, we do similar things like the first one. During this time, we don't have to write any C code. Again, let's start by creating the IOCTL request. We use a micro to do it in the last time,
but how can we access macros in Python at this time? No, we can. We have to port necessary macros to Python. And fortunately, common macros for creating IOCTL requests
has been plotted by some good guys. So we just need to pull Java-specific macros. And it's usually not a big deal, just a few lines. Again, we have to cover the data type, and it's still trivial here, since we're just going to call Python right here.
Notice that we use a viewing module called fcnt error here. It's provided to use IOCTL function, so we don't have to write any C code at this time. Finally, we get the same resources.
Now, you might be wondering in your mind, enough theories, please give me something in practical. Okay, so let me find a convincing example for you. A couple feet might sound interesting. Just attach a sensor onto a recipe verifier,
then get a sensor status by what we learned today. And then when a car is approaching, open again. Awesome, child. That's interesting, but I want to show you something more impressive. Like this one, do you know what it is? It's a very unique massive storage system
used in nuclear experiments. Unlikely just those media we use, this system is based on magnetic caps, thousands of caps. They use this system to store data of how our universe works.
You can see what's inside of it in the video in the right button. There are many kinds of devices, robot arms, backbone readers, tape drives, things like that. All these devices need to be controlled and coordinate by some system. So this gave us a pretty idea
to show what we can do from what we learned today. So in the following slides, lastly, we build such kinds of system with Python. Let's begin. We will use the tape simulator today so you can make it at home.
You can see my virtual drives and their device file path in the screenshot. We will use them later. And we also need a driver. Enos already has one, so no worry about it. Now let's see how to use the tape storage. To write data in tape cottage, we need to find it
and load it and then rewind our wallet to a correct position and then we can start to write. For proof of concept, we will see how to implement rewind and get status today.
First, let's see how to get tape status. We have a reducing how to manipulate a device with IOCTL and Python, but it's a little bit more complex this time. In the previous case, we just ask string and the device name. And this time we get tape status
in C struct. Notice how we convert C struct into a Python dictionary in the snippet. Let's go to the next one. In the previous one, we used C extension and now let's try a pure
Python approach. You can see how to use the struct module here to encode the arguments and decode the term values in this code snippet. And here is the last code snippet. And besides the board to recover today, the C types module is also very useful.
C types is a pointer function library for Python. It provides the compatible data type and allowing C functions like IOCTL. We can see how we use it. We implement the y function in this code snippet and you can run code snippet and get the result like this.
You can see how we fetch and change the tape position. This is the first step to build a massive tape storage we just saw. Okay, that's it. Summary. The purpose of this
talk is to let you know we can use Python to build some amazing things for our car to be there to our massive storage system. You just need to know how the computer works and how to manipulate a device in Python and you can make it. Thank you. Yay, thank you. Thank you very
much. Wonderful. We took a little bit less time so let's go, let's see who has questions.
So far we didn't have any questions on Discord and I don't think we have any questions on Zoom, not yet. Okay, that sounds great. I think it's a bit early for everyone.
No, someone is typing a question, I believe. Anika is saying it's applauding as well. Perfect. So our first question, so do you know any robotic kits that would be great to experiment with? You said the storage system I just saw,
I just saw. Okay, and do you have any other suggestions as well or like that's your favorite? Sorry, can you say that again? Is the one you just showed, is that your favorite or do you
have other suggestions as well? Oh no, I just, in the past few years, I tried to work on a sample jar just like the system I showed you, so I take it as an example.
Okay, so what are we getting? We're waiting for questions. Yeah, tell me more about your participation on PyCon Taiwan. How long have you been part of the community? Yeah, I love PyCon Taiwan. In fact, I like all PyCons. In the past few years, I have
attended PyCon Hong Kong and PyCon Japan and yeah, I think the PyCon community have you know, big connections, international connections, so I enjoy, yeah, all PyCons,
not just Taiwan. Okay, there is, yeah, the PyCon, the next PyCon Japan is coming up soon, right? The CFP just closed. Yeah, that's true. Yeah, we're quite excited. Did you just submit anything to it? Yeah, but you know, I love pandemics, so maybe we can just virtually traveling around the world is new. Yes, I think that's going to be the case,
but well, it's okay. Any more questions? Anyone has any other questions for Peter here? I see people typing, but I don't see, I don't see their actual outputs.
Well, maybe just cast. Yeah, let's see. No, I don't think we have anything else right now.
Okay. Well, if you have any more questions for Peter, please just type your questions on speak Python with devices. There is room for that under the knee breakouts. So well,
Peter, thank you very, very much. It was an absolute pleasure. Thank you for joining us. Yeah, thank you. And I will be there waiting for your more questions. You can have a coffee and think about us. Thank you. Thank you. Thank you.