
class A(object):

    @classmethod
    def __call__(cls, *args, **kwargs):
        print args

a = A()(__name__)

exit()
a = [2, 5,8, 3,7, 8]
print a[:5]
exit()
import random

stop_on_jackpot = False
print_every = 1000000
prizes = {(0, False):0,
          (1, False):0,
          (2, False):0,
          (3, False):7,
          (4, False):100,
          (5, False):1000000,
          (0, True):4,
          (1, True):4,
          (2, True):7,
          (3, True):100,
          (4, True):50000,
          (5, True):1300000000}


ticket_numbers = set(range(5))
ticket_powerball = 0

balls = tuple(range(69))
powerballs = tuple(range(26))

winnings = 0
spent = 0
max_prize = 0
jackpot = False
while not jackpot or not stop_on_jackpot:
    chosen_balls = random.sample(balls, 5)
    chosen_powerball = random.choice(powerballs)

    numbers_hit = sum(1 for x in chosen_balls if x in ticket_numbers)
    powerball_hit = chosen_powerball == ticket_powerball
    matches = (numbers_hit, powerball_hit)
    jackpot = matches == (5, True)

    prize = prizes[matches]
    winnings += prize
    max_prize = max(prize, max_prize)
    spent += 2

    if spent % print_every == 0 or jackpot:
    	stats = 'spent:{:,}, winnings:{:,}, net:{:,} max_win:{:,}'.format(
    		spent, winnings, winnings-spent, max_prize
    	)
    	print(stats)



exit()

import logging
import logging.handlers

h = logging.handlers.RotatingFileHandler
logger = logging.getLogger()

print logger.name

exit()

from slugify import slugify

def create_slug(title):
    slug = None
    slug_counter = 0
    _slug = slugify(title).lower()
    while True:
        slug = _slug
        if slug_counter > 0:
            slug += str(slug_counter)
        slug_counter += 1
        if not get_by_slug(slug):
            break
    return slug

def get_by_slug(slug):
    return None

print create_slug("malta a lait")

exit()
def f():
    try:
        print "OK"
        return "Boom"
    except Exception as e:
        print e.message
    finally:
        print("Something is executed no matter what")

print f()

exit()

exit()
import re
regex = re.compile('[^a-zA-Z]')
print regex.sub('', 'vita_malt')

exit()

import functools
import inspect

class InspectDecoratorCompatibilityError(Exception):
    pass
class _InspectMethodsDecorators(object):

    def __init__(self, method):
        self.method = method
        self.decos = []

    def parse(self):
        self._parse(self.method)
        return list(set([deco for deco in self.decos if deco]))

    @classmethod
    def extract_deco(cls, line):
        line = line.strip()
        if line.startswith("@"):
            if "(" in line:
                line = line.split('(')[0].strip()
            return line.strip("@")

    def _parse(self, method):
        argspec = inspect.getargspec(method)
        args = argspec[0]
        if args and args[0] == 'self':
            return argspec
        if hasattr(method, '__func__'):
            method = method.__func__
        if not hasattr(method, '__closure__') or method.__closure__ is None:
            raise InspectDecoratorCompatibilityError

        closure = method.__closure__
        for cell in closure:
            inner_method = cell.cell_contents
            if inner_method is method:
                continue
            if not inspect.isfunction(inner_method) \
                and not inspect.ismethod(inner_method):
                continue
            src = inspect.getsourcelines(inner_method)[0]
            self.decos += [self.extract_deco(line) for line in src]
            self._parse(inner_method)

def get_decorators_list(method):
    kls = _InspectMethodsDecorators(method)
    return kls.parse()


def test_get_decorators_list():

    def deco1(func):
        @functools.wraps(func)
        def decorated_view(*args, **kwargs):
            return func(*args, **kwargs)
        return decorated_view

    def deco2(func):
        @functools.wraps(func)
        def decorated_view(*args, **kwargs):
            return func(*args, **kwargs)
        return decorated_view

    class Hi(object):

        @deco1
        @deco2
        def hello(self):
            return True

    k_hi = Hi()
    decos = get_decorators_list(k_hi.hello)
    assert isinstance(decos, list)
    assert "deco1" in decos
    assert "deco2" in decos


test_get_decorators_list()
exit()



def parse_method_spec(method):
    argspec = inspect.getargspec(method)
    args = argspec[0]
    if args and args[0] == 'self':
        return argspec
    if hasattr(method, '__func__'):
        method = method.__func__
    if not hasattr(method, '__closure__') or method.__closure__ is None:
        raise InspectDecoratorCompatibilityError

    closure = method.__closure__
    for cell in closure:
        inner_method = cell.cell_contents
        if inner_method is method:
            continue
        if not inspect.isfunction(inner_method) \
            and not inspect.ismethod(inner_method):
            continue
        src = inspect.getsourcelines(inner_method)[0]
        deco = [extract_decorator_line(line) for line in src]
        print deco
        true_argspec = parse_method_spec(inner_method)
        if true_argspec:
            return true_argspec






