Skip to content

Instantly share code, notes, and snippets.

@SPRIME01
Forked from alecklandgraf/good.py
Created October 3, 2016 20:55
Show Gist options
  • Select an option

  • Save SPRIME01/6e6f7a4fd95afdc979cde3c987173553 to your computer and use it in GitHub Desktop.

Select an option

Save SPRIME01/6e6f7a4fd95afdc979cde3c987173553 to your computer and use it in GitHub Desktop.

Revisions

  1. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions good.py
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,20 @@
    }
    user_bio = '{first_name} is registered with email: {email}'.format(**user)

    # string format with an object
    class User(object):
    """A lightweight user model"""

    def __init__(self, first_name, last_name, email):
    self.first_name = first_name
    self.last_name = last_name
    self.email = email

    user = User('John', 'Doe', 'j.doe@example.com')

    user_bio = '{user.first_name} is registered with email: {user.email}'.format(user=user)


    # context managers
    """Say you want to time something like this in the shell:
  2. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions good.py
    Original file line number Diff line number Diff line change
    @@ -7,15 +7,15 @@
    user_bio = '{first_name} is registered with email: {email}'.format(**user)

    # context managers
    """Say you want to write something like this:
    """Say you want to time something like this in the shell:
    with Timer('measure my DB query'):
    user_count = User.objects.filter(active=True).count()
    In [8]: with Timer('user count'):
    ...: User.objects.count()
    ...:
    user count: 0.00135397911072
    And here it would output:
    measure my DB query: 0.12
    Well, to accomplish this we simply need a python class with __enter__ and __exit__ methods.
    Well, to accomplish this we simply need a python class, Timer, with __enter__ and __exit__ methods.
    """
    import time

  3. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion good.py
    Original file line number Diff line number Diff line change
    @@ -33,5 +33,5 @@ def __enter__(self):
    def __exit__(self, *args):
    self.end = time.time()
    self.interval = self.end - self.start
    print('{timer.name}: {time.interval}'.format(timer=self))
    print('{timer.name}: {timer.interval}'.format(timer=self))

  4. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion good.py
    Original file line number Diff line number Diff line change
    @@ -33,5 +33,5 @@ def __enter__(self):
    def __exit__(self, *args):
    self.end = time.time()
    self.interval = self.end - self.start
    print('{timer.name}: {time.interval}'.format(timer=self)
    print('{timer.name}: {time.interval}'.format(timer=self))

  5. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion good.py
    Original file line number Diff line number Diff line change
    @@ -33,5 +33,5 @@ def __enter__(self):
    def __exit__(self, *args):
    self.end = time.time()
    self.interval = self.end - self.start
    print('{name}: {interval}'.format(name=self.name, interval=self.interval)
    print('{timer.name}: {time.interval}'.format(timer=self)

  6. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions good.py
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,11 @@
    Well, to accomplish this we simply need a python class with __enter__ and __exit__ methods.
    """
    import time

    class Timer(object):
    """my timer context manager, borrowed from http://rustyrazorblade.com/2016/02/async-python-and-cassandra-with-gevent/"""
    """my timer context manager
    Mostly borrowed from http://rustyrazorblade.com/2016/02/async-python-and-cassandra-with-gevent/
    """

    def __init__(self, name):
    self.name = name
    @@ -30,5 +33,5 @@ def __enter__(self):
    def __exit__(self, *args):
    self.end = time.time()
    self.interval = self.end - self.start
    print('{name}: {interval}'.format(**self.__dict__))
    print('{name}: {interval}'.format(name=self.name, interval=self.interval)

  7. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion good.py
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@
    """
    import time
    class Timer(object):
    """my timer context manager"""
    """my timer context manager, borrowed from http://rustyrazorblade.com/2016/02/async-python-and-cassandra-with-gevent/"""

    def __init__(self, name):
    self.name = name
  8. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion good.py
    Original file line number Diff line number Diff line change
    @@ -30,5 +30,5 @@ def __enter__(self):
    def __exit__(self, *args):
    self.end = time.time()
    self.interval = self.end - self.start
    print('{name}: {interval}'.format(**self.__dict__)
    print('{name}: {interval}'.format(**self.__dict__))

  9. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion good.py
    Original file line number Diff line number Diff line change
    @@ -29,5 +29,6 @@ def __enter__(self):

    def __exit__(self, *args):
    self.end = time.time()
    print('{0}: {1}'.format(self.name, self.end - self.start))
    self.interval = self.end - self.start
    print('{name}: {interval}'.format(**self.__dict__)

  10. Aleck Landgraf revised this gist Feb 12, 2016. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions good.py
    Original file line number Diff line number Diff line change
    @@ -6,3 +6,28 @@
    }
    user_bio = '{first_name} is registered with email: {email}'.format(**user)

    # context managers
    """Say you want to write something like this:
    with Timer('measure my DB query'):
    user_count = User.objects.filter(active=True).count()
    And here it would output:
    measure my DB query: 0.12
    Well, to accomplish this we simply need a python class with __enter__ and __exit__ methods.
    """
    import time
    class Timer(object):
    """my timer context manager"""

    def __init__(self, name):
    self.name = name

    def __enter__(self):
    self.start = time.time()

    def __exit__(self, *args):
    self.end = time.time()
    print('{0}: {1}'.format(self.name, self.end - self.start))

  11. Aleck Landgraf created this gist Feb 12, 2016.
    8 changes: 8 additions & 0 deletions good.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # string format with kwargs
    user = {
    'first_name': 'John',
    'last_name': 'Doe',
    'email': 'j.doe@example.com',
    }
    user_bio = '{first_name} is registered with email: {email}'.format(**user)