Learning Python and Kodi Add-on Development - A Journey..
#16
(2015-07-21, 23:14)zag Wrote: Very simple here, we are just looping though a list of groceries, and assigning each item of the list to a new variable called "food". We then print out each food item to a new line (this seems to be automatic with python).

Code:
for item in ages:
    print item, ages[item]

Here we are getting slightly more complicated by looping through a dictionary and finding the object name, and the data (in this case the age). Printing the item in brackets means it looks at the data of the dictionary, rather than the object name.


If you need to loop through both dictionary keys and values, here's more correct 'Pythonic' way:
Code:
ages = {'dad': 50, 'mum': 45, 'sister': 21}
for name, age in ages.iteritems():
    print name, age
It's worth mentioning that Python dict stores its data in random order, so there's no way to predict in which order your items will be iterated. If you need a dictionary that keeps the order in which the items have been added, collections module provides OrderedDict class that has the same properties as dict, but it keeps the order of keys.

Also please note that Python does have the official style guide for coding - PEP-8.
Reply
#17
wow.. Pretty Amazing. Thank you. I am going to follow the tread and learn everything I can as well.
Reply
#18
Day 5
Functions are used in most object orientated languages to perform a piece of code multiple times. We call this a function because it can be re-usable over and over again so you don't have to repeat code. The idea is that most things that happen in your python program will be functions. This allows your basic program code to be very simple, and the real work is done inside the functions.

Code:
def hello(x):
    return 'Hello to ' + x

print hello('Zag')

What happens here is we define a function called "hello" in the first line, then tell the function to return the text "Hello to" and then the variable "x". The last line just calls the function and sends the parameter "Zag" which assigns "Zag" to the x variable, then runs the function code which writes the full line of text "Hello to Zag". As you can see, the fact that we can send a variable to the function now means it has unlimited possibilities to process the text easily and quickly. This is a very simple example but functions can be far more complex doing lots of different things with different data.

Now lets look at how we can send default parameters to the function.

Code:
def name(first='zag',last='Kodi'):
    print '%s %s' % (first,last)
    
name ()

name (last='XBMC')

So its basically the same but we are defining a default value to the first and last names. This function can now be called with no parameters "name ()" at all and return the default variable names. Or we can override them with our own values such as "name (last='XBMC')". This is useful because we may want to always have a default variable for common operations.

Sometimes you won't know how many parameters you want to have, so python allows you to have an unknown number of parameters passed to a function like this:

Code:
def list(*food):
    print food

list ('apples', 'beef', 'beans')

All that we are doing here is defining a parameter in the function that is a tupple with the asterisk *. This means it can be any number of data items sent to the function. When we call the function list later, we can enter lots of different food types and all of them will be printed to the screen no mater how many we use. You can mix and match static parameters or mutiple parameters by defining them in the function on the first line.

Hopefully this gets you on your way with functions, they will be the building blocks of most of your programs. Its always best to put code into functions because it should simplify your code, and organize it better, it also helps with keeping things consistent and for better debugging. Just remember to make sure anything that the user inputs is the correct variable type!
Reply
#19
Zag,

Thank you so much for doing this! I just subscribed to this thread, as I too want to learn python and Kodi add-on development.

Regards,

Bart
Reply
#20
I don't want to sound too critical, but I'd recommend to the TC to read some Python best practices guides, starting with PEP-8, and use them in his examples. My personal opinion is that a beginner's guide should not include bad coding style (like superfluous or missing spaces that hamper code readability) or even serious errors, like in the last example where a built-in name list has been re-defined, which can potentially lead to unpredictable results.
BTW, I'd recommend to use free PyCharm Community or Educational editions for coding. This IDE has quite good code inspection that helps to avoid at least basic code errors (not syntax errors, that is obvious, but things like re-defining build-in or external names, observing function/method call signatures, type mismatch in expressions and such.
Reply
#21
(2015-08-04, 13:38)Roman_V_M Wrote: in the last example where a built-in name list has been re-defined

