Thoughts on Technology

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, 6 June 2013

Python - Sorting Lists inside of Lists

Posted on 07:30 by Unknown
In some recent python code I was playing with I had the need to sort a bunch of lists based on elements within their elements. Thankfully python's built in sort function is plenty powerful and allows us to do this in a fairly easy manner. So for example if we have a list all of whose elements are also lists:

mylist = [["derp", 1, 7], ["bleh", 2, 0], ["merp", 0, 3]]

By default when we call the sort function on this list it will sort the sublists based on their first element in lexicographic order. For example:

mylist.sort()
print mylist
[['bleh', 2, 0], ['derp', 1, 7], ['merp', 0, 3]]

What if we don't want to sort these lists based on their first element though? By using the key argument we can sort our sublists based on any element we want. For example to sort the lists based on their second element we would use:

mylist.sort(key=lambda e: e[1])
print mylist
[['merp', 0, 3], ['derp', 1, 7], ['bleh', 2, 0]]

Or their third element:

mylist.sort(key=lambda e: e[2])
print mylist
[['bleh', 2, 0], ['merp', 0, 3], ['derp', 1, 7]]

Special thanks to the folks over at #python on freenode for helping me figure this little bit out. They are an extremely resourceful bunch. You can learn more about working with python lists here.

Cheers,
~Jeff Hoogland
Read More
Posted in python, tutorial | No comments

Friday, 1 February 2013

Tutorial 2: ELM Images, File Selector and Popups

Posted on 14:22 by Unknown
This is the second post in my series on developing GUI applications in Elementary using Python. Today we are going to continue building on the Hello Elementary example I started in the first tutorial. In today's post I will only be covering the code that is different from our previous examples, so if you haven't looked that one over yet please take a moment to do so now.

You can find the full source code for all of today's examples here.

Example 3:
We are going to start off by displaying a static, pre-defined image in our GUI:


It only takes us 8 lines of actual code to create and display the above image in our program:

    #Creates an Image object that displays an image
ic = elementary.Image(window)

#Use the os module to get the current path to our .py file. Our image is relative to our .py We do this because it is best to use the absolute file path to images for the best results.
location = os.path.dirname(os.path.abspath(__file__))

#Tell our icon to auto-fill open space
ic.size_hint_weight_set(evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND)
ic.size_hint_align_set(evas.EVAS_HINT_FILL, evas.EVAS_HINT_FILL)

#Here we set the image we want our icon to display
ic.file_set("%s/images/logo.png"%location)

#Optional, lets add mouse over text to our image
ic.tooltip_text_set("Look a pretty picture!")

#Lets show our icon
ic.show()

box.pack_end(windytax)
#Pack our icon between our text and button
box.pack_end(ic)
box.pack_end(button)

In this example we utilize the elementary Image object to display our selected .png file.

Example 4:
Very rarely do we want to simply display a single image for as long as our program is running. So lets give the user the ability to change the image we display in our program:


Elementary has a built in FileselectorButton object that when clicked presents our user with a nice file selector GUI:


The new code to add this file selector button looks like:

    #Creates a "FileselectorButton" object. This is a button (just like we have created before) except that when it is click it automatically opens a file selector window
fsb = elementary.FileselectorButton(window)

#We can set the text of our fsb just like a normal button text
fsb.text = "Change Image"

#Tooltip for mouse over
fsb.tooltip_text_set("Click Me!")

#This tells our file selector window what to do when our user selects a file. The first argument is the callback function we want run and our second argument is our image object we want to change the display of
fsb.callback_file_chosen_add(change_image, ic)

#Show our button
fsb.show()

box.pack_end(windytax)
box.pack_end(ic)
#Pack our file selector button between our image and button
box.pack_end(fsb)
box.pack_end(button)

window.resize_object_add(box)

window.resize(300,300)

window.show()

#Our fileselector callback. The file argument is the fileselectbutton object. The second argument is the full path to the file that was selected. The final argument is the image object we passed to this callback
def change_image(fsb, file_selected, image):
#Check to make sure a file of some sort was selected. If nothing was selected file_selected will equal None type
if file_selected:
#These are the extensions we will allow our program to display
validExtensions = [".png", ".jpg", ".gif"]

#Use the os module to easily get the extension of our file
fileName, fileExtension = os.path.splitext(file_selected)