def login_required(func):
    @functools.wraps(func)
    def decorated_view(*args, **kwargs):
        print "LOGIN REQUIRED DECO"
        print get_decorators(func)
        return func(*args, **kwargs)
    return decorated_view

def no_login_required(func):
    @functools.wraps(func)
    def decorated_view(*args, **kwargs):
        return func(*args, **kwargs)
    return decorated_view

def jq(func):
    @functools.wraps(func)
    def decorated_view(*args, **kwargs):
        return func(*args, **kwargs)
    return decorated_view

import ast, inspect
def get_decorators2(target):

    res = {}
    def visit_FunctionDef(node):
        res[node.name] = [ast.dump(e) for e in node.decorator_list]
    print target.__code__
    #print inspect.getsource(target)
    V = ast.NodeVisitor()
    V.visit_FunctionDef = visit_FunctionDef
    #V.visit(compile(inspect.getsource(target), '?', 'exec', ast.PyCF_ONLY_AST))
    return res

class Hi(object):

    @login_required
    @jq
    @no_login_required
    def hello(self):
        print "Hello World"

h = Hi()
h.hello()

exit()
def user_is():
    return True
def user_can():
    return AttributeError

@user_is("Admin", "Login")
@user_permission("W")
def p():
    pass
exit()
total = 6
per_page = 2
page = 2

if total > per_page * page:
    print "Here"
    showing = per_page
else:
    print "THERE"
    showing = total - per_page * (page - 1)

showing = (total - per_page) * (page - 1)
print showing

exit()
import oembed

consumer = oembed.OEmbedConsumer()
endpoint = oembed.OEmbedEndpoint('http://www.flickr.com/services/oembed', \
                                 ['http://*.flickr.com/*'])
consumer.addEndpoint(endpoint)

response = consumer.embed('https://www.youtube.com/watch?v=BT5W4zllhCo')

print response['url']

import pprint
pprint.pprint(response.getData())


exit()
from toucan import wp_markdown

data = '''
[TOC]

#Header Palto
Got it
#Header Kila
I see what you did here

#Header Nice
###Kite li anle

**this is some markdown**

blah blah blah
![image here](http://somewebsite.com/image1.jpg)
![another image here](http://anotherwebsite.com/image2.jpg)
'''

print wp_markdown.html(data, lazy_images=True)

print wp_markdown.extract_images(data)

print wp_markdown.toc(data)
exit()

import markdown
from markdown.treeprocessors import Treeprocessor
from markdown.extensions import Extension

# First create the treeprocessor

class ImgExtractor(Treeprocessor):
    def run(self, doc):
        "Find all images and append to markdown.images. "
        self.markdown.images = []
        for image in doc.findall('.//img'):
            self.markdown.images.append(image.get('src'))

# Then tell markdown about it

class ImgExtExtension(Extension):
    def extendMarkdown(self, md, md_globals):
        img_ext = ImgExtractor(md)
        md.treeprocessors.add('imgext', img_ext, '>inline')

# Finally create an instance of the Markdown class with the new extension

md = markdown.Markdown()

# Now let's test it out:

data = '''
**this is some markdown**
blah blah blah
![image here](http://somewebsite.com/image1.jpg)
![another image here](http://anotherwebsite.com/image2.jpg)
'''

html = md.convert(data)
#print html
print md.images


exit()
from six.moves.urllib.parse import urlencode, quote_plus, urlparse


d = urlparse("http://yahoo.com")
x = d.scheme == "https"
print x
exit()

a = {"hello": "world", "name":"whisky and I see"}
print quote_plus("Yo Mardix what's up")

print urlencode(a)

#a.update({"Joe": "Blow"})
#print a
exit()

endpoint_options={
     "read": {"menu": "Documents", "route": "list", "show": False},
     "list": {"menu": "Documents", "route": "list"},
     "archive": {"menu": "Archive", "route": "archive"},
     "authors": {"menu": "Authors", "route": "authors"}
 }






dn = DotDict(endpoint_options)
dn.location = "Charlotte"
dn.telephone.number.gamer = "910-551-7382"
print "Nice" if dn.joe else "Not Nice"

#print dn.joka.bo

print dn.get("archive.route.words", "No Nma")

#print dot_dict(endpoint_options, "archive.route.block.no_flex", "No name")

exit()
import re

# regular expression to match dates in format: 2010-08-27 and 2010/08/27
date_reg_exp = re.compile('\d{4}[/]\d{2}[/]\d{2}')

url_format_map = {
    "%slug": {
        "defaults": {
            "id": None,
            "date": None,
            "month": None
        },
        "pattern": "<regex('\d{4}[/]\d{2}[/]\d{2}'):date>/<slug>"
    },
    "%id-slug": {
        "defaults": {
            "date": None,
            "month": None
        },
        "pattern": "<regex('\d'):id>/<slug>"
    },
    "%date-slug": {
        "defaults": {
            "id": None,
            "month": None
        },
        "pattern": "<regex('\d{4}[/]\d{2}[/]\d{2}'):date>/<slug>"
    },
    "%month-slug": {
        "defaults": {
            "id": None,
            "date": None,
        },
        "pattern": "<regex('\d{4}[/]\d{2}[/]'):month>/<slug>"
    }
}




# a string to test the regular expression above
test_str= """
     fsf2010/08/27sdfsdfsd
     dsf sfds f2010/08/26 fsdf
     asdsds 2009-02-02 afdf
     """
# finds all the matches of the regular expression and
# returns a list containing them
matches_list=date_reg_exp.findall(test_str)

# iterates the matching list and prints all the matches
for match in matches_list:
  print match

exit()
d = {
    "<year>": "([0-9]{4})?",
    "<month>": "([0-9]{2})?",
    "<date>": "([0-9]{2})?",
    "<YMD>": "<year>/<month>/<date>"
}

s = "<year>/<month>/<date>"


pattern = re.compile('|'.join(d.keys()))
result = pattern.sub(lambda x: d[x.group()], s)

print result

exit()
def t():
    return False

a = [True, True, t]

print [x() if hasattr(x, "__call__") else x for x in a]

exit()
def is_auth():
    return is_a

def is_not_auth():
    return not is_a

print any([not is_a])
exit()
import pprint
from portfolio import Portfolio, menu

@menu("All", order=1)
class Index(Portfolio):

    @menu("Home", order=1)
    def index(self):
        pass

    @menu("Contact Us")
    def contact(self):
        pass

@menu("My Account", order=2)
class Hello(Portfolio):
    
    @menu("Hello World Index", class_="Index", order=2, title="The People People")
    def index(self):
        pass

    @menu(name="More About us")
    def about(self):
        pass

app = Portfolio.init(__name__)

#pprint.pprint(Portfolio._menu_stack)


exit()
import functools
import inspect
from flask_classy import FlaskView
from flask import Flask


__menu__ = []

app = Flask(__name__)

def get_class_that_defined_method(meth):
    for cls in inspect.getmro(meth.__class__):
        if meth.__name__ in cls.__dict__:
            return cls
    return None


def menu(name, url=None, **kwargs):
    def wrap(f):
        print name
        print url
        print f.__module__
        print f.__name__

        #arg_spec = inspect.getargspec(f)
        #print arg_spec

        #print inspect.
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            return f
        return wrapper
    return wrap


class Index(FlaskView):

    @menu("Home", url="Index:")
    def index(self):
        pass

    @menu("Login")
    def index(self):
        pass

Index.register(app)



exit()
import functools
import inspect
import wrapt
from flask_classy import FlaskView




class Portfolio(object):
    @classmethod
    def extends_(cls, kls):
        if inspect.isclass(kls):
            for _name, _val in kls.__dict__.items():
                if not _name.startswith("__"):
                    setattr(cls, _name, _val)
        elif inspect.isfunction(kls):
            setattr(cls, kls.__name__, kls)
        return cls


class Q(Portfolio):
    def nice(self):
        return "NICE"

class P(Portfolio):
    def set_name(self, name):
        self.name = name

menu_list = []




p = P()
q = Q()
#print p.__dict__



@p.extends_
@menu
def yo(self, name):
    self.set_name(name)
    print "I'm in self with %s in YO()  " % self.name


@p.extends_
@menu("Home")
class A(object):
    phone = "123-2453-4343"
    CONST = 54

    @classmethod
    @menu
    def c2(cls):
        return None

    def hello(self):
        print "This is hello"

    def index(self, name):
        self.set_name(name)
        print "I'm in self with %s " % self.name
        self.hello()

#print dir(p)
#print p.__dict__
p.hello()
p.yo("Koe")
p.index("Lola")
p.index("Nice One")

q.yo("Jones")
q.hello()
print p.phone
print p.CONST

#print f(name="Jones")
exit()

import functools

def deco(model, **kwargs):
    def wrapper(f):
        print f.__name__
        print model
        print kwargs
        return f
    return wrapper


@deco("my model", name="Jbeats")
class Global(object):
    pass


Global()
exit()




s1 = set([])
s2 = set(["B", "C", "W"])

deleted_s = s1 - s2
new_s = s2 - s1

print deleted_s
print new_s
temp3 = [x for x in s1 if x not in s2]

print temp3
exit()
from portfolio import utils

class Struct:
    def __init__(self, **entries): self.__dict__.update(entries)

a = Struct(Name="Macxis", Location="Charlotte")

print a.Name
a.Name = "Jose"
print a.Name
print a.Location

print type(a)
def test_mailer_ses():
    pass

def Mailer():
    pass


mailer = Portfolio.bind_(Mailer.init_app())()


class BaseClass(object):
    def hello(self):
        print "Hello"

print "bad_boy".capitalize()
new_class = type("NewClassName", (BaseClass,), {})
print new_class
nc = new_class()
nc.hello()


exit()
import werkzeug.exceptions as wex
import sys
import inspect

def import_string(import_name):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).
    If `silent` is True the return value will be `None` if the import fails.
    :param import_name: the dotted name for the object to import.
    :param silent: if set to `True` import errors are ignored and
                   `None` is returned instead.
    :return: imported object
    """
    # force the import name to automatically convert to strings
    # __import__ is not able to handle unicode strings in the fromlist
    # if the module is a package
    import_name = str(import_name).replace(':', '.')

    try:
        try:
            __import__(import_name)
        except ImportError:
            if '.' not in import_name:
                raise
        else:
            return sys.modules[import_name]

        module_name, obj_name = import_name.rsplit('.', 1)

        try:
            module = __import__(module_name, None, None, [obj_name])
        except ImportError:
            # support importing modules not yet set up by the parent module
            # (or package for that matter)
            module = import_string(module_name)

        try:
            print getattr(module, obj_name)
            return getattr(module, obj_name)
        except AttributeError as e:
            raise ImportError(e)

    except ImportError as e:
        pass



class HTTPException(object):
    pass

class Hello(HTTPException):
    pass

maps = {}
g = import_string("toucan.exceptions")
for name in dir(g):
    obj = getattr(g, name)
    try:
        if issubclass(obj, wex.HTTPException):
            maps[name] = obj
    except TypeError as ter:
        pass
print maps

exit()
for name, obj in g.items():
    try:
        if issubclass(obj, HTTPException):
            print name
    except TypeError as ter:
        pass


exit()


print (403 // 100)
exit()

def f(*a):
    l = ["a", "b"]
    l += list(a)
    print l

f("jone", "marie", "Locs")

exit()
class Deploy(object):
    """
    A class that allows you to deploy with git without setting up git remotes

    """
    def __init__(self, CWD, config_file="propel.yml"):
        """

        :param CWD: Current working dir
        :param config_file: the config file
        :return:
        """
        key = "deploy-remotes"
        with open(config_file) as propel_file:
            config = yaml.load(propel_file)
        self.config = config[key]
        self.CWD = CWD

    def run(self, cmd):
        subprocess.call(cmd.strip(), shell=True)

    def remote(self, name):
        remotes = self.config[name]
        name = "webcli_push__%s" % name
        cmd = self._gen_git_remote_command(name, remotes)
        cmd += self._gen_git_push_remote(name, force)
        cmd += self._gen_git_remove_remote(name)
        self.run("cd %s; %s" % (self.CWD, cmd))

    def all(self):
        l = []
        [l.extend(h) for k, h in self.config.items()]
        remotes = list(set(l))
        name = "webcli_push__all"
        cmd = self._gen_git_remote_command(name, remotes)
        cmd += self._gen_git_push_remote(name, force)
        cmd += self._gen_git_remove_remote(name)
        self.run("cd %s; %s" % (self.CWD, cmd))

    def reset_git(self):
        cmd = ""
        for k, values in self.config.items():
            cmd += self._gen_git_remote_command(k, values)
        self.run("cd %s; %s" % (self.CWD, cmd))

    def _gen_git_push_remote(self, name, force=False):
        force = " -f" if force else ""
        return "git push %s %s master;" % (force, name)

    def _gen_git_remove_remote(self, name):
        return "git remote remove %s;" % name

    def _gen_git_remote_command(self, name, remotes):
        """
        Generate the push command for a remote
        :param name (str): the remote name
        :param remotes (list): list of
        :return str:
        """
        if not isinstance(remotes, list):
            raise TypeError("'remotes' must be of list type")

        cmd = gen_git_remove_remote(name)
        cmd += "git remote add %s %s;" % (name, remotes[0])
        if len(remotes) > 1:
            for h in remotes:
                cmd += "git remote set-url %s --push --add %s;" % (name, h)
        return cmd





