Saturday, 16 May 2015

Returning A Value From A Function To Be Used Outside The Function in Python 3

Functions are really useful for doing stuff for you in a program. But the hardest thing for me to work out was how to use what ever they did in the rest of my program.

Problem
Let's say I wanted to make a function to convert my string to lowercase. My program would look a little like this:

# Returns a string in lowercase
def Make_Lowercase(cmt):
cmt = cmt.lower()
comment = "My name is SCOTT."
print ("Commment without lowercase formatting:")
print (comment)
print("")
 
#Replace the old variable with the lowercase one created in the function
print ("Comment run through function to make it lowercase:")
print (cmt)
 Here I am trying to get the new comment, cmt, but when I run the program it yells at me with:
Traceback (most recent call last):
  File "C:/Users/Im/Batman/Python3/ReturnFunction.py", line 13, in <module>
    print(cmt)
NameError: name 'cmt' is not defined
Boo! I can't access the local variable inside the Make_Lowercase function.

Solution
To fix this, we need to return the variable inside the function and place the result back into the original (or a new variable. Up to you.) variable.

Here is an example:

Now we can use the results from the function outside of the function and in the rest of the program. Yay!


Removing Unwanted Whitespace in a String in Python 3

I have written a simple chatbot program in Python 3 where the user enters a comment and the bot responds. After offering it to family to play with I discovered that they would often put whitespace in front of the sentence or after. For example:

'  How are you?     '  or  'How are you? '
Problem:
The problem is that when I send this to the file to search for the match response. It comes up without a match because the string is saved as:
 'How are you?'
As you can see this has no spaces before or after. The single quotation marks. So when I run the search with it comes up with nothing and I don't really want to add unessessary lists with spaces just in case the program receives sloppy input.

Solution:
It turns our that the solution is quite simple. Python provides two simple tools to remove extra white space before and after the sentence or even duplicate spaces between words in a string.

In the first function, remove_whitespace_either_side, we use the variable.strip() method. This method essentially strips the white space from either side of the sentence in the string.

But what happens if your program user is a bit of a typing numpty and manages to put extra white spaces between the works? For example:
' I am     a typing                   numpty!'
As you can see, the first function cannot deal with this.

This is where the second function, remove_any_duplicated_white_space , is so useful. This function uses two methods forming " ".join(variable.spit()). Here, we split all the words into individual strings and then join them back together into one string with a single space between each.

You can see the result of each in the code above.

Looking at the quality of input I have been getting for my chatbot I think I will be using the second function.

Friday, 15 May 2015

Python 3 - Tkinter Scrolling with Inserted Text

I am using tkinter's ScrolledText to create a text box that I insert text in each line.

The problem
The problem was that when I inserted new text, the text box would not automatically scroll down to reveal the new text.

The solution
The solution turned out to be the yview method.

In the example below, I added while loop of 100 numbers(lines) to demonstrate that noew the text box is now focussed to the bottom of the text.

Tuesday, 2 December 2014

Russel Brand Slammed by Journalist Dina Rickman for Keeping Focussed on an Interview

Since Russell Brand developed a social and political conscience (or as least begun to share it with the public) I have been a big fan. 

He has recently been supporting the campaign to fight Westbrook Properties from massively increasing rent in order to force eviction of tenants at New Era Estate. 

In an video interview with Paraic O'Brien, Brand slams...no, that's not the right work...smashes... nope still not there...politely and respectfully...yes, much better... dresses down O'Brien for changing the direction of the discussion about the rent hikes and forced evictions of the residents of New Era Estate by asking Brand how much his home costs. 

FYI, Brand rents.

O'Brien continues to try and force some sensational confession out of Brand while, now, completely ignoring the more important issue regarding the people of the New Era Estate in Hackney.

Shitty reporting? Why yes. 

Have a look for yourself:


