Wednesday, March 30, 2016

Quick Tip: Understanding the Yield Keyword in Python

When we call a function in Python, the function will normally start working until it encounters a return, exception, or reaches its end – after which it returns control back to the caller. Whenever you call that function again, the process will start from scratch!

Say that you asked a person to track the red cars on the road. The person will keep getting a question asking them if they spotted a red car or not, and the person in turn would answer with either ‘yes’ or ‘no’. If the person answered ‘yes’, the number of times the red car was spotted will increase.

Let’s see how we can do this in Python:

import time

def red_cars(answer):
    n = 0
    while True:
        if answer == 'yes':
            n = n + 1
            return n
        else:
            return n

stop = time.time() + 5 * 60
while time.time() < stop:
    answer = raw_input('Did you spot a red car on the road? ("yes" or "no"): ')
    times = red_cars(answer)
    print 'You have spotted ' + str(times) + ' cars so far!'

If you run the program, what do you notice? Did you notice that the number of times for the ‘yes’ answer is always capped at 1, and when you answer ‘no’ the number of times gets 0 regardless of answering ‘yes’ before?

Here is where Python’s yield keyword comes into play. yield is a means by which we temporarily hand control to the caller, and expect to continue from the point at which control has been handed over.

Before giving the solution to the above example, let me demonstrate a very simple example to better illustrate how yield works.

Say we have the following simple Python script:

def step_by_step():
    return 'step 1'
    return 'step 2'
    return 'step 3'
    
step = step_by_step()
for i in range (3):
    print step

If you run the script, you will get the following output:

step 1
step 1
step 1

Now, if we use yield instead, as follows:

def step_by_step():
    yield 'step 1'
    yield 'step 2'
    yield 'step 3'
    
step = step_by_step()
for i in range (3):
    print step.next()

The output would be as follows:


step 1
step 2
step 3

As you can see, we were able to create a series of values, as for each call the function continues from the point where it yields a value. This type of function is called a generator. Such function creates a generator iterator, as with each call to the method next() we move to the next yield statement.

If we come back to our main example (red cars), it can be written as follows to perform the required task:

import time

def red_cars(answer = None):
    n = 0
    while True:
        if answer=="yes":
            n = n + 1
            answer = yield n
        else:
            answer = yield n

car_color = red_cars()
car_color.next()

stop = time.time() + 5 * 60
while time.time() < stop:
    answer = raw_input('Did you spot a red car on the road? ("yes" or "no"): ')
    print 'You have spotted ' + str(car_color.send(answer)) + ' cars so far!'

Thus, as we can see, yield is deemed important when we are interested in resuming execution at the last point where the function (generator) exited, and where we are also interested in keeping the values of local variables between the different calls – unlike normal functions, where such values are destroyed when exiting the function.

Continue reading %Quick Tip: Understanding the Yield Keyword in Python%


by A. Hasan via SitePoint

4 Things I Wish I’d Been Told at the Start of My Freelance Career

I’ve been freelancing for around 5 years now... full time for the last 3. I enjoy the freedom, the high pay I’ve been able to achieve, and perhaps most importantly, the diversity of activities.

[author_more]

Of course, you can’t do something for 5 years without learning a few things, so I wanted to take a moment today to share a few of the things I wish I’d known back at the beginning. I’ll also mention a few things I managed to avoid that seem to be common tripping points for many freelancers.

I hope that’s enough intro for you, because we’re diving right in.

1. Pricing Has Nothing to Do with Service Quality

A lot of people tend to think that price is directly related to quality. Sure, you know of some exceptions, but on average, people to tend to earn what they’re worth, right?

No. Not with freelancing. In fact, you need to completely get out of that mentality if you want to make real money as a freelancer.

The reality of capitalism is that pricing has nothing to do with quality. Pricing is 100% determined by what people are willing to pay. As a freelancer, my service cost is based entirely on what I can negotiate from a client as opposed to the actual value of my work.

If you want to make more money as a freelancer, you don’t need a better service, you need better negotiating skills. Here’s the cliffnotes for making that happen:

How to Negotiate Higher Pay

Quality is important, of course, and we’ll talk about that next, but it has virtually nothing to do with your pricing. I’ve made $12 and $800 for essentially the exact same article. If you can change your mentality and understand that price is a matter of how much your client is willing to pay, you can immediately begin making more money for the exact same amount of work.

2. Your Long-term Success Will Depend Entirely on Quality

While the quality of your work will not determine how much you earn on any given project, it WILL determine how many projects you get from repeat and referred clients, which tend to make up the bulk of a successful freelancer’s income.

Don’t let this detract from my previous point. You can do $800 quality work and get paid $12 the rest of your life if you fail to grasp point #1.

If, however, you are successfully charging $800 for $12 quality work, you will typically find yourself failing to attract recurring business. There are always exceptions, of course, and if you are in a market where clients tend to have limited understanding of the service provided, you might be able to get away with scamming. But honestly, if your goal is simply to rob people, becoming a freelance service provider is a horribly inefficient way to accomplish that.

The best way to succeed over the long haul is to provide consistent value to everyone you work with. When a client comes away thrilled with your work, there’s a fantastic chance that will turn into additional work and a referral or two down the road. When this happens with EVERY client you work with, you are essentially building a passive lead-generation network.

Focus on doing quality work and continue to improve as your career progresses. This is THE best way to achieve long-term success as a freelancer.

Continue reading %4 Things I Wish I’d Been Told at the Start of My Freelance Career%


by Jacob McMillen via SitePoint

A Beginner’s Guide to npm — the Node Package Manager

TouchSlider – Javascript Component to Create Sliding Effect

TouchSlider is a lightweight javascript component to create sliding effect for mobile and PC.


by via jQuery-Plugins.net RSS Feed

Competition Davorin Jenko

International Music Competition Davorin Jenko official website


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

HappyCulture

Discover the new French hotel group HappyCulture. Explore their exceptional offer through an endless inspirational grid.


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

THE UX JOURNAL by Rokivo

Ideas, articles, books, links and tips find by the Rokivo UX team shared with you.


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery