In [2]:
data = [ (1, 2), (3, 4), (5, 6), (7, 8) ]

# Correct!
[(n, (x,y)) for n, (x, y) in enumerate(data)]


Out[2]:
[(0, (1, 2)), (1, (3, 4)), (2, (5, 6)), (3, (7, 8))]

In [1]:
from collections import Iterable

def gen_iter(lst: Iterable, ignore_types=(str, bytes)):
    for item in lst:
        if isinstance(item, Iterable) and not isinstance(item,ignore_types):
            yield from gen_iter(item)
        else:
            yield item
lst = {1: [4,5,6],2: [7,8,9], 3: [10,11,12],4: [13],5: [14],6: [15]}
[x for x in gen_iter(lst)]


Out[1]:
[1, 2, 3, 4, 5, 6]
  • ewqf: $x \equiv a \pmod{b}$

In [49]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1]   # bring some raw data

freq_series = pd.Series.from_array(frequencies)   # in my original code I create a series and run on that, so for consistency I create a series from the list.

x_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0, 121740.0, 123980.0, 126220.0, 128460.0, 130700.0]

# now to plot the figure...
plt.figure(figsize=(12, 8))
ax = freq_series.plot(kind='bar')
ax.set_title("Amount Frequency")
ax.set_xlabel("Amount ($)")
ax.set_ylabel("Frequency")
ax.set_xticklabels(x_labels)

rects = ax.patches

# Now make some labels
labels = ["label%d" % i for i in range(len(rects))]

In [50]:
for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width()/2, height + 5, label, ha='center', va='bottom')

plt.show()



In [144]:
class Person(object):
    def __init__(self, first_name):
        self.first_name = first_name

    # Getter function
    @property
    def last_name(self):
        return self._last_name

    # Setter function
    @last_name.setter
    def last_name(self, value):
        if not isinstance(value, str):
            raise TypeError('Expected a string')
            
        self._last_name = value

    # Deleter function (optional)
    @last_name.deleter
    def last_name(self):
        raise AttributeError("Can't delete attribute")
        
p1 = Person('chois')
p1.last_name='lu'
p1.first_name, p1.last_name


Out[144]:
('chois', 'lu')

In [1]:
class A:
    def spam(self):
        print('A.spam')

class B(A):
    def spam(self):
        print('B.spam')
        super().spam()  # Call parent spam()

In [26]:
# Descriptor attribute for an integer type-checked attribute
class Integer:
    def __init__(self, name):
        self.name = name

    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            return instance.__dict__[self.name]

    def __set__(self, instance, value):
        if not isinstance(value, int):
            raise TypeError('Expected an int')
        instance.__dict__[self.name] = value

    def __delete__(self, instance):
        del instance.__dict__[self.name]

class Point:
    x = Integer('x')
    y = Integer('y')

    def __init__(self, x, y):
        
        print(type(x))
        self.x = x
        self.y = y

In [20]:
class Date:
    __slots__ = ('year', 'month', 'day')
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day
d1 = Date(day='16', month='10', year='2017')

d1


Out[20]:
<__main__.Date at 0x523a208>

In [31]:
from collections import namedtuple
class Point(namedtuple('Point', ['x', 'y'])):
#     __slots__ = ()
    @property
    def hypot(self):
        return (self.x ** 2 + self.y ** 2) ** 0.5
    def __str__(self):
        return 'Point: x=%6.3f  y=%6.3f  hypot=%6.3f' % (self.x, self.y, self.hypot)
p1 = Point(2,3)
p1


Out[31]:
Point(x=2, y=3)

In [19]:
import time
class ContainerCls(object):
    """
    """
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day
        
    @classmethod
    def today(cls):
        t = time.localtime()
        return cls(t.tm_year, t.tm_mon, t.tm_mday)
    
t1 = ContainerCls(2017,10,27)
t1.year
t2 = ContainerCls.today()
t2.day
t3 = time.localtime()
print(t3.tm_zone)


?D1¨²¡À¨º¡Á?¨º¡À??