Enter Dina Rickman, in support of Brand and attacking O'Brein's dodgy reporting you may think. But no. She adds a new layer to the stupid with this headline:
Don't Ever Ask Russell Brand How Much His House Costs
...which he doesn't own.

Essential, in her few lines of reporting, she is attempting to lambaste Brand for putting O'Brein on point.  She was supporting O'Brein's bad reporting, which leads me to think that she does not know how to report.

I thought I would give her the benefit of the doubt and check out her other writing. Here are some of here recent highlights:

  • "Would You Want to be a Princess Nowadays?" (Oh wait, just a list of comments. No reporting going on here folks)
  • "I'm fat, so what? It's not an ugly word': Why the f-word might just finally be OK"
  • "We Need to Break The Public Hair Taboo"
Classy stuff.

I am beginning to think that we need to take a more active role in improving the media. So here is what I propose; copy and paste this message to Matthew Champion, the editor of i100 and let him know that Dina Rickman is not up to scratch:

TWITTER: @matthewchampion, I know you want a site we can trust, but @dinarickman has slipped through the cracks with this last @rustyrockets piece
Or your own creative twitter. I am sure Champion will listen, as he says:
Because i100 is from The Independent you can still trust us to take our facts very seriously (even the funny ones). Some of the stories will have been inspired by the brilliant work in the i or The Independent. Most will be from the crack team of i100 journalists.
Now, let's have a look at O'Brien. His editor, Ben de Pear, seems to be on his side with this ridiculous tweet:

@rustyrockets vocal on the rent row- but not on his own calling our @paraicobrien"snide" #NewEraEstate http://bit.ly/1FHK1sp #c4news at 7
 So perhaps even de Pear needs a little schooling on journalism. Let's hope this sinks in:

@bendepear, old @rustyrockets had to put your boy, @paraicobrien, back on topic about #NewEraEstate. You should be thanking him.
 What it all comes down to is a sub culture of shitty reporting. These are the people we rely on to get information about what is going on in our world and they are letting us down. That is why many of us are going to the internet for our news for better or worse. Perhaps we need to look at our news through the lens of sites like the Trews.

Thursday, 27 November 2014

Why are we seeing more and more protests in the western world?

We are seeing more and more protests in the western world. But why?

I don't have all the answers but I think its stems from a number of things.

First, I think the momentum got under-way as a result of the Arab Spring and Occupy movement. The protests seem to be a result of the continued frustration over the voice of the people being ignored by those in power, coupled with the continued strain on the common people via prohibitive laws or laws that breed inequality. This inequality can be between, class, races, religious practices or even between people and groups like large multinational corporations.

In recent years, we have also seen greater restrictions and management of protests. For example, you now have to get a permit to protest and you are cordoned into specific areas, usually to hide you from view of the people you wish to see your cause. Police create huge strategic plans to manage protests.

The strange thing is that this cordon and control by an outside source is completely contrary to the nature of a protest. A protest is designed to be a regaining of control by the protestees (new word). It is should be the responsibility of the protest groups to organise as best it can to get its point across. The very act of controlling a protest it the first aggressive act against the protest. Even before it hits the ground.

This control causes greater frustration by the protest group which causes them to push their cause even harder.

Secondly, we are are at a stage in our existence on Earth where our natural non-renewable resources are in rapid decline. Those that are in power are grasping for the last scraps and they will do anything to get them, like manipulate and corrupt governments. This is nothing unusual. We have seen resource declines throughout history with similar tragic effects until an alternative is found of the situation implodes.

I don't know if this is avoidable, but we have fostered a culture and political system where those who seek to gain more than others are rewarded. Once they have more they do everything in their power to stop others from gaining more and catching up. Even to the point where they will sacrifice some of their gains to prevent others from moving up. This is the very core of corruption.

This may be an effective tool in the past where you are competing for finite resources for your survival but as we are now forced to see each other less by our small groups and tribes and more as a collective member of Earth this strategy fails.