#If the extension is in our validExtenions lets check the image we are displaying!
if fileExtension in validExtensions:
image.file_set(file_selected)

Example 5:
Lets add one finishing touch to our application. If our user selects a file to display that doesn't have a valid image extension lets send them a popup telling them why the image displayed wasn't changed:


Showing a popup of this nature is fairly easy using elementary's Popup object. So the final edit to our code looks like this:

#This time we also pass the window object to our change image function. The reason for this is that our popup object needs a parent window object
def change_image(fsb, file_selected, image, window):
if file_selected:
validExtensions = [".png", ".jpg", ".gif"]

fileName, fileExtension = os.path.splitext(file_selected)

if fileExtension in validExtensions:
image.file_set(file_selected)
else:
#if we have an invalid extension lets give the user a popup message telling them why the image didn't change

#Create a popup message
popup = elementary.Popup(window)

#Set the title of our popup
popup.part_text_set("title,text", "Invalid File Extension")

#Set the text of our popup
popup.text = "File %s has an invalid file extension of %s"%(fileName, fileExtension)

#Create a button object
bt = elementary.Button(window)

#Set it's text
bt.text = "OK"

#Define a callback that is called when the button is clicked, lets pass our popup object to this call back so we can close the popup when the user presses OK
bt.callback_clicked_add(bnt_close, popup)

#Sets content for our popup. The first argument is an arbitrary name for the content piece and the second argument is the elementary object you would like displayed for the content
popup.part_content_set("button1", bt)

#Show the popup to our user
popup.show()

#The callback for our popup's OK button. The first agurment is the button object itself and the second object is the popup we passed to it
def bnt_close(bt, popup):
#Lets delete the popup so it goes away
popup.delete()

Hope everyone learned something today! Have any questions feel free to drop a comment below or start a discussion on our user boards.

Resources for this Lesson:
  • Displaying Images
  • Image
  • FileselectorButton
  • Popup 
~Jeff Hoogland
Read More
Posted in elementary, python, tutorial | No comments

Tuesday, 29 January 2013

Tutorial 1: Hello Elementary

Posted on 17:03 by Unknown
This post is the first in a series I am going to be publishing about using elementary and python to develop applications. The source code for all of the examples I am providing can be found in a GitHub repository here. Looking to get help with development? - We have started a programming focused section of the Bodhi Linux forums here. Other great resources for getting help are the Enlightenment devel mailing list as well as #e on freenode IRC. I've also added the python elementary API documentation to the Bodhi website here.

Example 1:
Since most people (myself included) learn best through examples, let's dive right into the code. To start, we are going to be creating a simple window that displays some text to us. It will look something like this:


Including my comments explaining what each line of code does, it takes us less than 50 lines of code to get the above window on our screen. Let's take a look (you can also find the source code for this lesson here):

#Import the elementary library so we can use it
import elementary

#Import evas, used for resizing things
import evas

#A function that creates and shows an elementary window
def hello_elementary():
#Creates a "Standard" elementary window. The first argument is the name of our window. The second argument is the title displayed on the window bar
window = elementary.StandardWindow("hello world", "Hello Elementary")

#callback_delete_request_add tells our window what to do when it's "close" button is pressed
window.callback_delete_request_add(lambda o: elementary.exit())

#Content for our window. Creates a "Label" object which display text in our window. Whenever we create an elementary object we must provide a parent window as input
windytax = elementary.Label(window)

#Tells our label object to change size based on the size of our window
windytax.size_hint_weight_set(evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND)
windytax.size_hint_align_set(evas.EVAS_HINT_FILL, evas.EVAS_HINT_FILL)

#Define what text our window should display
windytax.text = 'Hello Elementary!'

#If we want to see our object we need to tell it to be shown
windytax.show()

#resize_object_add adds our Label object "windytax" to the window
window.resize_object_add(windytax)

#resize takes an ordered pair as input for the size for our window, the dimenions are pixel by pixel
window.resize(300,300)

#Finally lets tell our window object to show up just like we did with our label
window.show()

#Runs when our script is run
if __name__ == "__main__":
#Runs our function which creates our window
hello_elementary()

#Starts an elementary event loop which displays all elementary objects we've created. Our code stays at this point until elementary.exit() is called
elementary.run()

#Once elementary is done running lets shut everything off to finish the application
elementary.shutdown()

