examples: python: move frequently used imports to the top

This commit is contained in:
Alexey Ivanov 2012-12-10 05:03:12 +08:00
Родитель f509f11311
Коммит ee129e79f6
1 изменённых файлов: 4 добавлений и 3 удалений

Просмотреть файл

@ -3,6 +3,9 @@
# Steve Ivy <steveivy@gmail.com> # Steve Ivy <steveivy@gmail.com>
# http://monkinetic.com # http://monkinetic.com
from random import random
from socket import socket, AF_INET, SOCK_DGRAM
# Sends statistics to the stats daemon over UDP # Sends statistics to the stats daemon over UDP
class StatsdClient(object): class StatsdClient(object):
def __init__(self, host='localhost', port=8125): def __init__(self, host='localhost', port=8125):
@ -56,14 +59,12 @@ class StatsdClient(object):
sampled_data = {} sampled_data = {}
if (sample_rate < 1): if (sample_rate < 1):
import random if random() <= sample_rate:
if random.random() <= sample_rate:
for stat, value in data.items(): for stat, value in data.items():
sampled_data[stat] = "%s|@%s" %(value, sample_rate) sampled_data[stat] = "%s|@%s" %(value, sample_rate)
else: else:
sampled_data = data sampled_data = data
from socket import socket, AF_INET, SOCK_DGRAM
udp_sock = socket(AF_INET, SOCK_DGRAM) udp_sock = socket(AF_INET, SOCK_DGRAM)
try: try:
for stat, value in sampled_data.items(): for stat, value in sampled_data.items():