-
-
Save SPRIME01/6e6f7a4fd95afdc979cde3c987173553 to your computer and use it in GitHub Desktop.
Revisions
-
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 time something like this in the shell: In [8]: with Timer('user count'): ...: User.objects.count() ...: user count: 0.00135397911072 Well, to accomplish this we simply need a python class, Timer, with __enter__ and __exit__ methods. """ import time -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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}: {timer.interval}'.format(timer=self)) -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)) -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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(name=self.name, interval=self.interval) -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ """ import time class Timer(object): """my timer context manager, borrowed from http://rustyrazorblade.com/2016/02/async-python-and-cassandra-with-gevent/""" def __init__(self, name): self.name = name -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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__)) -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,5 +29,6 @@ def __enter__(self): def __exit__(self, *args): self.end = time.time() self.interval = self.end - self.start print('{name}: {interval}'.format(**self.__dict__) -
Aleck Landgraf revised this gist
Feb 12, 2016 . 1 changed file with 25 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)) -
Aleck Landgraf created this gist
Feb 12, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)