This file contains 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 characters
defmodule SimpleTCP do | |
use Application | |
def start(_type, _args) do | |
import Supervisor.Spec, warn: false | |
children = [ | |
worker(SimpleTCP.Worker, [8000]) | |
] |
This file contains 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 characters
defmodule SimpleTCP.Worker do | |
import Socket | |
def start_link(port) do | |
pid = spawn_link(fn -> init(port) end) | |
{:ok, pid} | |
end | |
def init(port) do |
This file contains 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 characters
defmodule SimpleTCP.Sender do | |
use GenServer | |
import Socket | |
def start_link(socket, opts \\ []) do | |
GenServer.start_link(__MODULE__, [socket: socket], opts) | |
end | |
def init(socket) do | |
# Register the process with gproc and subcscribe to :something |
This file contains 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 characters
defmodule SimpleTCP.Worker do | |
import Socket | |
def start_link(port) do | |
pid = spawn_link(fn -> init(port) end) | |
{:ok, pid} | |
end | |
def init(port) do |
This file contains 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 characters
Project Gutenberg's The Adventures of Sherlock Holmes, by Arthur Conan Doyle | |
This eBook is for the use of anyone anywhere at no cost and with | |
almost no restrictions whatsoever. You may copy it, give it away or | |
re-use it under the terms of the Project Gutenberg License included | |
with this eBook or online at www.gutenberg.net | |
Title: The Adventures of Sherlock Holmes |
This file contains 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 characters
from django.db import models | |
from django.db.models import signals | |
from django.contrib.admin.utils import NestedObjects | |
class AppQuerySet(models.QuerySet): | |
def delete(self, **kwargs): | |
return self.update(is_void=True) | |
class AdvancedManager(models.Manager): |
This file contains 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 characters
from django.db import models | |
from django.db.models import signals | |
from django.contrib.admin.utils import NestedObjects | |
class AppQuerySet(models.QuerySet): | |
def delete(self, **kwargs): | |
return self.update(is_void=True) | |
class AppManager(models.Manager): | |
queryset_class = AppQuerySet |
This file contains 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 characters
>>> book.author.name | |
Traceback (most recent call last): | |
File "<console>", line 1, in <module> | |
File "/Users/kyle/orm_test/library/models.py", line 18, in __get__ | |
'Use `select_related` or `fetch_{rel}`'.format(rel=self.field.name) | |
RelationNotLoaded: Relation `author` not loaded. Use `select_related` or `fetch_author` | |
# We explicitly fetch the resource | |
>>> book.fetch_author() | |
<Author: Author object> |
This file contains 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 characters
# Retrieve values as a dictionary | |
>>> Book.objects.values('title', 'author__name') | |
<QuerySet [ | |
{'author__name': u'Nikolai Gogol', 'title': u'The Overcoat'}, {'author__name': u'Leo Tolstoy', 'title': u'War and Peace'}]> | |
# Retrieve values as a tuple | |
>>> Book.objects.values_list('title', 'author__name') | |
<QuerySet [(u'The Overcoat', u'Nikolai Gogol'), | |
(u'War and Peace', u'Leo Tolstoy')]> | |
>>> Book.objects.values_list('title') | |
<QuerySet [(u'The Overcoat',), (u'War and Peace',)]> |
This file contains 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 characters
>>> from django.db import connection | |
>>> connection.queries | |
[] | |
>>> Author.objects.all() | |
<QuerySet [<Author: Author object>]> | |
>>> connection.queries | |
[{u'time': u'0.002', u'sql': u'SELECT "library_author"."id", "library_author"."name" FROM "library_author" LIMIT 21'}] |
OlderNewer