In this example we create two elementary objects: A StandardWindow and a Label. The StandardWindow as you can guess is the window we are creating, while the Label is a child object that we add to our window to display.

Example 2:
We want our application to do much more than just display text (most of the time). So let's go ahead and add a couple more objects to our Hello Elementary application. Let's add a button that closes our application:


The full code for this application can be found here. I will now highlight what is different from our previous example.

def hello_elementary():
...

#Create an elementary button object
button = elementary.Button(window)

#Set some text for our button
button.text = "Goodbye Elementary"

#callback_pressed_add tells our button a callback to run when our button is pressed, the first argument is the function run and the following arguments are things to pass to the callback
button.callback_pressed_add(button_pressed, "argument1", "argument2")

#Show our button
button.show()

#Since we now have multiple objects we want to display on our window, we can position these objects using an elementary box which is a container object that you can "pack" items into.

#Create a box
box = elementary.Box(window)

#Tell our box to fill all open space in our window
box.size_hint_weight_set(evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND)
box.size_hint_align_set(evas.EVAS_HINT_FILL, evas.EVAS_HINT_FILL)

#Show our box
box.show()

#Lets pack our label and then button into our box!
box.pack_end(windytax)
box.pack_end(button)

#This time lets use our box instead of just our label
window.resize_object_add(box)

#Our callback when the button is pressed. The first argument for this function will be the elementary button object. The rest of the arguments are the custom things we passed above
def button_pressed(button, arg1, arg2):
#Show the content of our arguments in terminal
print arg1, arg2

#Lets have our button close the application, so run:
elementary.exit()

Example 2 adds two more elementary objects to our application - a Box and a Button. A Box is an elementary object that we use to hold other elementary objects to they are positioned how we want them inside our application window. You "pack" items into a box that is either vertical (default) or horizontal. A Button is an object that can have text and/or images displayed on it that can fire a callback when pressed.

Resources for this Lesson:
  • Hello Elementary
  • StandardWindow
  • Label
  • Box
  • Button
~Jeff Hoogland
Read More
Posted in elementary, python, tutorial | No comments

Tuesday, 1 January 2013

Introducing eAndora - Pandora Client

Posted on 14:32 by Unknown
A good deal of the work I do with the Bodhi project is packaging software/releases and managing things. One of my goals for this new year is to spend some time doing some actual development work with the Enlightenment Foundation Libraries and, more specifically, Elementary. I have a good bit of background with python programming, so my first leap into this field is writing a few small GUI applications in python and elementary.

I finally have enough work done on one of these projects that I feel OK sharing my work with the world. Today I would like to share with you a very, very early release of an application I am calling eAndora:


eAndora is a front end for Pandora Internet Radio. I've started a git repository here that I will be publishing updates to as I write them. The interface is still very basic, but the player is completely functional in the aspect that it allows you to browse and play your Pandora stations and skip tracks you don't like. In addition to being developed with Elementary, eAndora differs from projects like Pithos in the fact that it uses VLC as a streaming back end as opposed to the more common gstreamer.

Here are a few screen shots of the first draft of the GUI:

Login Screen:


Main Window:


Station Select:


I've packaged eAndora for Bodhi Linux and users can get it via a simple:

sudo apt-get install eandora

This package includes a launcher that is placed in your Audio menu.

Folks on other operating systems will need at least EFL 1.7.3, version 1.7.0 of the python EFL bindings and VLC 2.0.  Once you have all these installed simply clone the git repo and launch eAndora with:

python eAndora.py 

Please feel free to let me know any issues you find with eAndora by dropping a comment on this post. Do not however report a lack of functionality as an issue - there is still a good deal of work that needs to be done on this project - I just believe in sharing early and often.

Cheers,
~Jeff Hoogland
Read More
Posted in eandora, elementary, open source, python | No comments
Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • HOWTO: Watch Netflix on Bodhi Linux
    Not being able to utilize the Netflix video streaming service has been an issue on the Linux desktop for the past few years. This is due to...
  • Lenovo ThinkVision USB Monitor Review
    I picked up a new toy a couple of weeks ago - a secondary monitor for my multitude of computers. The screen I picked up though a slightly di...
  • HOWTO: Test E18, EFL 1.8.0, and Terminology 0.4.0 on Bodhi Linux
    As of this past weekend the testing builds of the Enlightenment window manager DR18 (E18 for short) are in the Bodhi Linux testing repositor...
  • HOWTO: Use Ubuntu Software Center in Mint 9
    There are a few reasons why I use Linux Mint instead of Ubuntu on my systems. That being said one of the reasons I do not list for using M...
  • Clarifying details about E17 and Compositing
    I just wanted to take a moment to clarify the state of compositing with the Enlightenment desktop. In two recent reviews of Bodhi Linux ...
  • HOWTO: Starcraft 2 on Linux with Wine
    Okie dokie - so I've mentioned before that I play Starcraft 2 under my Linux install with no issues. Since the game's official rel...
  • Bodhi Linux ARM Release Candidate for Genesi
    Edit/Update: You can find the latest release here ->  http://www.bodhilinux.com/downloads_mobile.php Early this year I posted about our ...
  • My Beef with the iPad
    Now I've written a couple of pieces about the iPad thus far and if you read through them you will see that the device has failed to imp...
  • Blackboard - No Linux for Online Education
    In addition to being a Linux Advocate and working 40+ hours a week I am also a full time student. Due to schedule constraints I often take ...
  • Steam Client and Source Games Porting to OSX
    So the devil called me this afternoon and said they where having a snowstorm in hell. In other news Valve announced that their revolutionar...

Categories

  • 3g modem
  • adobe
  • android
  • appeal
  • apple
  • arm
  • art
  • asus tablet
  • benchmark
  • bodhi
  • bordeaux
  • cedega
  • chakra
  • chrome os
  • chromebook
  • cockatrice
  • codeweavers
  • comic
  • cricket wireless
  • crysis
  • cxgames
  • debian
  • dell duo
  • diablo3
  • distro review
  • dtf
  • e18
  • eandora
  • eccess
  • elementary
  • elive
  • enlightenment
  • fedora
  • firefox
  • gaming
  • genesi
  • gnome
  • google
  • google chrome
  • google wave
  • handheld device
  • hardware
  • helios
  • howto
  • html5
  • ideapad
  • interview
  • ipad
  • jolicloud
  • kde
  • l4d2
  • laptops
  • lenovo
  • linux
  • lxde
  • macbook
  • math
  • maxima
  • media
  • meego
  • milestone
  • mint
  • mir
  • mk802
  • moblin
  • n900
  • netflix
  • nexus 7
  • nvidia
  • open pandora
  • open source
  • opengl
  • opera
  • operating systems
  • palm
  • phones
  • promotion
  • python
  • qt
  • rant
  • raspberry pi
  • reviews
  • sabayon
  • software
  • source games
  • spotlight
  • sprint
  • starcraft2
  • steam
  • t-mobile
  • tutorial
  • ubuntu
  • unigine
  • unity
  • wayland
  • web application
  • windows
  • windows 7
  • wine
  • wxmaxima
  • xfce

Blog Archive

  • ▼  2013 (20)
    • ▼  December (1)
      • HOWTO: Watch Netflix on Bodhi Linux
    • ►  November (1)
    • ►  September (1)
    • ►  June (1)
    • ►  May (2)
    • ►  March (2)
    • ►  February (3)
    • ►  January (9)
  • ►  2012 (57)
    • ►  December (5)
    • ►  November (4)
    • ►  October (2)
    • ►  September (1)
    • ►  August (4)
    • ►  July (9)
    • ►  June (4)
    • ►  May (4)
    • ►  April (1)
    • ►  March (7)
    • ►  February (6)
    • ►  January (10)
  • ►  2011 (107)
    • ►  December (8)
    • ►  November (8)
    • ►  October (5)
    • ►  September (14)
    • ►  August (9)
    • ►  July (8)
    • ►  June (7)
    • ►  May (10)
    • ►  April (9)
    • ►  March (13)
    • ►  February (9)
    • ►  January (7)
  • ►  2010 (122)
    • ►  December (10)
    • ►  November (8)
    • ►  October (10)
    • ►  September (14)
    • ►  August (17)
    • ►  July (10)
    • ►  June (9)
    • ►  May (14)
    • ►  April (8)
    • ►  March (7)
    • ►  February (7)
    • ►  January (8)
  • ►  2009 (27)
    • ►  December (10)
    • ►  November (7)
    • ►  October (10)
Powered by Blogger.

About Me

Unknown
View my complete profile