Skip to content

Commit 7a5f9bd

Browse files
committed
added configurable default parameters
1 parent 93f117e commit 7a5f9bd

6 files changed

Lines changed: 31 additions & 15 deletions

File tree

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name = 'python-statsd',
11-
version = '1.3',
11+
version = '1.4',
1212
author = 'Rick van Hattem',
1313
author_email = 'Rick.van.Hattem@Fawo.nl',
1414
description = '''statsd is a client for Etsy's node-js statsd server.

statsd/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from statsd.timer import Timer
44
from statsd.counter import Counter, increment, decrement
55

6-
__all__ = ['Client', 'Connection', 'Timer', 'Counter', 'increment', 'decrement']
6+
__all__ = ['Client', 'Connection', 'Timer', 'Counter', 'increment',
7+
'decrement']
78

statsd/client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class Client(object):
1313
#: prefixed by name
1414
name = None
1515

16-
#: The :class:`~statsd.connection.Connection` to use, creates a new connection if no \
17-
#: connection is given
16+
#: The :class:`~statsd.connection.Connection` to use, creates a new
17+
#: connection if no connection is given
1818
connection = None
1919

2020
def __init__(self, name, connection=None):
@@ -27,6 +27,7 @@ def __init__(self, name, connection=None):
2727

2828
@classmethod
2929
def _get_name(cls, *name_parts):
30+
3031
def to_str(value):
3132
if isinstance(value, unicode):
3233
value = value.encode('utf-8', 'replace')
@@ -43,8 +44,8 @@ def get_client(self, name=None, class_=None):
4344
:keyword name: The name to use, if the name for this client was `spam`
4445
and the `name` argument is `eggs` than the resulting name will be
4546
`spam.eggs`
46-
:keyword class_: The :class:`~statsd.client.Client` subclass to use (e.g.
47-
:class:`~statsd.Timer` or :class:`~statsd.counter.Counter`)
47+
:keyword class_: The :class:`~statsd.client.Client` subclass to use
48+
(e.g. :class:`~statsd.Timer` or :class:`~statsd.counter.Counter`)
4849
'''
4950

5051
# If the name was given, use it. Otherwise simply clone

statsd/connection.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ class Connection(object):
1111
:keyword sample_rate: The sample rate, defaults to `1` (meaning always)
1212
'''
1313

14-
def __init__(self, host='localhost', port=8125, sample_rate=1):
15-
self._host = host or 'localhost'
16-
self._port = int(port) or 8125
17-
self._sample_rate = sample_rate or 1.0
14+
default_host = 'localhost'
15+
default_port = 8125
16+
default_sample_rate = 1
17+
18+
@classmethod
19+
def set_defaults(cls, host='localhost', port=8125, sample_rate=1):
20+
cls.default_host = host
21+
cls.default_port = port
22+
cls.default_sample_rate = sample_rate
23+
24+
def __init__(self, host=None, port=None, sample_rate=None):
25+
self._host = host or self.default_host
26+
self._port = int(port or self.default_port)
27+
self._sample_rate = sample_rate or self.default_sample_rate
1828
self.logger = logging.getLogger('%s.%s'
1929
% (__name__, self.__class__.__name__))
2030
self.udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -54,3 +64,4 @@ def __repr__(self):
5464
self._port,
5565
self._sample_rate,
5666
)
67+

statsd/counter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ def __sub__(self, delta):
6969
self.decrement(delta=delta)
7070
return self
7171

72+
7273
def increment(key):
73-
counter = Counter(key).increment()
74+
return Counter(key).increment()
75+
7476

7577
def decrement(key):
76-
counter = Counter(key).decrement()
78+
return Counter(key).decrement()
7779

statsd/timer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Timer(statsd.Client):
1010
1111
>>> timer = Timer('application_name')
1212
>>> timer.start()
13-
>>> # do something
13+
>>> # do something
1414
>>> timer.stop('executed_action')
1515
'''
1616

@@ -86,12 +86,12 @@ def decorate(self, function_or_name):
8686
8787
>>> from statsd import Timer
8888
>>> timer = Timer('application_name')
89-
>>>
89+
>>>
9090
>>> @timer.decorate
9191
... def some_function():
9292
... # resulting timer name: application_name.some_function
9393
... pass
94-
>>>
94+
>>>
9595
>>> @timer.decorate('my_timer')
9696
... def some_function():
9797
... # resulting timer name: application_name.my_timer
@@ -103,6 +103,7 @@ def decorate(self, function_or_name):
103103
else:
104104
return self._decorate(function_or_name.__name__, function_or_name)
105105

106+
106107
if __name__ == '__main__':
107108
import doctest
108109
doctest.testmod()

0 commit comments

Comments
 (0)