def execute(*args, **kwargs): time.sleep(10) logger.debug('a execute:%s, %s', args, kwargs) publisher = Publisher() publisher.publish('B') return None
def handler(data, context): """ Handler method that calculates the difference of a dataset and sends messages to Google Cloud Pub/Sub. :param: data Dictionary like object that holds trigger information. :param: context Google Cloud Function context. """ try: bucket_name = data["bucket"] file_name = data["name"] # Exit when file does not need to be processed if not file_name.startswith(config.prefix_filter): logging.info("Do not process file, exiting...") return "OK", 204 file = GoogleCloudStorage().read(file_name, bucket_name) file.top_level_attribute = config.top_level_attribute file.csv_dialect_parameters = config.csv_dialect_parameters records = file.to_json(Formatter(config.template)) if not config.full_load: if config.state.type == "datastore": records = GoogleCloudDatastore().difference( records, config.state.kind, config.state.property) else: raise NotImplementedError("Unkown state type!") # Exit when no new records exist if not len(records): logging.info("No new records found, exiting...") return "OK", 204 metadata = Gobits.from_context(context=context) publisher = Publisher(config.topic.batch_settings) publisher.publish( config.topic.project_id, config.topic.id, records, metadata.to_json(), config.topic.batch_size, config.topic.subject, ) # Store the new state records if not config.full_load: if config.state.type == "datastore": logging.info("Adding new items to state") GoogleCloudDatastore().put_multi(records, config.state.kind, config.state.property) except Exception as e: logging.exception(e) return "Bad Request", 400 return "OK", 204
def main(): publisher = Publisher() with open(TRIPDATA_PATH, "r") as trip_data: reader = csv.DictReader(trip_data) for line in reader: publisher.publish(json.dumps(line), '/queue/source') publisher.publish(str('exit'), '/queue/source') publisher.disconnect()
def publish(): observer.received_msgs.inc() payload = request.get_json() img_url = payload["url"] queue_name = payload.get("queue_name", "") publisher = Publisher({"rabbitmq_hostname": rabbitmq_hostname}) publisher.publish(img_url, queue_name) observer.processed_msgs.inc() return f"Sent {img_url} to {('exchange ' + publisher.exchange_name) if not queue_name else queue_name}"
def publish(source, destination=""): """ Publishes all the posts found in _posts to destination """ config = Config(source, destination).data() posts = make_posts(config['source'], config['posts_prefix'], extensions=list(markup.extensions())) documents = posts + [HomePage(posts), Feed(posts), Archive(posts)] publisher = Publisher(documents, config['publish_dir'], config['templates_dir'], config) publisher.publish() copy_supporting_files(config['source'], config['destination']) return True
def publish(): bs = request.args.get('bs') topic = request.args.get('topic') try: p = Publisher(bs) result = p.publish(topic, request.get_data()) except Exception as e: result = '{0}'.format(e) return '{0}'.format(result)
def test_subscribe(): p = Publisher() result = [] def callback(data): result.append(data) p.subscribe('test', callback) p.publish('test', 4) assert result[0] == 4 p.publish('test', 4) assert result[1] == 4 p.publish('test2', 4) assert len(result) == 2 p.unsubscribe('test', callback) p.publish('test', 4) assert len(result) == 2
def test_subscribe(): p = Publisher() result = [] def callback(data): result.append(data) p.subscribe('test', callback) p.publish('test', 4) assert result[0] == 4 p.publish('test', 4) assert result[1] == 4 p.publish('test2', 4) assert len(result) == 2 p.unsubscribe('test', callback) p.publish('test', 4) assert len(result) == 2
class TestPublisher(unittest.TestCase): def setUp(self): broker_address = cfg.BROKER_ADDRESS broker_exchange_name = cfg.EXCHANGE_NAME broker_queue_name = cfg.QUEUE_NAME broker_client_obj = BrokerClient(broker_address, broker_exchange_name, broker_queue_name) self._publisher = Publisher(broker_client_obj) def test_connect_to_broker(self): try: loop = asyncio.get_event_loop() loop.run_until_complete(self._publisher.connect_to_broker()) self.assertTrue(True) except ConnectionError: self.assertTrue(False) def test_publish(self): try: meter_house = Meter("HOUSE_TEST") loop = asyncio.get_event_loop() loop.run_until_complete(self._publisher.connect_to_broker()) pv_house_a = loop.run_until_complete( meter_house.generate_power_value()) loop.run_until_complete(self._publisher.publish(pv_house_a)) self.assertTrue(True) except ConnectionError: self.assertTrue(False) def test_disconnect_from_broker(self): try: loop = asyncio.get_event_loop() loop.run_until_complete(self._publisher.connect_to_broker()) loop.run_until_complete(self._publisher.disconnect_from_broker()) self.assertTrue(True) except ConnectionError: self.assertTrue(False)
def publish(self, topic_name, msg): if (topic_name in self.topics) and self.subscriptions[topic_name]: self.message_queue[self.subscriptions[topic_name]].append(msg) else: raise Exception('Topic or subscription does not exist') def pull(self, sub_name): if self.message_queue.get(sub_name): if len(self.message_queue[sub_name]) == 0: return [] else: return self.message_queue[sub_name].pop(0) else: raise Exception('Subscription does not exist') if __name__ == '__main__': topic = 'topic' subscription = 'sub' mb = Broker() pub = Publisher(broker=mb, topic_name=topic) sub = Subscriber(broker=mb, topic_name=topic, sub_name=subscription) pub.create_topic() print(mb.topics) sub.subscribe() print(mb.subscriptions) print(mb.message_queue) pub.publish('Hi there') message = sub.pull() print(message)
import os import sys from time import sleep if __name__ == '__main__': if __package__ is None: import sys from os import path sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) from publisher import Publisher else: from ..publisher import Publisher this_ip = sys.argv[1] pub = Publisher(this_ip) pub.register("topic1") while True: pub.publish("topic1", "TEST") sleep(1)
import os import sys from time import sleep if __name__ == '__main__': if __package__ is None: import sys from os import path sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) from publisher import Publisher else: from ..publisher import Publisher this_ip = sys.argv[1] pub = Publisher(this_ip) pub.register("topic1", 3) pub.register("topic2", 2) pub.register("topic3", 1) while True: pub.publish("topic1", "PUB1") pub.publish("topic2", "PUB1") pub.publish("topic3", "PUB1") sleep(1)
DELAY = 3 * 60 ERROR_DELAY = 30 log_level = os.environ.get("LOG_LEVEL", 20) logging.basicConfig(format='[%(asctime)s][%(levelname)s] %(message)s', level=int(log_level)) GPIO.setmode(GPIO.BCM) if __name__ == '__main__': if not os.environ["LOCATION"]: raise Exception("LOCATION environment variable not specified.") if not os.environ["MQTT_ADDRESS"]: raise Exception("MQTT_ADDRESS environment variable not specified.") publisher = Publisher(os.environ["MQTT_ADDRESS"], os.environ["LOCATION"]) dht = DHT22(14) while True: logging.debug("Reading values from sensor.") h, t, error = dht.read_data() if error: logging.warning("Error reading sensor data.") sleep(ERROR_DELAY) else: logging.info("Publishing values.") publisher.publish(humidity=h, temperature=t) sleep(DELAY)
import os import sys from time import sleep if __name__ == '__main__': if __package__ is None: import sys from os import path sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) from publisher import Publisher else: from ..publisher import Publisher this_ip = sys.argv[1] pub = Publisher(this_ip) pub.register("topic1", 1) while True: pub.publish("topic1", "PUB2") sleep(1)
import os import sys from time import sleep if __name__ == '__main__': if __package__ is None: import sys from os import path sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) from publisher import Publisher else: from ..publisher import Publisher this_ip = sys.argv[1] pub = Publisher(this_ip) pub.register("topic1", 0, 3) pub.register("topic2", 0, 2) pub.publish("topic1", "test1-1") pub.publish("topic1", "test1-2") pub.publish("topic1", "test1-3") pub.publish("topic2", "test2-1") pub.publish("topic2", "test2-2") pub.publish("topic2", "test2-3")
class Observer: def __init__(self, base_dir): self.publisher = Publisher() self.publisher.declare_queue('hello') self.base_dir = base_dir self.csv_filename = 'screenshot_list.csv' self.face_detector = FaceDetector() # self.cap0 = cv2.VideoCapture() # self.cap0.open(0) self.cap = cv2.VideoCapture(1) # default is 0 self.eyegaze_process = None def __del__(self): # self.cap0.release() # if self.eyegaze_process is not None: # self.eyegaze_process.shutdown() self.cap.release() cv2.destroyAllWindows() def one_screenshot(self): ''' :param base_dir: :return: ''' timestamp = str(time.time()) # face_img_path = self.base_dir + '/' + timestamp + ".jpeg" face_img_name = timestamp + ".jpeg" face_img_path = os.path.join(self.base_dir, face_img_name) ret0, mycapture = self.cap.read() # mycapture = pyautogui.screenshot() # mycapture = cv2.cvtColor(np.array(mycapture), cv2.COLOR_RGB2BGR) if mycapture is None: print("capture null") return # cv2.imwrite(path, img) print('saved to ' + face_img_path + " at " + timestamp) # detect face face_flag = self.face_detector.detect(mycapture, face_img_path) if face_flag: print("write face_img to csv!") # write to csv self._write_to_csv(self.csv_filename, face_img_name, timestamp) # face_img_path # publish message self.publisher.publish(face_img_path) # return [path] def _write_to_csv(self, csv_filename, path, timestamp): if not os.path.exists(csv_filename): # file = open(csv_filename, 'w') # file.close() df = pd.DataFrame(columns=['img_name', 'time']) df.to_csv(csv_filename, index=False) with open(csv_filename, 'a') as fd: fd.write(path + ',' + timestamp + '\n') def screenshots(self, time_control: TimeControl): for i in range(time_control.batch_num): batch_start_time = time.time() for j in range(time_control.unit_num): # self.one_screenshot() unit_start_time = time.time() # threading.Thread(target=self.one_screenshot(), args=(), daemon=True) p1 = multiprocessing.Process(target=self.one_screenshot(), args=(), daemon=True) p1.start() unit_end_time = time.time() time_to_sleep = max( 0, time_control.unit_interval - unit_end_time + unit_start_time) time.sleep(time_to_sleep) batch_end_time = time.time() time_to_sleep = max( 0, time_control.batch_interval - batch_end_time + batch_start_time) time.sleep(time_to_sleep) @staticmethod def run_eyegaze(): HOST = '127.0.0.1' PORT = 4242 base_dir = os.path.join(os.getcwd()) # , "data" print(base_dir) eyegaze = Eyegaze(HOST, PORT, base_dir) eyegaze.run_gazepoint() def run(self, time_control: TimeControl, use_eyegaze=False): # open a new process to run the eyegaze program if use_eyegaze is True: self.eyegaze_process = multiprocessing.Process( target=self.run_eyegaze, args=(), daemon=True) self.eyegaze_process.start() print(self.eyegaze_process.pid) self.screenshots(time_control)
import datetime def generate_random_orders(num): order_prefix = 1234567800 orders = [] for i in range(num): cl_order_id = "Order" + str(order_prefix + i) symbol = "0005.HK" side = "Buy" transact_time = str(datetime.datetime.now()) ord_type = "Limit" text = "New Order Single" price = 66 order_single = Order(cl_order_id, symbol, side, transact_time, ord_type, text, price) orders.append(order_single) return orders if __name__ == "__main__": server_admin.start_server() host = server_admin.host port = server_admin.port orders_to_send = generate_random_orders(10) publisher = Publisher(host, port) for order in orders_to_send: publisher.publish(order) publisher.close() server_admin.stop_server()
for line in input_file: try: fields = line.split('|') if isValidPreProcessing(fields) is False: continue CMTE_ID = fields[0] NAME = fields[7] ZIP_CODE = fields[10] TRANSACTION_DT = fields[13] TRANSACTION_AMT = fields[14] OTHER_ID = fields[15] if donor.isRepeat(str(NAME) + str(ZIP_CODE[:5])): results = recipient.calculateRunningPercentile( str(CMTE_ID), str(ZIP_CODE[:5]), str(TRANSACTION_DT[4:]), TRANSACTION_AMT, percentile_value) publisher.publish(results) except BaseException as e: print str(e) continue else: continue input_file.closed
class App(tk.Frame): """Verify and publish trade signals on the Ethereum blockchain. Main.py is a GUI for accessing the functionality of publisher.py and auditor.py modules. Auditor.py and Publisher.py modules could be integrated into existing financial systems or trading software (minus this GUI).""" # Set true to run on Eth mainnet, otherwise run on Ropsten testnet. LIVE = False # Ethereum node endpoints. TESTNET = "" MAINNET = "" # Ethereum public address and private key. pub_k = "" pvt_k = "" # Get one at https://etherscan.io. ETHERSCAN_API_TOKEN = "" def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.parent = parent # Set Eth node endpoint. if self.LIVE: self.endpoint = self.MAINNET else: self.endpoint = self.TESTNET # Load data.json signal dict. with open("data.json") as file: self.data = json.load(file) # Init component modules. self.auditor = Auditor(self.endpoint, self.data, self.ETHERSCAN_API_TOKEN, self.LIVE) self.publisher = Publisher(self.endpoint, self.pub_k, self.pvt_k, self.data) self.init_gui() # Check if address and key are valid self.output.insert( self.output.size() + 1, "Address check: " + str(self.auditor.validate(self.pub_k))) self.output.insert( self.output.size() + 1, "Private key check: " + str(self.auditor.validate(self.pvt_k))) def publish(self): """Publish signal composed from gui combobox inputs to Ethereum, then print results and tx hash in the output pane.""" # No need to check address & key as they are validated at init. # Signal is formed from combobox value strings signal = [ self.instrumentcbb.get(), self.insttypecbb.get(), self.exchangecbb.get(), self.strategycbb.get(), self.directioncbb.get(), self.order_typecbb.get(), self.misccbb.get(), self.triggercbb.get() ] # Print params to output pane self.output.insert(self.output.size() + 1, "Raw signal: " + str(signal)) # Encode param strings encoded = self.publisher.encode(signal) self.output.insert(self.output.size() + 1, "Encoded signal: " + str(encoded)) # Publish and print hash txhash = self.publisher.publish(encoded) self.output.insert(self.output.size() + 1, "Published signal. Tx hash: " + str(txhash)) def audit(self): """Scrape and audit tx's using address parameter from addr_field then print the results in the output pane.""" # Double check address is valid. address = self.addr_field.get() if not self.auditor.validate(address) == "Valid": self.output.insert(self.output.size() + 1, "Invalid address.") # Print to ouput pane result = self.auditor.scrape(address) if result: # First print the raw signal self.output.insert( self.output.size() + 1, str(len(result)) + " encoded signals found at " + str(address) + ":") for i in result: self.output.insert(self.output.size() + 1, i) # Now print decoded human-readable signals decoded = self.auditor.decode(result) self.output.insert(self.output.size() + 1, "Decoded signal output:") for i in decoded: self.output.insert(self.output.size() + 1, i) else: self.output.insert( self.output.size() + 1, str("No signals present for address " + address + ".")) pass def to_clipboard(self, item): """Write the given object to system clipboard.""" c = tk.Tk() c.withdraw() c.clipboard_clear() c.clipboard_append(item) c.update() c.destroy() def init_gui(self): """Init gui components.""" # left panel upper (input panel) self.input = tk.Frame(self, width=430) self.input_upper = tk.Frame(self.input, width=430, height=320) self.input_lower = tk.Frame(self.input, width=430) self.title_label_upper = tk.Label( self.input_upper, text="Search an Ethereum address for encoded signals:", font=('', 11, 'bold')) self.pub_k_label = tk.Label(self.input_upper, text="Your ETH address is " + self.pub_k) pvt_preview = self.pvt_k[:8] + "...." + self.pvt_k[-8:] self.pvt_k_label = tk.Label(self.input_upper, text="Your ETH private key is " + pvt_preview + " (withheld for security)") # noqa self.addr_field = tk.Entry(self.input_upper, width=50) self.addr_field.insert(0, self.pub_k) self.addr_label = tk.Label(self.input_upper, text="Originating address:") self.spacing_label = tk.Label(self.input_upper, text=" ") self.audit_button = tk.Button(self.input_upper, text=" Audit ", command=self.audit) self.copy_pub_k_button = tk.Button( self.input_upper, text=" Copy ", command=lambda: self.to_clipboard(self.pub_k)) self.copy_pvt_k_button = tk.Button( self.input_upper, text=" Copy ", command=lambda: self.to_clipboard(self.pvt_k)) self.title_label_upper.grid(row=0, columnspan=5) self.addr_label.grid(row=1, column=0, columnspan=1, sticky="w") self.addr_field.grid(row=1, column=1, columnspan=2, pady=10) self.spacing_label.grid(row=1, column=4, columnspan=1) self.audit_button.grid(row=1, column=5, columnspan=1, sticky="e") self.pub_k_label.grid(row=2, columnspan=3, sticky="w", pady=20) self.pvt_k_label.grid(row=3, columnspan=3, sticky="w") self.copy_pub_k_button.grid(row=2, column=5, columnspan=1, sticky="w") self.copy_pvt_k_button.grid(row=3, column=5, columnspan=1, sticky="w") # Left panel lower (input panel) self.title_label_lower = tk.Label( self.input_lower, text="Publish trade signals to your Ethereum address:", font=('', 11, 'bold')) self.instrument_label = tk.Label(self.input_lower, text="Instrument:") self.instrumentcbb = ttk.Combobox( self.input_lower, state="readonly", values=list(self.data['data']['instrument'].values()), width=55) self.instrumentcbb.current(0) self.insttype_label = tk.Label(self.input_lower, text="Instrument type:") self.insttypecbb = ttk.Combobox( self.input_lower, state="readonly", values=list(self.data['data']['inst_type'].values()), width=55) self.insttypecbb.current(3) self.exchange_label = tk.Label(self.input_lower, text="Exchange:") self.exchangecbb = ttk.Combobox( self.input_lower, state="readonly", values=list(self.data['data']['exchange'].values()), width=55) self.exchangecbb.current(0) self.strategy_label = tk.Label(self.input_lower, text="Strategy:") self.strategycbb = ttk.Combobox( self.input_lower, state="readonly", values=list(self.data['data']['strategy'].values()), width=55) self.strategycbb.current(2) self.direction_label = tk.Label(self.input_lower, text="Direction:") self.directioncbb = ttk.Combobox( self.input_lower, state="readonly", values=list(self.data['data']['direction'].values()), width=55) self.directioncbb.current(0) self.order_type_label = tk.Label(self.input_lower, text="Order type:") self.order_typecbb = ttk.Combobox( self.input_lower, state="readonly", values=list(self.data['data']['order_type'].values()), width=55) self.order_typecbb.current(2) self.misc_label = tk.Label(self.input_lower, text="Misc:") self.misccbb = ttk.Combobox(self.input_lower, state="readonly", values=list( self.data['data']['misc'].values()), width=55) self.misccbb.current(1) self.trigger_label = tk.Label(self.input_lower, text="Trigger:") self.triggercbb = ttk.Combobox( self.input_lower, state="readonly", values=list(self.data['data']['trigger'].values()), width=55) self.triggercbb.current(0) self.publish_button = tk.Button(self.input_lower, text=" Publish Signal ", width=20, command=self.publish) self.title_label_lower.grid(row=0, columnspan=5, pady=5) self.instrument_label.grid(row=1, column=0, columnspan=1, sticky="w") self.instrumentcbb.grid(row=1, column=2, columnspan=3) self.insttype_label.grid(row=2, column=0, columnspan=1, sticky="w") self.insttypecbb.grid(row=2, column=2, columnspan=3) self.exchange_label.grid(row=3, column=0, columnspan=1, sticky="w") self.exchangecbb.grid(row=3, column=2, columnspan=3) self.strategy_label.grid(row=4, column=0, columnspan=1, sticky="w") self.strategycbb.grid(row=4, column=2, columnspan=3) self.strategy_label.grid(row=5, column=0, columnspan=1, sticky="w") self.strategycbb.grid(row=5, column=2, columnspan=3) self.direction_label.grid(row=6, column=0, columnspan=1, sticky="w") self.directioncbb.grid(row=6, column=2, columnspan=3) self.order_type_label.grid(row=7, column=0, columnspan=1, sticky="w") self.order_typecbb.grid(row=7, column=2, columnspan=3) self.misc_label.grid(row=8, column=0, columnspan=1, sticky="w") self.misccbb.grid(row=8, column=2, columnspan=3) self.trigger_label.grid(row=9, column=0, columnspan=1, sticky="w") self.triggercbb.grid(row=9, column=2, columnspan=3) self.publish_button.grid(row=10, columnspan=6, pady=10) # Add input panels to window self.input_upper.pack(side="top", fill="both", expand=True) self.input_lower.pack(side="top", fill="both", expand=True) # Output pane (right) self.title_frame = tk.Frame(self) self.titlelabel = tk.Label(self.title_frame, text="Output:", font=('', 11, 'bold')) self.titlelabel.pack() self.output = tk.Listbox(self, selectmode=tk.SINGLE) self.output.config(width=100, height=100) self.output.grid(row=1, column=1) # # Status bar self.status_frame = tk.Frame(self) self.status = tk.Label(self.status_frame, text="Ready...", relief=tk.SUNKEN, anchor='w') self.status.pack(fill="both", expand=True) # Add output pane and status bar to parent self.input.grid(row=0, column=0, rowspan=2, sticky="nsew") self.title_frame.grid(row=0, column=1, sticky="ew") self.output.grid(row=1, column=1, sticky="nsew") self.status_frame.grid(row=2, column=0, columnspan=2, sticky="ew") self.grid_rowconfigure(1, weight=1) self.grid_columnconfigure(1, weight=1)
from publisher import Publisher from time import sleep from random import randint my_pub = Publisher("10.0.0.1", 5556) my_pub.register_pub("topic1") my_pub.register_pub("topic2") my_pub.register_pub("topic3") while True: curtopic = "topic" + str(randint(1, 3)) my_pub.publish("topic" + str(randint(1, 3)), str(randint(100000, 999999))) sleep(1)
from publisher import Publisher import json amqp_config = {'exchangeName': 'e.message.created', 'routingKey': 'message.created', 'userName': '******', 'password': '******', 'host': 'localhost', 'port': '5672', 'virtualHost': '/', } writer = Publisher(amqp_config) for x in range(0,1000): some_dict = {'message': 'Test Message', 'value': x, } some_json = json.dumps(some_dict) writer.publish(some_json)
def test_publish(self): pp = Publisher(self.personal_cloud) result = pp.publish(self.test_file_path, self.remote_file_path) self.assertEqual(result, 0) # 0 means published successfully
import os import sys from time import sleep if __name__ == '__main__': if __package__ is None: import sys from os import path sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) ) from publisher import Publisher else: from ..publisher import Publisher this_ip = sys.argv[1] pub = Publisher(this_ip) pub.register("topic1") pub.register("topic2") for i in range(10): t = i % 2 pub.publish("topic" + str(t + 1), i)
def test_publisher(): p = Publisher(host=host, exchange=exchange, queue=queue) for i in range(5): p.publish(json.dumps({'foo': 'bar'})) print('queue size is {}'.format(p.qsize()))