Python For Beginners A Crash Course Guide For Machine Learning and Web Programming (2020) - 40639
Python For Beginners A Crash Course Guide For Machine Learning and Web Programming (2020) - 40639
FOR BEGINNERS
A crash course guide for machine learning and web
programming. Learn a computer language in easy steps
with coding exercises.
JASON TEST
© Copyright 2020 - All rights reserved.
The content contained within this book may not be reproduced, duplicated or transmitted without direct
written permission from the author or the publisher.
Under no circumstances will any blame or legal responsibility be held against the publisher, or author,
for any damages, reparation, or monetary loss due to the information contained within this book. Either
directly or indirectly.
Legal Notice:
This book is copyright protected. This book is only for personal use. You cannot amend, distribute, sell,
use, quote or paraphrase any part, or the content within this book, without the consent of the author or
publisher.
Disclaimer Notice:
Please note the information contained within this document is for educational and entertainment
purposes only. All effort has been executed to present accurate, up to date, and reliable, complete
information. No warranties of any kind are declared or implied. Readers acknowledge that the author is
not engaging in the rendering of legal, financial, medical or professional advice. The content within this
book has been derived from various sources. Please consult a licensed professional before attempting
any techniques outlined in this book.
By reading this document, the reader agrees that under no circumstances is the author responsible for
any losses, direct or indirect, which are incurred as a result of the use of information contained within
this document, including, but not limited to, — errors, omissions, or inaccuracies.
TABLE OF CONTENT
INTRODUCTION
Equivalent Problems
Why use design patterns?
Python Design Patterns
Generative Patterns
Structural Patterns
Behavioral patterns
Python Specific Design Patterns
Type Annotation in Python
4 programming languages to learn, even if you are a humanist
1. Vba
2. SQL
3. R
4. Python
Well, as it were, let's go
Where to get Python?
1.PYTHON CODE OPTIMIZATION WITH CTYPES
Basic optimizations
Built-in Data Structures
List expressions
ctypes
Compiling C code in Python
Structures in Python
Call your code in C
Something More: PyPy
2.FINDING THE PERFECT TOOLKIT: ANALYZING POPULAR PYTHON PROJECT
TEMPLATES
Overview
De facto standards
Flake8
Pytest and coverage.py
Sphinx
Promising tools
Pyproject.toml
Poetry
Dependabot
Personal recommendations
Pre-commit
Pylint
Black
Subtotals
Bonus: Template Recommendations
Django
Data Processing and Analysis
General Purpose Templates
3.HOW ARE BROAD INTEGER TYPES IMPLEMENTED IN PYTHON?
Presentation and Definition
Decoding ob_digit
Decoding ob_size
Storage
Python path
Integer Long Operations
Addition
Subtraction
Multiplication
Division and other operations
Optimizing Frequently Used Integers
4.CREATE A BOT IN PYTHON TO LEARN ENGLISH
Idea
Base structure
Decorators are our everything
Admin panel
Main functionality
We pack the bot
5. THE THERMAL IMAGER ON THE RASPBERRY PI
MLX90640. What is it?
And then what is the GY-MCU90640?
Connect to Raspberry PI
Soft
Results
6.FINDING A FREE PARKING SPACE WITH PYTHON
Decomposing the task
We recognize parking spaces
Recognize cars
We recognize empty parking spaces
Send SMS
Putting it all together
7. CREATING GAMES ON THE PYGAME FRAMEWORK | PART 1
Event handling
Breakout class
Keystroke handling
Mouse event handling
Handling Timer Events
Game process
Moving racket
Moving ball
Setting the initial speed of the ball
Summarize
CREATING GAMES ON THE PYGAME FRAMEWORK | PART 2
Collision Recognition
Collision of a ball with a racket.
Collision with the floor.
Collision with ceiling and walls
Collision with bricks
Game Menu Design Program
Button Creation
Menu Design
Show and hide GameMenu
To summarize
CREATING GAMES ON THE PYGAME FRAMEWORK| PART 3
End of the game
Game over!
Game End Message Display
Saving records between games
Adding Sound Effects and Music
Sound effects
Record your sound effects and messages
Playing background music
Adding Advanced Features
Special effects system
Racket increase
Ball slowdown
More points
Extra lives
Future Features
Go to the next level
Second ball
Lasting records
Bombs and bonuses
Summarize
8.OBJECT-ORIENTED PROGRAMMING (OOP) IN PYTHON 3
Algorithms and data structures
What is object-oriented programming (OOP)?
Python classes
Python Objects (Instances)
How to define a class in Python
Instance attribute
Class attributes
Create Objects
What's the matter?
Review of exercises (# 1)
Instance Methods
Attribute Change
Python object inheritance
Dog Park Example
Extending parent class functionality
Parent and child classes
Overriding parent class functionality
Review of exercises (# 2)
CONCLUSION
INTRODUCTION
Design patterns are reusable model for solving known and common
problems in software architecture.
hey are best described as templates for working with a specific normal
T situation. An architect can have a template for designing certain types of
door frames, which he fit into many of his project, and a software
engineer or software architect must know the templates for solving common
programming tasks.
An excellent presentation of the design pattern should include:
Name
Motivating problem
Decision
Effects
Equivalent Problems
If you thought it was a rather vague concept, you would be right. For
example, we could say that the following “pattern” solves all your problems:
Structural Patterns
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
Behavioral patterns
Chain of responsibility
Command
Iterator
Mediator
Memento
Observer
State
Strategy
Visitor
type checking:
expansion of the IDE functionality in terms of providing information
about the expected types of arguments and the type of return value
for functions:
overload of functions and work with generics:
interaction with other languages:
use in predicate logical functions:
mapping of requests in databases:
marshaling parameters in RPC (remote procedure call)
4. Python
Python is an even more powerful and popular programming language (by
the way, it really has nothing to do with python ). Like R, Python has special
libraries that work with Excel files, and it also knows how to collect
information from the Internet (forget about manually driving data into
tables!). You can write endlessly about Python, but if in a nutshell - it’s a
really convenient and quick tool that is worth mastering if you want to
automate routine operations, develop your own algorithmic thinking and
generally keep up with technical progress.
Unlike R, Python does not have one of the most convenient programming
environments - here everyone is free to choose to taste. For example, we
recommend IDLE, Jupyter Notebook, Spyder, for more advanced
programmers - PyCharm.
You can learn how to program in Python and analyze data using it in our
online course “Python. Data Analysis ". The consultants from Big4 and Big3
will tell you how to work with libraries, create programs and algorithms for
processing information.
Well, as it were, let's go
Well, if you (You, He, She, It, They) are reading this means we are
starting our first Python lesson. Hooray (...) Today you will learn how to
code. And what's more, you'll learn how to code in Python.
But to start coding you need Python. Where to get it?
It's alife
Yes, you just wrote your first program. Now you are a programmer (NO).
Let's look at the structure of the print function. This function consists of
the name - print, and the place where we throw what this function should
output. If you want to display any text, you need to write it in quotation mark,
either in single or double.
But now let's enter some text. For this, Python has an input function. To
accept something, you need to write the following:
What does this program do for us? First we introduce something, at the
moment we don’t care what it is, text or numbers. After, our program
displays what you wrote.
But how to make sure that we write the text before entering something?
Really simple. There are two ways to do this.
How to do
But this is not aesthetically pleasing. We are here with you all the
aesthetes. And so in order for you to enter values on a separate line, you can
do the following:
We add up the two numbers that the user enters. (Wow, I found out how
much 2 + 2 will be)
1. PYTHON CODE OPTIMIZATION WITH
CTYPES
Content:
1. Basic optimizations
2. Styles
3. Python compilation
4. Structures in Python
5. Call your code in C
6. Pypy
Basic optimizations
A few notes:
Pylint
Although Pylint is criticized for being slow, although this tool is criticized
for the features of its settings, I can say that it allowed me to grow above
myself in the field of programming.
He gives me specific instructions on those parts of the code that I can
improve, tells me how to make the code better comply with the rules. For a
free tool, this alone is already very much. Therefore, I am ready to put up
with the inconvenience associated with Pylint.
Black
Black at the root of the debate over where to put spaces in the code. This
protects our teams from an empty talk and meaningless differences in files
caused by different editors' settings.
In my case, it brightens up what I don't like about Python (the need to use
a lot of spaces). Moreover, it should be noted that the Black project in 2019
joined the Python Software Foundation, which indicates the seriousness and
quality of this project.
As a result, I want to say that if you still do not use pre-commit, Black, and
Pylint - think about whether these tools can benefit your team.
Subtotals
Twelve of the eighteen investigated templates were created using the
cookiecutter framework. Some of those templates where this framework is
not used have exciting qualities.
But given the fact that cookiecutter is the leading framework for creating
project templates, those who decide to use a template that did not use a
cookiecutter should have excellent reasons for this. Such a decision should be
very well justified.
Those who are looking for a template for their project should choose a
template that most closely matches his view of things. If you, when creating
projects according to a precise template, continuously have to reconfigure
them in the same way, think about how to fork such a template and refine it,
inspired by examples of templates from my list.
And if you are attracted to adventure - create your template from scratch.
Cookiecutter is an excellent feature of the Python ecosystem, and the simple
process of creating Jinja templates allows you to quickly and easily do
something your own.
Bonus: Template Recommendations
Django
Together with the most popular Django templates, consider using we
make-django-template. It gives the impression of a deeply thought out
product.
hen you write in a low-level language such as C, you are worried about
W choosing the right data type and qualifiers for your integers, at each
step, you analyze whether it will be enough to use it simply intor
whether you need to add longor even long double. However, when writing
code in Python, you don’t need to worry about these “minor” things, because
Python can work with numbers of integerany size type .
In C, if
you try to calculate 220,000 using the built-in function powl, you will get the
output inf.
But in
Python, making this easier than ever is easy:
It must be under the hood that Python is doing something very beautiful, and
today we will find out the exactly what it does to work with integers of
arbitrary size!
Presentation and Definition
Integer in Python, this is a C structure defined as follows:
Other types that have PyObject_VAR_HEAD:
PyBytesObject
PyTupleObject
PyListObject
This means that an integer, like a tuple or a list, has a variable length, and
this is the first step to understanding how Python can support work with giant
numbers. Once expanded, the macro _longobjectcan be considered as:
There PyObjectare some meta fields in the structure that are used for
reference counting (garbage collection), but to talk about this, we need a
separate article. The field on which we will focus this ob_digitand in a bit
ob_size.
Decoding ob_digit
ob_digitIs a statically allocated array of unit length of type digit (typedef
для uint32_t). Since this is an array, it ob_digitis a pointer primarily to a
number, and therefore, if necessary, it can be increased using the malloc
function to any length. This way, python can represent and process very large
numbers.
Typically, in low-level languages such as C, the precision of integers is
limited to 64 bits. However, Python supports integers of arbitrary precision.
Starting with Python 3, all numbers are presented in the form bignum and are
limited only by the available system memory.
Decoding ob_size
ob_sizestores the number of items in ob_digit. Python overrides and then
uses the value ob_sizeto to determine the actual number of elements
contained in the array to increase the efficiency of allocating memory to the
array ob_digit.
Storage
The most naive way to store integer numbers is to store one decimal digit
in one element of the array. Operation such as additional and subtractions can
be performed according to the rules of mathematics from elementary school.
With this approach, the number 5238 will be saved like this:
This approach is inefficient because we will use up to 32-bit digits
(uint32_t) for storing a decimal digit, which ranges from 0 to 9 and can be
easily represented with only 4 bits. After all, when writing something as
universal like python, the kernel developer needs to be even more inventive.
So, can we do better? Of course, otherwise, we would not have posted this
article. Let's take a closer look at how Python stores an extra-long integer.
Python path
Instead of storing only one decimal digit in each element of the array
ob_digit, Python converts the numbers from the number system with base 10
to the numbers in the system with base 230 and calls each element as a
number whose value ranges from 0 to 230 - 1.
In the hexadecimal number system, the base 16 ~ 24 means that each
"digit" of the hexadecimal number ranges from 0 to 15 in the decimal number
system. In Python, it’s similar to a “number” with a base of 230, which
means that the number will range from 0 to 230 - 1 = 1073741823 in decimal.
In this way, Python effectively uses almost all of the allocated space of 32
bits per digit, saves resources, and still performs simple operations, such as
adding and subtracting at the math level of elementary school.
Depending on the platform, Python uses either 32-bit unsigned integer
arrays or 16-bit unsigned integer arrays with 15-bit digits. To perform the
operations that will be discussed later, you need only a few bits.
Example: 1152921504606846976
As already mentioned, for Python, numbers are represented in a system
with a base of 230, that is, if you convert 1152921504606846976 into a
number system with a base of 230, you will get 100.
1152 9215 0460 6846 976 = 1 * ((230) 2 + 0) * ((230) 1 + 0) *( (230) 0)
Since it is the ob_digitfirst to store the least significant digit, it is stored as
001 in three digits. The structure _longobjectfor this value will contain:
ob_size like 3
ob_digit like [0, 0, 1]
We created a demo REPL that will show how Python stores an integer inside
itself, and also refers to structural members such as ob_size, ob_refcountetc.
Integer Long Operations
Now that we have a pure idea of how Python implements integers of
arbitrary precision, it is time to understand how various mathematical
operations are performed with them.
Addition
Integers are stored "in numbers," which means that addition is as simple as
in elementary school, and the Python source code shows us that this is how
addition is implemented. A function with a name x_addin a file
longobject.cadds two numbers.
The code snippet above is taken from a function x_add. As you can see, it
iterates over a number by numbers and performs the addition of numbers,
calculates the result and adds hyphenation.
It becomes more interesting when the result of addition is a negative
number. The sign ob_sizeis an integer sign, that is, if you have a negative
number, then it ob_sizewill be a minus. The value ob_sizemodulo will
determine the number of digits in ob_digit.
Subtraction
Just as addition takes place, subtraction also takes place. A function with a
name x_subin the file longobject.csubtracts one number from another.
The code snippet above is taken from a function x_sub. In it, you see how
the enumeration of numbers occurs and subtraction is performed, the result is
calculated and the transfer is distributed. Indeed, it is very similar to addition.
Multiplication
And again, the multiplication will be implemented in the same naive way
that we learned from the lessons of mathematics in elementary school, but it
is not very efficient. To maintain efficiency, Python implements the
Karatsuba algorithm, which multiplies two n-digit numbers in O (nlog23)
simple steps.
The algorithm is not simple and its implementation is beyond the scope of
this article, but you can find its implementation in functions and in the file
.k_mul k_lopsided_mullongobject.c
Division and other operations
All operations on integers are defined in the file longobject.c, they are very
simple to find and trace the work of each. Attention: A detailed
understanding of the work of each of them will take time, so pre-stock up
with popcorn.
Optimizing Frequently Used Integers
Python preallocates a small number of integers in memory ranging from -5
to 256. This allocation occurs during initialization, and since we cannot
change integers (immutability), these pre-allocated numbers are singleton and
are directly referenced instead of being allocated. This means that every time
we use/create a small number, Python instead of reallocation simply returns a
reference to the previously allocated number.
Such optimization can be traced in the macro IS_SMALL_INTand
function get_small_intc longobject.c. So Python saves a lot of space and time
in calculating commonly used integer numbers.
4. CREATE A BOT IN PYTHON TO LEARN
ENGLISH
o, this is not one of the hundreds of articles on how to write your first
N Hello World bot in Python. Here you will not find detailed instructions
on how to get an API token in BotFather or launch a bot in the cloud. In
return, we will show you how to unleash the full power of Python to the
maximum to achieve the most aesthetic and beautiful code. We perform a
song about the appeal of complex structures - we dance and dance. Under the
cut asynchrony, its system of saves, a bunch of useful decorators, and a lot of
beautiful code.
Disclaimer: People with brain OOP and adherents of the “right” patterns
may ignore this article.
Idea
To understand what it is like not to know English in modern society,
imagine that you are an 18th-century nobleman who does not know French.
Even if you are not very well versed in history, you can still imagine how
hard it would be to live under such circumstances. In the modern world,
English has become a necessity, not a privilege, especially if you are in the IT
industry.
The project is based on the catechism of the future: the development of a
neural network as a separate unit, and education, which is based on games
and sports spirit. Isomorphic paradigms have been hanging in the air since
ancient times, but it seems that over time, people began to forget that the
most straightforward solutions are the most effective.
Here is a shortlist of the basic things I want to put together:
Base structure
The bot will be based on the python-telegram-bot (ptb) library. I use
loguru as a logger , though there is one small snag here. The fact is that ptb
by default uses a different logger (standard logging) and does not allow you
to connect your own. Of course, it would be possible to intercept all journal
messages globally and send them to our registrar, but we will do it a little
easier:
self._counter = 0
self.CACHE SIZE = cache st ze
def add (self, d s: class, file: str) -> NDne:
All or's to fasten a class to a sa ver
+ cls - Inst an ce of the class
+ file - The fi1 e the instari ce is 'orking with
self._cache_files.append (file)
self._cl asses.append (cls)
if fi1e i s eotptl’ (fi1e): return hDne
1 ogger. opt d an.' = True) .debug (fT or {cl s . class names)
file (file) is not empty' ')
fa data in sells oad (file):
is.save nDn Caclti ng (data)
cl ear_file (file)
sel f._counter = 0
How command modules get access to the main class, we will consider
further.
All out of nothing
Ptb handlers have two arguments - update and context, which store all the
necessary information stack. Context has a wonderful chat_data argument
that can be used as a data storage dictionary for chat. But I do not want to
constantly refer to it in a format context.chat_data['data']. I would like
something light and beautiful, say context.data. However, this is not a
problem.
We continue to simplify our lives. Now I want all the necessary information
for a specific user to be in quick access context.
Now we’ll completely become impudent and fasten our bot instance to
context:
It looks cool, but it doesn't work. It's all about the bind_context decorator,
which will always return the name of the wrapper function. Correct this
misunderstanding.
There are many message handlers in the bot, which, by design, should
cancel the command when entering zero. Also I need to discard all edited
posts.
We do not forget at the same time about the most important decorator -
@run_asyncon which asynchrony is based. Now we collect the heavy
function.
Remember that asynchrony is a Jedi sword, but with this sword, you can
quickly kill yourself.
Sometimes programs freeze without sending anything to the log.
@logger.catch, the last decorator on our list, ensures that any error is
correctly reported to a logger.
Admin panel
Let's implement an admin panel with the ability to receive/delete logs and
send a message to all users.
The add_conversation_handler function allows you to add a conversation
handler in a minimalistic way:
Main functionality
Let's teach our bot to add new words to user dictionaries.
We pack the bot
Before we pack our bot, add proxy support and change log levels from the
console.
There are two type of sensor is available version, the cases of which differ
in the viewing angle of the matrix. A more squat structure A overlooks the
outside world at an angle of 110 (horizontal) at 75 (vertical) degrees. B -
under 55 by 37.5 degrees, respectively. The device case has only four outputs
- two for power, two for communicating with the control device via the I2C
interface. Interested datasheets can be downloaded here.
And then what is the GY-MCU90640?
As you see, there are two types of boards, with a narrow or wide-angle
version of the sensor onboard.
Which version is best for you? A good question, unfortunately, I had it
only after the modules was already ordered and received. For some reason, at
this time of the orders, I did not pay attention to these nuances. But in vain.
A wider version will be useful on self-propelled robots or in security
system (the field of view will be larger). According to the datasheets, it also
has less noise and higher measurement accuracy.
But for visualization tasks, I would more recommend a more “long-range”
version of B. For one very significant reason. In the future, when shooting, it
can be deployed (manually or on a platform with a drive) and take composite
"photos," thereby increasing the more than a modest resolution of 32 by 24
pixels. Collects thermal images 64 by 96 pixels, for example. Well, all right,
in the future, the photos will be from the wide-angle version A.
Connect to Raspberry PI
There are two ways to control the thermal imaging module:
1. Shorten the “SET” jumper on the board and use I2C to contact
the internal microcontroller MLX90640 directly.
2. Leave the jumper alone and communicate with the module
through a similar interface installed on the STM32F103 board via
RS-232.
If you write in C ++, it will probably be more convenient to ignore the
extra microcontroller, short-circuit the jumper and use the API from the
manufacturer, which lies here.
Humble pythonists can also go the first way. It seems like that there are a
couple of Python libraries ( here and here ). But unfortunately, not a single
one worked for me.
Advanced pythonists can write a module control driver in Python. The
procedure for obtaining a frame is described in detail in the datasheet. But
then you will have to prescribe all the calibration procedures, which seems
slightly burdensome. Therefore, I had to go the second way. It turned out to
be moderately thorny, but quite passable.
Thanks to the insight of Chinese engineers or just a happy coincidence, the
shawl turned out to have a perfect location of the conclusions:
It remains only to put the block and insert the scarf into the raspberry
connector. A 5 to 3 Volt converter is installed on the board, so it seems that
nothing threatens Raspberry's delicate Rx and Tx terminals.
It should be added that the connection according to the first option, is also
possible, but requires more labor and solder skill. The board must be installed
on the other side of the Raspberry connector (shown in the title photo of this
post).
Soft
On a well-known Chinese site, such a miracle is offered to access the GY-
MCU90640:
Most likely, there should be some description of the interaction protocol
with the microcontroller installed on the board, according to which this
software product works! After a brief conversation with the seller of scarves
(respect to these respected gentlemen), such a protocol was sent to me. It
appeared in pdf and pure Chinese.
Thanks to Google’s translator and active copy-paste, after about an hour
and a half, the protocol was decrypted, anyone can read it on Github. It
turned out that the scarf understands six basic commands, among which there
is a frame request on the COM port.
Each pixel of the matrix is, in fact, the temperature value of the object that
this pixel is looking at. The temperature in degrees Celsius times 100
(double-byte number). There is even a special mode in which the scarf will
send frames from the matrix to the Raspberry 4 times per second.
THE SOFTWARE IS PROVIDED ‘AS IS‘, WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENf SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES Ofi OTHER LIABILITY, WHETHER IN ANACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ” “
norm =
up.nint8 ((f, 100 - Twin) • 255, (Tmax-Tmin))
norm.shy = (24.32)
T n = 40
Tmin = 20
Results
The script polls the thermal imaging matrix and outputs the frames to the
monitor console on which the Raspberry PI is connected, four times per
second. This is enough not to experience significant discomfort when
shooting objects. To visualize the frame, the OpenCV package is used. When
the “s” button is pressed, the thermal imaging “heat maps” in jpg format are
saved in the folder with the script.
For more information, I deduced the minimum and maximum temperatures
on the frame. That is, looking at the color, you can see what approximately
the temperature of the most heated or chilled objects. The measurement error
is approximately a degree with a larger side. The thermal range is set from 20
to 40 degrees. Exit the script by pressing Ctrl + C.
The script works approximately the same on both the Raspberry Pi Zero W
and the Pi 3 B +. I installed the VNC server on the smartphone. Thus, picking
up raspberries connected to a power bank and a smartphone with VNC
running, you can get a portable thermal imager with the ability to save
thermal images. Perhaps this is not entirely convenient, but quite functional.
After the first start, an incorrect measurement of the maximum
temperature is possible. In this case, you need to exit the script and rerun it.
That is all for today. The experiment with a home-made thermal imager
turned out to be successful. With the helping of this device, it is quite
possible to conduct a thermal imaging inspection of the house on your own,
for example.
Due to the lower temperature contrast than indoors, the pictures were not
very informative. In the photo above, the whole house is on two sides. On the
bottom - photos of different windows.
In the code, I changed only the temperature range. Instead of +20 ... + 40, I
set -10 ... + 5.
6. FINDING A FREE PARKING SPACE WITH
PYTHON
live in a proper city. But, like in many others, the search for a parking
I space always turns into a test. Free spaces quickly occupy, and even if you
have your own, it will be difficult for friends to call you because they will
have nowhere to park.
So I decided to point the camera out the window and use deep learning so
that my computer tells me when the space is available:
It may sound complicated, but writing a working prototype with deep
learning is quick and easy. All the necessary components are already there -
you just need to know where to find them and how to put them together.
So let's have some fun and write an accurate free parking notification
system using Python and deep learning
Decomposing the task
When we have a difficult task that we want to solve with the help of
machine learning, the first step is to break it down into a sequence of simple
tasks. Then we can use various tools to solve each of them. By combining
several simple solutions, we get a system that is capable of something
complex.
Here is how I broke my task:
The video stream from the webcam directed to the window enters the
conveyor input: Through the pipeline, we will transmit each frame of the
video, one at a time.
The first point is to recognize all the possible parking spaces in the frame.
Before we can look for unoccupied places, we need to understand in which
parts of the image there is parking.
Then on each frame you need to find all the cars. This will allow us to
track the movement of each machine from frame to frame.
The third step is to determine which places are occupied by machines and
which are not. To do this, combine the results of the first two steps.
Finally, the program should send an alert when the parking space becomes
free. This will be determined by changes in the location of the machines
between the frames of the video.
Each of the step can be completed in different ways using different
technologies. There is no single right or wrong way to compose this
conveyor: different approaches will have their advantages and disadvantages.
Let's deal with each step in more detail.
We need to scan this image somehow and get a list of places to park:
The solution “in the forehead” would be to simply hardcode the locations
of all parking spaces manually instead of automatically recognizing them.
But in this case, if we move the camera or want to look for parking spaces on
another street, we will have to do the whole procedure again. It sounds so-so,
so let's look for an automatic way to recognize parking spaces.
Alternatively, you can search for parking meters in the image and assume
that there is a parking space next to each of them:
In this two-dimensional array, each row reflects one frame of the parking
space. And each column indicates how strongly each of the places intersects
with one of the detected machines. A result of 1.0 means that the entire space
is entirely occupied by the car, and a low value like 0.02 indicates that the car
has climbed into place a little, but you can still park on it.
To find unoccupied places, you just need to check each row in this array. If
all numbers are close to zero, then most likely, the place is free!
However, keep in mind that object recognition does not always work
correctly with real-time video. Although the model based on Mask R-CNN is
wholly accurate, from time to time, it may miss a car or two in one frame of
the video. Therefore, before asserting that the place is free, you need to make
sure that it remains so for the next 5-10 next frames of video. This way, we
can avoid situations when the system mistakenly marks a place empty due to
a glitch in one frame of the video. As soon as we make sure that the place
remains free for several frames, you can send a message!
Send SMS
The last part of our conveyor is sending SMS notifications when a free
parking space appears.
Sending a message from Python is very easy if you use Twilio. Twilio is
an accessible API that allows you to send SMS from almost any
programming language with just a few lines of code. Of course, if you prefer
a different service, you can use it. I have nothing to do with Twilio: it's just
the first thing that come to brain .
To using Twilio, sign-up for a trial account, create a Twilio phone number,
and get your account authentication information. Then install the client
library:
After that, use the following code to send the message:
To add the ability to send messages to our script, just copy this code there.
However, you need make sure that the message is not sent on every frame,
where you can see the free space. Therefore, we will have a flag that in the
installed state will not allow sending messages for some time or until another
place is vacated.
Putting it all together
# Location of parking spaces.
To run that code, you first need to install Python 3.6+, Matterport Mask R-
CNN, and OpenCV.
I specifically wrote the code as simple as possible. For example, if he sees
in the first frame of the car, he concludes that they are all parked. Try
experiment with it and see if you can improve its reliability.
Just by changing the identifiers of the objects that the model is looking for,
you can turn the code into something completely different. For example,
imagine that you are working at a ski resort. After making a couple of
changes, you can turn this script into a system that automatically recognizes
snowboarders jumping from a ramp and records videos with cool jumps. Or,
if you work in a nature reserve, you can create a system that counts zebras.
You are limited only by your imagination.
7. CREATING GAMES ON THE PYGAME
FRAMEWORK | PART 1
Breakout class
The Breakout class implements most of the knowledge about how
Breakout is controlled. In this tutorial series, we will meet the Breakout class
several times. Here are the lines that various event handlers register.
It should be noted that all key events (for both the left and right “arrows”)
are transmitted to one racket handler method.
Keystroke handling
The Game class calls registered handlers for each key event and passes the
key. Note that this is not a Paddle
class. Into Breakout, the only object that is interested in such events is a
racket. When you press or release a key, its method is handle() called. The
Paddle object does not need to know if this was a key press or release event,
because it controls the current state using a pair of Boolean variables:
moving_left and moving_right . If moving_left True, it means that the
"left" key was pressed, and the next event will be the release of the key,
which will reset the variable. The same applies to the right key. The logic is
simple and consists of switching the state of these variables in response to
any event.
Game process
Gameplay (gameplay) is where the Breakout rules come into play. The
gameplay consists in moving various objects in response to events and in
changing the state of the game based on their interactions.
Moving racket
You saw earlier that the Paddle class responds to arrow keys by updating
its
fields moving_left and moving_right . The movement itself occurs in a
method update(). Certain calculations are performed here if the racket is
close to the left or right edge of the screen. We do not want the racket to go
beyond the boundaries of the screen (taking into account a given offset).
Therefore, if the movement moves the object beyond the borders, then the
code adjusts the movement so that it stops right at the border. Since the racket
only moves horizontally to the vertical component of the movement is always
zero.
Moving ball
The ball simply uses the functionality of the base class GameObject,
which moves objects based on their speed (its horizontal and vertical
components). As we will soon see, the speed of a ball is determined by many
factors in the Breakout class. Since the movement consists simply of adding
speed to the current position, the direction in which the ball moves is
completely determined by the speed along the horizontal and vertical axes.
Setting the initial speed of the ball
The Breakout ball comes out of nowhere at the very beginning of the game
every time a player loses his life. It simply materializes from the ether and
begins to fall either exactly down or at a slight angle. When the ball is created
in the method create_ball(), it gets the speed with a random horizontal
component in the range from - 2 to 2 and the vertical component specified in
the config.py module (the default value is 3 ).
Summarize
In this part, we looked at handling events such as keystrokes, mouse
movements, and mouse clicks. We also examined some elements of Breakout
gameplay: moving the racket, moving the ball, and controlling the speed of
the ball.
In the fourth part, we will consider the important topic of collision
recognition and see what happens when the ball hits different game objects: a
racket, bricks, walls, ceiling, and floor. Then we will pay attention to the
game menu. We will create our buttons, which we use as a menu, and will be
able to show and hide if necessary.
Menu Design
The function create_menu () create a menu with two buttons with the text
'PLAY' and 'QUIT.' It has two built- in function, on _ play () and on _ quit
() which it pass to the correspond button. Each button is add to the list
objects (for rendering), as well as in the field menu _ buttons .
Let's see how these sound effects load and play at the right time. First, play
sound effects (or background music), we need to initialize the Pygame sound
system. This is done in the Game class:pygame.mixer.pre_init(44100, 16, 2,
4096)
Then, in the Breakout class, all sound effects are loaded from config into
the object pygame mixer Soundand stored in the dictionary:
Now we can play sound effects when something interesting happens. For
example, when a ball hits a brick:
The sound effect is played asynchronously: that is, the game does not stop
while it is playing. Several sound effects can be played at the same time.
Record your sound effects and messages
Recording your sound effects can be a simple and fun experience. Unlike
creating visual resources, it does not require much talent. Anyone can say
"Boom!" or “Jump,” or shout, “They killed you. Get lucky another time! ”
Playing background music
Background music should play continuously. Theoretically, a very long
sound effect can be created, but the looped background music is most often
used. Music files can be in .wav, .mp3, or .midi format. Here's how the music
is implemented:
Only one background music can play at a time. However, several sound
effects can be played on top of background music. This is what is called
mixing.
Adding Advanced Features
Let's do something curious. It is interesting to destroy bricks with a ball,
but it quickly bothers. What about the overall special effects system? We will
develop an extensible special effects system associated with some bricks,
which is activated when the ball hits the brick.
This is what the plan will be. Effects have a lifetime. The effect begins
when the brick collapses and ends when the effect expires. What happens if
the ball hits another brick with a special effect? In theory, you can create
compatible effects, but to simplify everything in the original implementation,
the active effect will stop, and a new effect will take its place.,
Special effects system
In the most general case, a special effect can be defined as two purposes.
The first role activates the effect, and the second reset it. We want attach
effects to bricks and give the player a clear understanding of which bricks
special , so they can try to hit them or avoid them at certain points.
Our special effects are determined by the dictionary from the module
breakout.py. Each effect has a name (for example, long_paddle) and a value
that consists of a brick color, as well as two functions. Functions are defined
a lambda functions that take a Game instance, which includes everything that
can change the special effect in Breakout.
When creating bricks, they can be assigned one of the special effects. Here
is the code:
effect field, which usually has the value None, but (with a probability of
30%) may contain one of the special effects defined above. Note that this
code does not know what effects exist. He simply receives the effect and
color of the brick and, if necessary, assigns them.
In this version of Breakout, we only trigger effects when we hit a brick,
but you can come up with other options for triggering events. The previous
effect is discarded (if it existed), and then a new effect is launched. The reset
function and effect start time are stored for future use.
If the new effect is not launched, we still need to reset the current effect
after its lifetime. This happens in the method update(). In each frame, a
function to reset the current effect is assigned to the field reset_effect. If the
time after starting the current effect exceeds the duration of the effect, then
the function is called reset_effect(), and the field reset_effecttakes the value
None (meaning that there are currently no active effects).
Racket increase
The effect of a long racket is to increase the racket by 50%. Its reset
function returns the racket to its normal size. The brick has the color Orange.:
Ball slowdown
Another effect that helps in chasing the ball is slowing the ball, that is,
reducing its speed by one unit. The brick has an Aquamarine color.
More points
If you want great results, then you will like the effect of tripling points,
giving three points for each destroyed brick instead of the standard one point.
The brick is dark green.
Extra lives
Finally, a very useful effect will be the effect of extra lives. He just gives
you another life. It does not need a reset. The brick has a gold color.
Future Features
There are several logical directions for expanding Breakout. If you are
interested in trying on yourself in adding new features and functions, here are
a few ideas.
Second ball
The effect of temporarily adding a second ball will create enormous chaos.
The difficulty here is to treat both balls as equal, regardless of which one is
the original. When one ball disappears, the game continues with the only
remaining. Life is not lost.
Lasting records
When you have levels with increasing difficulty, it is advisable to create a
high score table. You can store records in a file so that they are saved after
the game. When a player breaks a record, you can add small pizzas or let him
write a name (traditionally with only three characters).
Bombs and bonuses
In the current implementation on, all special effects are associated with
bricks, but you can add effects (good and bad) falling from the sky, which the
player can collect or avoid.
Summarize
Developing Breakout with Python 3 and Pygame has proven to be an
enjoyable experience. This is a compelling combination for creating 2D
games (and for 3D games too). If you love Python and want to create your
games, then do not hesitate to choose Pygame.
8. OBJECT-ORIENTED PROGRAMMING
(OOP) IN PYTHON 3
Python Classes
Object Instances
Definition and work with methods
OOP Inheritance
You start with a classkeyword to indicate that you are creating a class, then
you add the class name (using the CamelCase notation starting with a capital
letter).
Also here we used the Python keyword pass. This is huge often used as a
placeholder where the code will eventually go. This allows us to run this code
without generating an error.
Note: the above code is correct in Python 3. On Python 2.x ("deprecated
Python"), you would use a slightly different class definition:
Not the (object)parts in parentheses indicate the parent class that you are
inheriting from (more on this below). In Python-3, this is no longer necessary
because it is implicit by defaulting.
Instance attribute
All class create objects, and all objects contain characteristics called
attributes (called properties in the first paragraph). Use the init ()method to
initialize (for example, determine) the initial attributes of an object by giving
them a default value (state). This method must have atleast one argument, as
well as a self variable that refers to the object itself (for example, Dog).
In our Dog()class, each dog has a specific name and age, which is certainly
important to know when you start creating different dogs. Remember: the
class is intended only to define a dog, and not to create instances of
individual dogs with specific names and ages: we will come back to this
soon.
Similarly, a self variable is also an instance of a class. Since class
instances have different meanings, we could argue, Dog.name = namenot
self.name = name. But since not all dogs have the same name, we must be
able to assign different values for different instances. Hence the need for a
special self variable that will help track individual instances of each class.
NOTE: you will never have to call a init ()method: it is called
automatically when a new instance of Dog is created.
Class attributes
Although instance attributes are specific to each object, class attributes are
the same for all instances, in this case, all dogs.
Thus, although each dog has a unique name and age, each dog will be a
mammal. Let's create some dogs ...
Create Objects
Instantiating is an unusual term for creating a new unique instance of a
class.
For example: >>>
We started by definea new Dog()class, then created two new dogs, each of
which was assigned to different
objects. So, to create an instance of the class, you use the class name
follow by parentheses. Then, to demonstration that each instance is actually
different, we created two more dogs, assigning each variable, and then
checking to see if these variables are equal.
What do you think is the type of class instance? >>>
NOTE Notice how we use point records to access the attributes of each
objects. Save as (dog_class.py), then run program. You should see:
Instance Methods
Instance methods are defined inside the class and are used to get the
contents of the instance. They can also be used to perform operation with the
attribute of our objects. Like a init method, the first argument is always self:
In the last method, speak()we define the behavior. What other types of
behavior can you assign to a dog? Go back to the beginning of the paragraph
to see examples of the behavior of other objects.
Attribute Change
You can changes the value of attributes based on some behavior: >>>
Note. In Python 2.x, there is a difference between the new and old-style
classes. I will not go into details, but you will usually want to specify an
object as the parent class to make sure that you define a new style class if you
are writing Python 2 OOP code.
Dog Park Example
Let's imagine that we are in a dog park. There are several Dog objects
involved in Dog behavior, each with different attributes. In ordinary
conversation, this means that some dogs are running, and some are stretched,
and some are just watching other dogs. Besides, each dog was named its
owner, and since each dog lives and breathes, each is aging.
How else can you distinguish one dog from another? How about a dog
breed: >>>
Each dog breed has slightly different behaviors. To take this into account,
let's create separate classes for each breed. These are child classes of the
parent Dogclass.
Extending parent class functionality
Create new file this called dog_inheritance.py :
Read the comments aloud while you are working with this program to help
you understand what is happening, and then, before you run the program, see
if you can predict the expected result.
You should see:
Conclusion: >>>
Review of exercises (# 2)
Exercise: "Legacy of the dogs"
Create a Petsclass that contains dog instances: this class is completely
separate from the Dogclass. In other words, a Dogclass is not inherited from
the Petsclass. Then assign three instances of the dog to the Petsclass instance
. Start with the following code below. Save the file as pets_class.py . Your
output should look like this:
Start Code:
Solution: “Dog Inheritance”
Exercise: Hungry Dogs
Use the same files, add an instance attribute is_hungry = Truein the
Dogclass. Then add the called method, eat()which when called changes the
value is_hungryto False. Find out how best to feed each dog, and then print
"My dogs are hungry." if everyone is hungry or "My dogs are not hungry." if
everyone is not hungry. The final result should look like this:
1. What class?
2. What an example?
3. What is the relationship between class and instance?
4. What Python syntax is used to define a new class?
5. What is the spelling convention for a class name?
6. How do you create or create an instance of a class?
7. How do you access the attributes and behavior of an instance
of a class?
8. What kind of method?
9. What is the purpose self?
10. What is the purpose of the init method?
11. Describe how inheritance helps prevent code
duplication.
12. Can child classes override the properties of their
parents?
Solution: “Test of understanding” Show hide
Now you should know what classes are, why you want or should use them,
and how to create parent and child classes to better structure your programs.
Remember that OOP is a programming paradigm, not a Python concept.
Most modern programming languages, such as Java, C #, C ++, follow the
principles of OOP. So the good news is that learning the basics of object-
oriented programming will be useful to you in a variety of circumstances -
whether you work in Python or not.
Now the industry does not stand still and there are more and more web
sites, services and companies that need specialists.
Demand for developers is growing, but competition among them is
growing.
To be the best in your business, you need to be almost an absolutely
universal person who can write a website, create a design for it, and promote
it yourself.
In this regard, even a person who has never sat at this computer begins to
think, but should I try?
But very often it turns out that such enthusiasts burn out at the initial stage,
without having tried themselves in this matter.
Or maybe he would become a brilliant creator of code? Would create
something new? This we will not know.
Every day, the threshold for entering programming is growing. You can
never predict what new language will come out.
Such an abundance breaks all the desire of a newly minted programmer
and he is lost in this ocean of information.
All these javascripts of yours ... pythons ... what fear ..
A great misconception is the obligation to know mathematics. Yes, it is
needed, but only in a narrow field, but it is also useful for understanding
some aspects.
The advice that can be given to people who are just starting their activity is
not to chase everything at once. Allow yourself time to think.
What do I want to do? Create a program for everyone to be useful? Create
additional services to simplify any tasks? Or do you really have to go make
games?
The second advice will be to answer my question how much time am I
ready to devote to this? The third point is to think about how fast you want to
achieve your intended result.
So, there is no “easy way” to start programming, it all depends on you -
your interest, what you want to do and your tasks.
In any case, you need to try and do everything possible in your power.
Good luck in your endeavors!