Package: dnaStreaming
How To UseThis a Python3 library is for Dow Jones customers consuming data from a Dow Jones Factiva Stream.To use this we recommend ‘pip installing’ this by making the following addition to your requirements.txt:# To fetch latest version from PyPi
dnaStreaming

# to fetch latest version from GitHub
git+https://github.com/dowjones/dj-dna-streams-python#egg=dnaStreamingAuthThere is currently one way to authenticate, which is by usingyour user key.ConfiguringTo run this code, you need to provide credentials from one of the authentication methods and your subscriptions. There are 3 ways to do this. You can either set environment variables or you can use a configuration file.1. Set environment variables.To set your service account credentials, set:An environment variable named ‘USER_KEY’.To set your subscription ID, simply set an environment variable named ‘SUBSCRIPTION_ID’ like soexport SUBSCRIPTION_ID="ABC1234567889"The code above is the command line expression for setting this environment variable on Mac OSX. Other operating systems might have a slightly different techniques for setting environment variables on the command line.2. Using the configuration file.In this codebase you will find a file named ‘customer_config.json’. You are not required to use this file. If you prefer to use this configuration file, follow these directions: Open this file and add your service account credentials. Then add your subscription IDs. Remember that this is a JSON file so follow basic JSON formatting and syntax conventions.3. Pass in variables as function arguments.You may pass your service account credentials to the Listener constructor like so:fromdnaStreaming.listenerimportListener# User key authenticationlistener=Listener(user_key=<YOURUSERKEY>)Or you may use the environment variables.
Remember that passing credentials and subscription ID(s) in this way will override the environment variable and the config file settings.fromdnaStreaming.listenerimportListenerlistener=Listener()4. Listening to messagesYou may want to listen messages synchronously like so:defcallback(message,subscription_id):print('Subscription ID:{}: Message:{}'.format(subscription_id,message.data.__str__()))returnTrue# If desired return False to stop the message flow. This will unblock the process as well.listener.listen(callback,maximum_messages=10)# Omitting maximum_messages means you will continue to get messages as they appear. Can be a firehose. Use with caution.# You may pass subscription ID as a parameter to the listen functionYou may want to listen messages asynchronously like so:defcallback(message,subscription_id):print('Subscription ID:{}: Message:{}'.format(subscription_id,message.data.__str__()))future=listener.listen_async(callback)# After calling `listed_async` you need to keep the main thread alive.forcountinrange(0,5):sleep(1)# Stop receiving messages after 5 secondsiffuture.running():future.cancel()Log FilesVery minimal logging is written to the module’s path ‘logs/dj-dna-streaming-python.log’. To keep maintenance simple this log is overwritten every time the app starts.Testingcd dnaStreaming/test
pip install -r requirements.txt
py.test . -sor, alternatively, to test against python2.7 and python3.5:toxFlake8If you are maintaining this library, ensure you run flake8 before you commit. At project root command line:flake8 ./dnaStreaming ./testsRunning the Demonstration Code/DevelopmentIf you are enhancing this codebase (and not just using it as a library), follow these example MacOS steps:1. Checkout the Project from Git.2. Go to the Project Root.3. Create a Virtual Environment.virtualenv venv4. Then activate the virutal environment by executing this command:source ./venv/bin/activate5. Install the Dependenciespip install -r requirements.txt6. Install the App:python setup.py install7. Set the Configuration VariablesSee the config section.8. Run the Demo CodeRunning Non-Docker Demo:Execute the following at the project root:python ./dnaStreaming/demo/show_stream.py -sOrpython ./dnaStreaming/demo/show_stream_async.py -sIf you are havingImportError:  No module named …run this in your terminal before running the demo:export PYTHONPATH='.'Running Docker DemoExecute the following at the project root:docker run -it \
-e USER_KEY=<your user KEY> \
-e SUBSCRIPTION_ID=<your subscription ID> \
dj-dna-streaming-python