Set Comprehension in Python

In a previous article I discussed list comprehensions. Today I will talk about set comprehensions. Set comprehensions were added to Python in version 2.7 and they are similir to List Comprehensions, the only difference is that we use curly braces instead of square brackets when making them.

A set is an un-ordered collection in which each element can only appear once. Below is an example of a set of the squares of all numbers between 0 and 10:

>>> nums = {x**2 for x in range(10)}
>>> nums
set([0, 1, 4, 81, 64, 9, 16, 49, 25, 36])
>>> 
By TerraMeijar Posted in Python

Python List Comprehensions

A special syntax form in the Python language is the list comprehension. List comprehensions allow programmers to make their code more compact and (sometimes) harder to read if they push the comprehensions too far! The syntax consists of an expression in square brackets to create a new list. In order to show the beauty of the list comprehension, I will present a solution (using list comprehensions) to a common problem: iterating through a list of items and then creating a second list that contains data from the first list that satisfies a particular condition.

Suppose we have a list of numbers that range from 0 up to 20. We want to create another list from this range that only contains the numbers that are multiples of 2. The code to do this would normally look like this:

>>> numbers = range(0,21)
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

>>> b = [] # We create an empty list that will hold the multiples of 2
>>> for number in numbers:
        if number % 2 == 0:
            b.append(number)
        else:
            continue


>>> b
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

The code uses to for loop to iterate through each number in the numbers list and an if statement to test if the number is a multiple of 2. If it is, the number is appended to the b list. Let’s do the same thing using a list comprehension:


>>> numbers = range(0, 21)
>>> b = [number for number in numbers if number % 2 == 0]

>>> b
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


Wow! In the second example, we managed to code a solution to the problem on only one line. The result is created in a single step. This syntax is a looks a little foreign at first so let’s break it down.
The statement on line 2 creates a list, which is explains the square brackets. The “number” variable is what we want to return after each iteration of the loop on the right. The “for” loop goes through each number in numbers and checks if each number is a multiple of 2 using the modulus operand. If “number” is a multiple of 2, it is appended to “b”

The first and middle parts of the list comprehension are straight forward, but the last part, the if, is odd, I know. The only way to get this is to commit a few brain cells to remembering how that works.

The next blog post I will write will be on the Set Comprehension.

The python lambda

Recently I learned how to use the lambda function, a simple, unbound one liner function. A lambda is similar to a normal Python function, the only difference is in the way it is used. The example below will explain better:


def poww(x):
    return x ** 2

>>> poww(4)
16

The poww function defined above takes an argument x and squares it. So if we were to call the poww function and pass in the value 4 to it, the return value would be 16.

OK, good and well, let’s try this using lambda:


(lambda i: i ** 2)(4)
16

The lambda looks odd, but it is no different from the function we defined above, the “i” in the lambda function represents the argument you pass in and the part on the right of the “:” is what gets returned.

The lambda is ideal for when you need a function that will do something once and be discarded.

Using TelOne? Read this to learn how to save data and secure your network.

TelOne is Zimbabwe’s largest ADSL Internet Service Provider and their Internet packages are affordable and meet most people’s basic Internet needs. A complaint that I’ve come across many times on the TelOne facebook page and from talking to users of the TelOne packages is that the data bundles they offer seem to get used up before their expiry date.

There are a number of reasons for this, the first being that customers are usually ignorant of how much data their devices actually use even when they are not browsing.This can be a lot of data because computers, smartphones and tablets tend to automatically download and update their software in the background without your knowledge.

Secondly there have been glitches and security holes in the TelOne billing system in the past that either left some subscribers without any data bundles or for the lucky few, with unlimited Internet access. As far as I can tell, TelOne has patched these holes up and even improved their service offering.

Now it’s up to the home user to keep an eye on their usage and save money. In this post, I will show you how to use a service called OpenDNS to 1. give you a more stable Internet experience, 2.allow you to restrict access to some websites, protect your network and save on data.

OpenDNS is a company that provides DNS servers and content filters for small to large networks.
To use the OpenDNS service, you need to sign up for an OpenDNS account first and add your home network to your dashboard. This basically means telling OpenDNS what your IP address is.TelOne IP addresses are dynamic and always changing, so you need to download a piece of software provided by OpenDNS that will keep their servers updated with your current IP address. Doing this is necessary to enjoy the full benefits of using the system. Once you have created an account and installed the IP Updater software, you need to update the DNS settings in your router to use the OpenDNS servers which are 208.67.222.222 and 208.67.220.220.
Check the documentation that came with your router for instructions on how to do it for your home router.Updating the DNS settings will make all the devices in your network use OpenDNS servers as opposed to connecting to your service provider’s DNS.

A DNS server, put simply is a server whose core function is to locate the web addresses of the websites you visit. All Internet Service Providers(TelOne included) make you connect to their DNS servers by default. These servers are not always reliable as they may go down or be slow and this affects your Internet experience. OpenDNS have made a reputation out of offering a robust DNS service and best of all, it’s free for home users.

The immediate result of changing your DNS to that of OpenDNS is that you may find the lag on your connection dropping, meaning less time spent waiting for websites to load. Less time spent waiting for pages to load makes your connection appear to be faster. Fast is good.

Filtering Websites

To get the most out of this service, you need to setup a content filter. A content filter will allow you to choose what websites can be accessed within your network. Websites can be blocked individually by specifying the website address or by the website category. Blocking entire website categories is easy, say for instance you wanted to block pornography, adult-themed websites, social networks and general time wasters you could do so from the dashboard with only a few clicks.

