Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Learning programming with Python, part V

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

[Tutorial] Learning programming with Python, part V

Post by Acid_Snake » Sat Jun 02, 2012 4:47 pm

Part V - Some Modules
As promised I'm going to let you guys in to some very useful modules built-in with Python.
Importing (loading) a module:
You should know this by now, but in case you don't:

Code: Select all

import name_of_the_module
You can use variables to refer to a module, but it has to be declared on module load:

Code: Select all

import this_module as this_var
I will only explain some basic and useful parts for some modules, not everything. For more help on modules, use:

Code: Select all

import whatever_module
help(whatever_module)
os:
The os module provides a set of OS-based commands, mostly file operations (remove, copy, chown, chmod....). Some examples:
Change directory:

Code: Select all

os.chdir(path)
so to navigate to the root:

Code: Select all

os.chdir("/")   # in Linux
os.chdir("c:\")   # in Windows
Change access permission:

Code: Select all

os.chmod(path, mode)
# path must be a string, mode must be an integer
Change owner/group

Code: Select all

os.chown(path, uid, gid)
There are many more, but more interesting is os.system(). os.system is used to pass commands to the system:

Code: Select all

os.system("uname -a")
os.system("ls -a")
os.system("sudo apt-get install ubuntu-desktop")
os.system("vlc")
if the commands are executed and exited correctly it will return a 0, any other number means an error:

Code: Select all

if os.system("apt-get -v") != 0:
  print "You don't have apt-get installed"
elif os.system("apt-get -v") == 0:
  print "You have apt-get installed"
random:
The random module is used to do things that involve random choices or generators, it can be used to create random numbers/strings or randomly select from a list.
Select from a list:

Code: Select all

x = [1,2,3,4,5,6,7,8,9,0]
random.choice(x)
Generate a random number:

Code: Select all

int("".join(random.sample("1234567890",10)))
10 is the length, so it can be any number depending on the number of integers you want.
Generate a random string:

Code: Select all

"".join(random.sample("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890",10))
Generate a random number within a range:

Code: Select all

random.randrange(10)
This will generate a number from 0 to 10.

zipfile:
A module for loading, extracting and creating .zip files.
Creating a file:

Code: Select all

zipf = zipfile.ZipFile(path_to_file, "w")
Opening a zip file:

Code: Select all

zipf = zipfile.ZipFile(path_to_file, "r")
Adding a file to the zip file:

Code: Select all

x = "name of the file to be stored"
zipf.writestr(x, open(x, "rb").read())
Extracting everything:

Code: Select all

y = "Path to extract everything"
zipf.extractall(y)
Note: extractall() is available from python 2.6 and up
Example:
We're going to create a file named test.txt, write something to that file, store it in a zip file named test.zip and extract it in a folder test:

Code: Select all

import os, zipfile
x = open("test.txt","w")
x.write("test")
x.close()
zipf = zipfile.ZipFile("test.zip", "w")
zipf.writestr("test.txt", open("test.txt", "rb").read())
os.mkdir("test")
zipf.extractall("test")
urllib2:
Mostly a module for conecting to a URL, it can be used to download a file:

Code: Select all

url = "www.some.url.of.a.file.com"
dl = urllib2.urlopen(url)
dl_file = open("whatever_file","w")
dl_file.write(dl.read())
dl_file.close()
shutil:
A very little used yet powerful module, it provides a wrapper for some os functions to work on files an folders, some functions:
shutil.copyfile(source, destiny) --> copy a file from source to destiny
shutil.copytree(source, destiny) --> recursively copy a folder from source to destiny
shutil.move(source, destiny) --> recursively move a directory/file from source to destiny
shutil.rmtree(directory) --> (recursively) remove a directory

sys:
This module includes functions that interact with the Python interpreter itself, normally you don't need to use it, except the function sys.exit() which will terminate your app:

Code: Select all

sys.exit("exiting...")
Tkinter:
Tkinter is the default GUI toolkit with Python, it doesn't have much in terms of widgets, but it can be useful for quick and simple GUIs. I will explain this module more in-depth later.

That's all for now.

Previous: viewtopic.php?f=37&t=11904
Next: viewtopic.php?f=37&t=13056
Advertising
Last edited by Acid_Snake on Sun Aug 12, 2012 2:39 pm, edited 1 time in total.

User avatar
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [Tutorial] Learning programming with Python, part V

Post by qwikrazor87 » Sat Aug 11, 2012 9:55 am

Good job with the tutorials. :)
I hope you have time to make more. I'm thinking about getting into Python, it has some interesting functions that I could make use of. Thanks.
Advertising
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS

User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python, part V

Post by Acid_Snake » Sun Aug 12, 2012 2:42 pm

I just made a tutorial for GUI programming with Tkinter, you can find it here: viewtopic.php?f=37&t=13056
If you are looking for something more specific just ask me and I might add it to the tutorials.

User avatar
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [Tutorial] Learning programming with Python, part V

Post by qwikrazor87 » Sun Aug 12, 2012 9:10 pm

Thank you. :) It'd be awesome if you made tutorials aimed toward coding for the PSP.
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS

User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Learning programming with Python, part V

Post by Acid_Snake » Sun Aug 12, 2012 9:32 pm

qwikrazor87 wrote:Thank you. :) It'd be awesome if you made tutorials aimed toward coding for the PSP.
I had that in mind too! I'll use example codes from some of my homebrews.

User avatar
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [Tutorial] Learning programming with Python, part V

Post by qwikrazor87 » Sun Aug 12, 2012 9:40 pm

Thanks a lot. I'll be looking forward to it. :)
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS

Post Reply

Return to “Programming and Security”