Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

An iterator is just a fancy sounding term for an object that has a next() method. So a yield-ed function ends up being something like this:

Original version:
***
def some_function():
for i in xrange(4):
yield i

for i in some_function():
print i
***
This is basically what the Python interpreter does with the above code:
***
class it:
def __init__(self):
# Start at -1 so that we get 0 when we add 1 below.
self.count = -1

# The __iter__ method will be called once by the 'for' loop.
# The rest of the magic happens on the object returned by this method.
# In this case it is the object itself.
def __iter__(self):
return self

# The next method will be called repeatedly by the 'for' loop
# until it raises StopIteration.
def next(self):
self.count += 1
if self.count < 4:
return self.count
else:
# A StopIteration exception is raised
# to signal that the iterator is done.
# This is caught implicitly by the 'for' loop.
raise StopIteration

def some_func():
return it()

for i in some_func():
print i
***
For more insight as to what's happening behind the scenes, the for loop can be rewritten to this:
***
iterator = some_func()
try:
while 1:
print iterator.next()
except StopIteration:
pass
***
Does that make more sense or just confuse you more? :)

I should note that this is an oversimplification for illustrative purposes. :)
3 years ago
The simplest explanation for the __name__ variable (imho) is the following:

Create the following files.
***
# a.py
import b
***
and
***
# b.py
print "Hello World from %s!" % __name__

if __name__ == '__main__':
print "Hello World again from %s!" % __name__
***
Running them will get you this output:
***
$ python a.py
Hello World from b!
***
As you can see, when a module is imported, Python sets globals()['__name__'] in this module to the module's name. Also, upon import all the code in the module is being run. As the if statement evaluates to False this part is not executed.
***
$ python b.py
Hello World from __main__!
Hello World again from __main__!
***
As you can see, when a file is executed, Python sets globals()['__name__'] in this file to "__main__". This time, the if statement evaluates to True and is being run.
3 years ago
To remove a local branch from your machine:
***
git branch -d {the_local_branch} (use -D instead to force deleting the branch without checking merged status)
***
To remove a remote branch from the server:
***
git push origin --delete {the_remote_branch}
***
3 years ago
Here's an example of what they can be used for. In a testing framework I wrote, I wanted to keep track of the order in which classes were defined, so that I could later instantiate them in this order. I found it easiest to do this using a metaclass.
***
class MyMeta(type):

counter = 0

def __init__(cls, name, bases, dic):
type.__init__(cls, name, bases, dic)
cls._order = MyMeta.counter
MyMeta.counter += 1

class MyType(object): # Python 2
__metaclass__ = MyMeta

class MyType(metaclass=MyMeta): # Python 3
pass
Anything that's a subclass of MyType then gets a class attribute _order that records the order in which the classes were defined.
***
3 years ago
??! is a trigraph that translates to |. So it says:
***
!ErrorHasOccured() || HandleError();
***
which, due to short circuiting, is equivalent to:
***
if (ErrorHasOccured())
HandleError();
***
3 years ago
O(log N) basically means time goes up linearly while the n goes up exponentially. So if it takes 1 second to compute 10 elements, it will take 2 seconds to compute 100 elements, 3 seconds to compute 1000 elements, and so on.

?It is O(log n) when we do divide and conquer type of algorithms e.g binary search. Another example is quick sort where each time we divide the array into two parts and each time it takes O(N) time to find a pivot element. Hence it N O(log N)
3 years ago
According to my knowledge :
File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).

Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1 may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named 1". & indicates that what follows and precedes is a file descriptor and not a filename. So the construct becomes: 2>&1.

Consider >& as redirect merger operator.
3 years ago
you could try:
***
function clone(obj) {
if (obj === null || typeof (obj) !== 'object' || 'isActiveClone' in obj)
return obj;

if (obj instanceof Date)
var temp = new obj.constructor(); //or new Date(obj);
else
var temp = obj.constructor();

for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
obj['isActiveClone'] = null;
temp[key] = clone(obj[key]);
delete obj['isActiveClone'];
}
}
return temp;
}
***
3 years ago
The Sass .sass file is visually different from .scss file, e.g.

Example.sass - sass is the older syntax
***
$color: red

=my-border($color)
border: 1px solid $color

body
background: $color
+my-border(green)
***
Example.scss - sassy css is the new syntax as of Sass 3
***
$color: red;

@mixin my-border($color) {
border: 1px solid $color;
}

body {
background: $color;
@include my-border(green);
}
***
Any valid CSS document can be converted to Sassy CSS (SCSS) simply by changing the extension from .css to .scss.
3 years ago
The main point is that:

Abstract is object oriented. It offers the basic data an 'object' should have and/or functions it should be able to do. It is concerned with the object's basic characteristics: what it has and what it can do. Hence objects which inherit from the same abstract class share the basic characteristics (generalization).

Interface is functionality oriented. It defines functionalities an object should have. Regardless what object it is, as long as it can do these functionalities, which are defined in the interface, it's fine. It ignores everything else. An object/class can contain several (groups of) functionalities; hence it is possible for a class to implement multiple interfaces.
3 years ago
In the simplest terms, git pull does a git fetch followed by a git merge.

You can do a git fetch at any time to update your remote-tracking branches under refs/remotes//. This operation never changes any of your own local branches under refs/heads, and is safe to do without changing your working copy. I have even heard of people running git fetch periodically in a cron job in the background (although I wouldn't recommend doing this).

A git pull is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches.

From the Git documentation for git pull:

In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.
3 years ago
Instead of converting each date, you can use the following code:
***
long difference = (sDt4.getTime() - sDt3.getTime()) / 1000;
System.out.println(difference);
***
And then see that the result is:
***
1
***
3 years ago