Whenever a device in your network tries to access a blocked website, OpenDNS will not allow the connection and the website isn’t loaded.

Being able to filter which websites can be accessed in your network means you’re in a better position to limit access to websites that are either inappropriate or data-heavy(e.g Filesharing websites and social media sites like Youtube and Facebook). The great part about all this is that its free, simple and you only have to set it up once and forget about it.

zimclix

How I became an entrepreneur.

For a very long time I was bothered by how business in my city don’t have an online presence except for a Facebook page and a directory listing. That was until I decided to do something about it, so last year in May I started a ZimClix, a business designed to provide consultation to small to medium sized companies to get them online. Having presence on the Internet is important because we live in a world where the majority of people are online in one way or the other for most of their waking hours. Small businesses need to take advantage of this to have a chance at success.

I do not have a business background and my web development skills could definitely be better, but I didn’t let that stop me from helping business owners who could benefit greatly from my skills. In May last year I decided to jump in and learn everything on the fly. The experience of running a business from my home office has taught me a lot about myself and I’ve been forced to learn valuable things that only experience can teach you. Starting out, I had to wear many hats in the company: director, developer, accounts manager,sales and customer support representative and the cook sometimes… all in one. I have experience working in Technical roles like administration and customer support so doing this comes easy for me. Sales, marketing and design..not so much, I keep learning and improving everyday.

One thing I get a lot of from developers especially, is that my prices are too high for what I offer. I’ll admit, I cannot compare to the bigger companies out there on price points, BUT I offer something that they don’t, that is personal, customer-centric support. My relationshop with clients doesn’t stop when they pay me or when the contract expires. I won’t do something unless I know the client will be happy at the end of the day. This work isn’t a chore because I love building web sites, helping people and most recently, working with businesses to increase their visibility and sales. Don’t just take my word for it, see what some of the people I’ve worked with have to say.

Recently, we managed to Integrate our billing system with a local payment gateway, so this means it’s now possible to order our products online and pay using Mastercard, Visa, EcoCash, TeleCash and ZimSwitch. This was done to make it as easy as possible for clients to pay for their invoices and also add funds to their accounts.

I’m excited about what the future holds for my business!

If you’re interested in having a website developed or need hosting, feel free to visit the website at https://www.zimclix.co.zw/ or to get in touch with me directly.

IMG_20160415_133314

Meeting with the NUST FSAs

A long overdue post.
Two weeks ago I was invited by Zibusiso to the National University of Science and Technology to talk to the Firefox Student Ambassadors club he has been working hard to get off the ground. As this was the club’s first meting, we discussed Mozilla’s goals for the year, our mission and a plan to pull off the Campus Campaign. Pictures below:
IMG_20160415_133314

IMG_20160415_133321

IMG_20160415_133325

Effort is between you and you – Ray Lewis

This is a great video, I’m adding it here to motivate me when I need it.

Redyawning


Ray Lewis gave this speech to the Stanford basketball team before their NIT finals. Stanford went on to win that game as underdogs. When I get lost during the shuffle of everyday life I go back to this video. I’m posting it for myself so I can have it at arms reach. I hope this finds someone else and effects them as much it has effected me. I’ll always go back to that line… Effort is between you and you. I try and remind myself of this every morning. It’s about how well you do all the little things in life that make up a greatness. Thanks for the inspiration Ray Lewis.

Transcript:

“If tomorrow wasn’t promised – what would you give for today? Forget everything else.

Forget everything else. Forget that there was any sunlight left. What would you spend today…thinking about? Yourself? Or the man that’s beside you?…

View original post 203 more words

AfricaCom 2014

I had the opportunity to man the Mozilla Booth at Africacom 2014 with fellow Mozillian Oarabile Mudongo.

This was the largest Tech show I’d ever attended. Our job was to demo Firefox OS phones made by Alcatel and to get people excited before the launch. Africacom was hosted in the beautiful city of Cape Town in South Africa. There were more than 6000 attendees on each of the days.

The show was held over three days at the Cape Town International Convention Center.
Show video: https://www.youtube.com/watch?v=jK0mZIK9YFw

Pictures below

africacom-google

africacom-stage

africacom-phones

africacom-vu-brian

africacom3

africacom2

africacom

IRC Cloud: I love this client

For those of you who don’t already know, I am not a big fan of Corporate Culture and I have done my best to avoid working in a a strict shirt and tie corporate environment.

Currently I work as a contractor for a company in the US (https://www.getfoxyproxy.org), I work on providing clients with Customer and Tech support. I work with people from all over the world, Asia, US, Middle East etc Because of this, we have systems in place that make it easy for a globally distributed workforce to work together and collaborate on projects easily.

Our virtual ‘office’ is an IRC channel hosted on Mozilla’s IRC network. This is where we talk shop and joke around and sometimes share memes. Now my job requires that I use Virtual Machines, different browser and different Operating Systems. Switching back and forth between the different browsers and IRC client wasn’t fun for me, so I started using IRCCloud (https://www.irccloud.com/).

Why Do I like IRC Cloud?

1. IRC Cloud is a web based IRC client, which means that there’s no need to download the right version for each different platform I use.

2. IRCCloud keeps me online all the time. With IRCCloud, i’m always online, even when my computer is off. That way, I never miss a message.

3. It runs in the browser. Any modern browser can run IRCCloud and it supports web notifications, which means that if someone sends me a PM or mentions me in a comment, I’ll know, even if i’m viewing a different tab.

4. It works on mobile. There’s an app for the iOS and Android I think, so if you never miss a thing.

There you have it, IRCCloud is what I use and I encourage you to do the same.