Was going to mention this too, but I expect OP will cover it when he gets to namespaces.

On the editor of choice, I use SublimeText and recommend it over PyCharm:

- PyCharm's internal debugger is normally really useful, but not in Kodi addon development (this is because the python modules xbmc, xbmcgui, xbmcaddon, xbmcvfs are not available)
- SublimeText has plugins that are built using python (rather than PyCharms Java plugins).
- phil65's SublimeText plugin: http://forum.kodi.tv/showthread.php?tid=221682

Of course, both editors are better than IDLE for the medium to long term. While learning, the immediate feedback of IDLE is very handy. But as soon as you start entering longer code segments (i.e. building classes, nested if statements) its restrictions become annoying. SublimeText allows you to edit your scripts and then press ctrl-b will build and run them.
Reply
#22
(2015-08-05, 03:58)Karnagious Wrote: - PyCharm's internal debugger is normally really useful, but not in Kodi addon development (this is because the python modules xbmc, xbmcgui, xbmcaddon, xbmcvfs are not available)

You might want to check this http://romanvm.github.io/xbmcstubs/
Reply
#23
(2015-08-05, 11:20)Razze Wrote:
(2015-08-05, 03:58)Karnagious Wrote: - PyCharm's internal debugger is normally really useful, but not in Kodi addon development (this is because the python modules xbmc, xbmcgui, xbmcaddon, xbmcvfs are not available)

You might want to check this http://romanvm.github.io/xbmcstubs/
Yep, knew about stubs.

From the link:
Code:
Warning: xbmcstubs are literally stubs and do not include any useful code, so don't try to run your program outside Kodi unless you add some testing code into xbmcstubs or use some mocking library to mock Kodi Pyhton API.

Doc strings and auto-completion don't make up for the "ImportError: No module named xbmc" roadblock.
Reply
#24
(2015-08-06, 02:28)Karnagious Wrote:
(2015-08-05, 11:20)Razze Wrote:
(2015-08-05, 03:58)Karnagious Wrote: - PyCharm's internal debugger is normally really useful, but not in Kodi addon development (this is because the python modules xbmc, xbmcgui, xbmcaddon, xbmcvfs are not available)

You might want to check this http://romanvm.github.io/xbmcstubs/
Yep, knew about stubs.

From the link:
Code:
Warning: xbmcstubs are literally stubs and do not include any useful code, so don't try to run your program outside Kodi unless you add some testing code into xbmcstubs or use some mocking library to mock Kodi Pyhton API.

Doc strings and auto-completion don't make up for the "ImportError: No module named xbmc" roadblock.

It's always a good idea to separate "Kodi" and "non-Kodi" parts of your addon into separate modules and test them separately. Also, unit-testing can help you to catch the most obvious bugs.

As for xbmcstubs, they are a great help for coding, but for standalone testing you need to add some mocking library in the mix. But I would suggest to move this discussion to a more appropriate topic: http://forum.kodi.tv/showthread.php?tid=173780
Reply
#25
hey Zag, are you coming back? I hope so
Reply
#26
Yep just been on Holiday and moving house recently Wink

Will continue my journey soon...
Reply
#27
Zag,
Hi. Thanks for these posts! Like you, I have some programming experience (C, C++, Labview) and would like to learn about python and create my own addon. It is really difficult for newbies to find the info needed to dive in. I really appreciate your time in creating this. I look forward to the rest of your posts. If you find any good links to helpful info, please share. Thanks for your time.

Big Grin
Reply
#28
Still awaiting day 6.. lol
Reply
#29
Still learning PHP, but I plan to start with some programming language such as Phyton really soon and can't wait. Great topic!
Reply
#30
I found this great blog post recently for beginners.

https://medium.freecodecamp.org/learning...0ea540b567
Image Image
Reply



Logout Mark Read Team Forum Stats Members Help
Learning Python and Kodi Add-on Development - A Journey..5
This forum uses Lukasz Tkacz MyBB addons.