Communism was our first attempt at collective sharing but it failed to understand the highly and irrationally competitive nature of many people. This quickly led to corruption. Democracy, on the other hand understood that some people are innately corrupt and buffered for it better.

The thing is, Democracy can only buffer for so long until corruption takes hold and if Democracy starts with corruption, it is essentially a loosely hidden dictatorship of the corrupt elite and is destined to fail from the start.

What we are seeing in the Democratic world now is that the buffer has worn through and the corrupt elite are taking hold.

We are now railing against this because we see where this will lead. Thus we protest. The corrupt-elite know what's coming so they militarized the police force. They try and control protest before it even starts. They block and control the media through corporate monopolies and attempt to reign in the free flow of information (net-neutrality). And they seek to pry on our everyday lives (internet surveilance).

We see the inequality rise in our countries and know that we need to stand against it before the corruptions takes hold.

We are now understanding that we are all brothers and sisters of Earth and we seek to take responsibly for our planet and each other but are barred from doing so by the very nature of capitalistic democracy and corrupt quasi-democratic dictatorships.

Global society is in dire need of an audit. A revolution. These protests are the first salvo of this revolution and those corrupted and in power know this and are fearful. It is time for the pendulum to swing in the other direction.


... the alteration of men on a mass scale is, necessary, ... a revolution; this revolution is necessary, therefore, not only because the ruling class cannot be overthrown in any other way, but also because the class overthrowing it can only in a revolution succeed in ridding itself of all the muck of ages and become fitted to found society anew.
Karl Marx
Let's hope we can do it peacefully.

Friday, 31 October 2014

Pay Inequality and Inequity Aversion

I was reading something on Inequity Aversion, specifically the Ultimatum Game. Basically, It is played between two players where one person gets a sum of money and must decide how that money is divided between the two. If the other player is unhappy with the division then they can veto the decision and no player gets any of the money. The average minimum accepted amount that was generally not vetoed was that the holder of the money keeps 80% and the other player keeps 20%.

I was thinking about this and the pay divide and thought that maybe a good first step for governments might be to mandate that the highest paid member of the company can only earn, as a minimum, 80% more than the lowest paid member (of course this would also include the sum total of share options and other perks).

For example, if a CEO earn's $1,000,000 per year in their company then the lowest paid worker would earn $200,000 per year. If the CEO wishes to earn more, say $1,500,000, then the whole organisation would have to work to ensure that lowest paid worker earned $300,000 per year.

This can equally be worked out in terms of hourly wage. Let's say our CEO works 50hrs a week giving him a $1,000,000 salary for the year her hourly rate would be:

     Hourly rate of highest paid worker = Annual Salary/(weekly hrs x 52 weeks in a year)

     Hourly rate of highest paid worker = 1,000,000/(50/52)

                           1,000,000/2600
                           $385 per hour

    Hourly rate of lowest paid worker =

                           Hourly rate of highest paid worker - (hourly rate of highest paid worker x 80%)
                           $385 - (385 x 80%)
                           $385 - (308)
                           $77 per hour


If the highest paid members wish to earn more then they had to work with all members of the company to increase the minimum wage of the lowest paid member. This would also incentivise all members of the organisation to seek to improve the business. Just a thought I am chewing on at the moment.

I think that when management are incentivised in this way they would also be willing to be more open about he goals of their company with all its members so they can work together to move forward. 


Saturday, 26 July 2014

Science Education and T-shrits

I have been thinking about science education for a while and I really like the idea of combining science education and art. As a bit of an experiment I thought I would design a few t-shirts with some science themes.

My idea is that shirt designs just sort of get science out there in a beautiful way. I love to see what shirts people are wearing on my commute or out shopping. I also think that science is truly beautiful and it is worth sharing this beauty with others.

So putting this all together I decided to start a little science based clothing company called Nerdlish. You know, sharing the language of the nerd. So at the moment here is the link:

NERDLISH

I'll talk more on this later.