diff --git a/algorithms/insertion_sort.py b/algorithms/insertion_sort.py index cf4fd5f..332d6b0 100644 --- a/algorithms/insertion_sort.py +++ b/algorithms/insertion_sort.py @@ -2,7 +2,7 @@ def insertionSort(mylist): for index in range(1, len(mylist)): - print "Index is ", index # 1, 2, 3, 4, 5, 6, 7, 8; this is the outer loop + print("Index is ", index) # 1, 2, 3, 4, 5, 6, 7, 8; this is the outer loop # setup first case (only one item) currentvalue = mylist[index] @@ -17,6 +17,6 @@ def insertionSort(mylist): if __name__ == '__main__': mylist = [54,26,93,17,77,31,44,55,20] - print "Original: ", mylist + print("Original: ", mylist) insertionSort(mylist) - print "Insertion Sorted: ", mylist \ No newline at end of file + print("Insertion Sorted: ", mylist) \ No newline at end of file diff --git a/algorithms/merge_sort.py b/algorithms/merge_sort.py index eb044cd..77cbb24 100644 --- a/algorithms/merge_sort.py +++ b/algorithms/merge_sort.py @@ -2,14 +2,14 @@ def mergeSort(mylist): - print "Splitting", mylist + print("Splitting", mylist) if len(mylist) > 1: mid = len(mylist) // 2 lefthalf = mylist[:mid] - print "Left half ", lefthalf + print("Left half ", lefthalf) righthalf = mylist[mid:] - print "Right half ", righthalf + print("Right half ", righthalf) mergeSort(lefthalf) mergeSort(righthalf) @@ -42,11 +42,11 @@ def mergeSort(mylist): j += 1 k += 1 - print "Merging", mylist + print("Merging", mylist) if __name__ == '__main__': - mylist = [54,26,93,17,77,31,44,55,20] - print "Original: ", mylist + mylist = [54, 26, 93, 17, 77, 31, 44, 55, 20] + print("Original: ", mylist) mergeSort(mylist) - print "Merge Sorted: ", mylist \ No newline at end of file + print("Merge Sorted: ", mylist) diff --git a/algorithms/quick_select.py b/algorithms/quick_select.py new file mode 100644 index 0000000..223e8ab --- /dev/null +++ b/algorithms/quick_select.py @@ -0,0 +1,62 @@ +""" Python 3 implementation of a quickselect algorithm """ +from typing import List +import unittest +import random + + +class Solution: + + def quickselect(self, items, item_index): + if items is None or len(items) < 1: + return None + + if item_index < 0 or item_index > len(items) - 1: + raise IndexError() + + return self.select(items, 0, len(items) - 1, item_index) + + def select(self, lst, l, r, index): + # base case + if r == l: + return lst[l] + + # choose random pivot + pivot_index = random.randint(l, r) + + # move pivot to beginning of list + lst[l], lst[pivot_index] = lst[pivot_index], lst[l] + + # partition + i = l + for j in range(l+1, r+1): + if lst[j] < lst[l]: + i += 1 + lst[i], lst[j] = lst[j], lst[i] + + # move pivot to correct location + lst[i], lst[l] = lst[l], lst[i] + + # recursively partition one side only + if index == i: + return lst[i] + elif index < i: + return self.select(lst, l, i-1, index) + else: + return self.select(lst, i+1, r, index) + + +class SolutionTest(unittest.TestCase): + + def test_quickselect(self): + s = Solution() + response = s.quickselect([12, 2, 4, 3, 5], 2) + assert response == 4 + + response = s.quickselect([12, 2, 4, 3, 5], 0) + assert response == 2 + + + + +if __name__ == '__main__': + unittest.main() diff --git a/algorithms/quick_sort.py b/algorithms/quick_sort.py index b8bf974..bce0341 100644 --- a/algorithms/quick_sort.py +++ b/algorithms/quick_sort.py @@ -1,45 +1,71 @@ -""" Quick Sort """ +""" Quick Sort in Python3 +Quick sort uses divide and conquer to gain the same advantages as merge sort, +with the benefit of using less storage, but at the cost of a worse worst case runtime +O(n^2) if the pivot values are bad. +""" +import pdb +from typing import List def quickSort(mylist): + """ Initialize our recursive function """ quickSortHelper(mylist, 0, len(mylist)-1) def quickSortHelper(mylist, first, last): - if first < last: + """ Recursive function to split up """ + if first < last: # check if need to sort still + splitpoint = partition(mylist, first, last) + # now that we know our splitpoint, we can then recursively run quicksort on the list's bottom half and top half quickSortHelper(mylist, first, splitpoint-1) quickSortHelper(mylist, splitpoint+1, last) def partition(mylist, first, last): - pivotvalue = mylist[first] + """ Partition Process, made up of: + * Pick a pivot value (i.e. what we'll compare our unsorted numbers to) + Based off this value, we'll compare our unsorted values and either move + our items to the left of the pivot or to the right of the pivot. + * """ + pivotvalue = mylist[first] # get the first value as pivotvalue - leftmark = first+1 + leftmark = first + 1 rightmark = last done = False while not done: + # Go from leftmost side onwards (to right) and try to find a value + # that is greater than the pivot value (i.e. left side of pivot should be + # smaller values than pivot value, if we found one that is greater, we + # stop at leftmark, saying we need to do a swap to the right side) while leftmark <= rightmark and mylist[leftmark] <= pivotvalue: leftmark += 1 - while mylist[rightmark] >= pivotvalue and rightmark >= leftmark: + # Go from rightmost side inwards (to left) and try to find a value + # that is less than the pivot value (i.e. right side of pivot should be + # greater values than pivot value, if we found one that is smaller, we + # stop at rightmark, saying we need to do a swap to the left side) + while rightmark >= leftmark and mylist[rightmark] >= pivotvalue: rightmark -= 1 if rightmark < leftmark: - done = True + done = True # we're done sorting through this list because we've crossed else: - # swap + # we have a swap between a value in the left list and a value in the right list mylist[leftmark], mylist[rightmark] = mylist[rightmark], mylist[leftmark] - # swap - mylist[leftmark], mylist[rightmark] = mylist[rightmark], mylist[leftmark] - + # Once rightmark is less than leftmark, then rightmark is now the split point. + # That means what we picked as the pivot value can now be exchanged with the + # contents of the split point and the pivot value is now in the correct place + # Note: remember that our pivot value was the first value in our list + mylist[first], mylist[rightmark] = mylist[rightmark], mylist[first] + return rightmark if __name__ == '__main__': - mylist = [54,26,93,17,77,31,44,55,20] - print "Original: ", mylist + mylist = [54, 26, 93, 17, 77, 31, 44, 55, 20] + print("Original: ", mylist) quickSort(mylist) - print "Quick Sorted: ", mylist \ No newline at end of file + print("Quick Sorted: ", mylist) diff --git a/avro/example.py b/avro/example.py new file mode 100644 index 0000000..2e43fc1 --- /dev/null +++ b/avro/example.py @@ -0,0 +1,19 @@ +import pdb + +import avro.schema +from avro.datafile import DataFileReader, DataFileWriter +from avro.io import DatumReader, DatumWriter + + +schema = avro.schema.Parse(open("user.avsc").read()) + +writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema) +writer.append({"name": "Alyssa", "favorite_number": 256}) +writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"}) +writer.close() + +reader = DataFileReader(open("users.avro", "rb"), DatumReader()) +for user in reader: + print(user) + +reader.close() diff --git a/avro/user.avsc b/avro/user.avsc new file mode 100644 index 0000000..fac4b1d --- /dev/null +++ b/avro/user.avsc @@ -0,0 +1,11 @@ +{ + + "namespace": "example.avro", + "type": "record", + "name": "User", + "fields": [ + {"name": "name", "type": "string"}, + {"name": "favorite_number", "type": ["int", "null"]}, + {"name": "favorite_color", "type": ["string", "null"]} + ] +} diff --git a/avro/users.avro b/avro/users.avro new file mode 100644 index 0000000..de4e0b4 Binary files /dev/null and b/avro/users.avro differ diff --git a/boto/amazon_boto3_sample.py b/boto/amazon_boto3_sample.py new file mode 100644 index 0000000..d678d20 --- /dev/null +++ b/boto/amazon_boto3_sample.py @@ -0,0 +1,82 @@ +""" +Fabfile runs functions as commands with 'fab' then the function name + +Usage: + fab + +Examples: + fab print_me + +Assumes: + Your environment files and variables are setup: + e.g. AWS_DEFAULT_REGION + And/or your ~/.aws/config and credentials file is setup with: + config + [default] + region=us-west-2 + credentials + [default] + aws_access_key_id='' + aws_secret_access_key='' + +Example .bashrc / .bash_profile alias + alias print_me="fab -f /home/will/GitHub/python-examples/fabric/fabfile.py print_me" +""" + +import os + +import boto3 + + +AWS_DEFAULT_REGION = os.getenv('AWS_DEFAULT_REGION', 'us-west-2') + + +def print_me(): + """ Run 'fab print_me' to test fab works locally """ + print("Hey") + + +def list_buckets(): + """ List all buckets in a S3 """ + s3_client = boto3.client('s3') + response = s3_client.list_buckets() + buckets = [bucket['Name'] for bucket in response['Buckets']] + print(buckets) + + response = s3_client.get_bucket_location('my_bucket') + print(response) + #{'LocationConstraint': 'us-west-1', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HostId': '', ... + + return response['Buckets'] + + +def create_bucket(bucket_name): + """ Create a bucket """ + s3_client = boto3.client('s3') + response = s3_client.create_bucket(Bucket=bucket_name, + CreateBucketConfiguration={'LocationConstraint': AWS_DEFAULT_REGION}) + # Make sure to check that your ~/.aws/config default region is pointed to the same region as this call + return response + + +def get_all_files_from_bucket(bucket_name): + s3_resource = boto3.resource('s3') + my_bucket = s3_resource.Bucket(bucket_name) + for s3_file in my_bucket.objects.all(): + print(s3_file.key) + + +def get_file_from_bucket(bucket_name, filename): + s3_resource = boto3.resource('s3') + response = s3_resource.Bucket('my_bucket_name').download_file('my_remote_file.txt', '/home/will/my_local_file.txt') + print(response) + + +def put_file_into_bucket(bucket_name, filename): + s3_resource = boto3.resource('s3') + response = s3_resource.Bucket('my_bucket_name').upload_file('/home/will/my_local_file.txt', 'my_remote_file.txt') + print(response) + + +if __name__ == '__main__': + pass diff --git a/Fabric/sample_fabric.py b/fabric/sample_fabric.py similarity index 99% rename from Fabric/sample_fabric.py rename to fabric/sample_fabric.py index ef9b776..cfaee71 100644 --- a/Fabric/sample_fabric.py +++ b/fabric/sample_fabric.py @@ -18,7 +18,7 @@ """ #import logging # debug output Paramiko prints -from fabric.api import run +from fabric.api import run, cd #For debugging, prints Paramiko's debug statements to #standard error stream diff --git a/jenkins/test_build.py b/jenkins/test_build.py new file mode 100644 index 0000000..7f8e8f9 --- /dev/null +++ b/jenkins/test_build.py @@ -0,0 +1,18 @@ +from jenkinsapi.jenkins import Jenkins + + +jenkins = Jenkins('http://localhost:8078', username='admin', password='admin') +job_name = 'django_unit_tests' +job = jenkins[job_name] + +queue_item = job.invoke() +print('Building ... ') + +queue_item.block_until_complete() + +build = job.get_last_completed_build() + +print('Last build number was: ', build.get_number()) +print('Last build result was: ', build.get_status()) +#print('Last build based on branch: ', build.get_revision_branch()) + diff --git a/logging/sample_msg_to_logstash.py b/logging/sample_msg_to_logstash.py new file mode 100644 index 0000000..370fd74 --- /dev/null +++ b/logging/sample_msg_to_logstash.py @@ -0,0 +1,27 @@ +# Send a sample message to logstash + +import socket +import json +import sys + +HOST = 'localhost' +PORT = 5000 + +try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +except socket.error, msg: + sys.stderr.write("[ERROR] %s\n" % msg[1]) + sys.exit(1) + +try: + sock.connect((HOST, PORT)) +except socket.error, msg: + sys.stderr.write("[ERROR] %s\n" % msg[1]) + sys.exit(2) + +msg = {'@message': 'python test message, hello there', '@tags': ['python', 'test']} + +sock.send(json.dumps(msg)) + +sock.close() +sys.exit(0) diff --git a/pyarrow/deathstar.parquet b/pyarrow/deathstar.parquet new file mode 100644 index 0000000..c94f66a Binary files /dev/null and b/pyarrow/deathstar.parquet differ diff --git a/pyarrow/parquet_example.py b/pyarrow/parquet_example.py new file mode 100644 index 0000000..3861de3 --- /dev/null +++ b/pyarrow/parquet_example.py @@ -0,0 +1,186 @@ +import pdb + +import arrow +import numpy as np +import pandas as pd +import pyarrow as pa +import pyarrow.parquet as pq +import requests +from pandas import Series +from pandas.io.json import json_normalize + + +def get_api_endpoint(url: str) -> pd.DataFrame: + """ Get API endpoint and convert to DataFrame + Example: + url: https://swapi.co/api/starships/9/ + data: + +(Pdb) print(d) + {'MGLT': '10', 'cargo_capacity': '1000000000000', + 'films': ['https://swapi.co/api/films/1/'], + 'manufacturer': 'Imperial Department of Military Research, Sienar Fleet Systems', + 'starship_class': 'Deep Space Mobile Battlestation', + 'created': '2014-12-10T16:36:50.509000Z', + 'model': 'DS-1 Orbital Battle Station', + 'url': 'https://swapi.co/api/starships/9/', + 'consumables': '3 years', 'hyperdrive_rating': '4.0', + 'crew': '342953', 'name': 'Death Star', 'max_atmosphering_speed': 'n/a', + 'edited': '2014-12-22T17:35:44.452589Z', 'length': '120000', + 'pilots': [], 'cost_in_credits': '1000000000000', 'passengers': '843342'} + df: + df.columns + Index(['MGLT', 'cargo_capacity', 'consumables', 'cost_in_credits', 'created', + 'crew', 'edited', 'films', 'hyperdrive_rating', 'length', + 'manufacturer', 'max_atmosphering_speed', 'model', 'name', 'passengers', + 'pilots', 'starship_class', 'url'], + dtype='object') + +(Pdb) df.head() + MGLT cargo_capacity consumables cost_in_credits \ + 0 10 1000000000000 3 years 1000000000000 ... + """ + r = requests.get(url) + d = r.json() + return pd.DataFrame(dict([(k, Series(v)) for k, v in d.items()])) + + +def df_add_partition_columns(df, date_field): + """ Return a dataframe with new columns used for partitioning by datetime + Example: 2018-03-04T14:12:15.653Z returns with new df columns of 'year', 'month', day' + """ + df[date_field] = df[date_field].map(lambda t: pd.to_datetime(t, format="%Y-%m-%dT%H:%M:%S.%fZ")) + df['year'], df['month'], df['day'] = df[date_field].apply(lambda x: x.year), df[date_field].apply(lambda x: x.month), df[date_field].apply(lambda x: x.day) + return df + + +def df_to_parquet_table(df: pd.DataFrame) -> pa.Table: + """ Convert DataFrame to Pyarrow Table + Example: + pyarrow.Table + MGLT: string + cargo_capacity: string + consumables: string + cost_in_credits: string + created: string + crew: string + edited: string + films: string + hyperdrive_rating: string + length: string + manufacturer: string + max_atmosphering_speed: string + model: string + name: string + passengers: string + pilots: double + starship_class: string + url: string + __index_level_0__: int64 + metadata + -------- + {b'pandas': b'{"columns": [{"field_name": "MGLT", "pandas_type": "unicode", "m' + b'etadata": null, "name": "MGLT", "numpy_type": "object"}, {"field' + b'_name": "cargo_capacity", "pandas_type": "unicode", "metadata": ' + b'null, "name": "cargo_capacity", "numpy_type": "object"}, {"field' + b'_name": "consumables", "pandas_type": "unicode", "metadata": nul' + b'l, "name": "consumables", "numpy_type": "object"}, {"field_name"' + b': "cost_in_credits", "pandas_type": "unicode", "metadata": null,' + b' "name": "cost_in_credits", "numpy_type": "object"}, {"field_nam' + b'e": "created", "pandas_type": "unicode", "metadata": null, "name' + b'": "created", "numpy_type": "object"}, {"field_name": "crew", "p' + b'andas_type": "unicode", "metadata": null, "name": "crew", "numpy' + b'_type": "object"}, {"field_name": "edited", "pandas_type": "unic' + b'ode", "metadata": null, "name": "edited", "numpy_type": "object"' + b'}, {"field_name": "films", "pandas_type": "unicode", "metadata":' + b' null, "name": "films", "numpy_type": "object"}, {"field_name": ' + b'"hyperdrive_rating", "pandas_type": "unicode", "metadata": null,' + b' "name": "hyperdrive_rating", "numpy_type": "object"}, {"field_n' + b'ame": "length", "pandas_type": "unicode", "metadata": null, "nam' + b'e": "length", "numpy_type": "object"}, {"field_name": "manufactu' + b'rer", "pandas_type": "unicode", "metadata": null, "name": "manuf' + b'acturer", "numpy_type": "object"}, {"field_name": "max_atmospher' + b'ing_speed", "pandas_type": "unicode", "metadata": null, "name": ' + b'"max_atmosphering_speed", "numpy_type": "object"}, {"field_name"' + b': "model", "pandas_type": "unicode", "metadata": null, "name": "' + b'model", "numpy_type": "object"}, {"field_name": "name", "pandas_' + b'type": "unicode", "metadata": null, "name": "name", "numpy_type"' + b': "object"}, {"field_name": "passengers", "pandas_type": "unicod' + b'e", "metadata": null, "name": "passengers", "numpy_type": "objec' + b't"}, {"field_name": "pilots", "pandas_type": "float64", "metadat' + b'a": null, "name": "pilots", "numpy_type": "float64"}, {"field_na' + b'me": "starship_class", "pandas_type": "unicode", "metadata": nul' + b'l, "name": "starship_class", "numpy_type": "object"}, {"field_na' + b'me": "url", "pandas_type": "unicode", "metadata": null, "name": ' + b'"url", "numpy_type": "object"}, {"field_name": "__index_level_0_' + b'_", "pandas_type": "int64", "metadata": null, "name": null, "num' + b'py_type": "int64"}], "column_indexes": [{"field_name": null, "pa' + b'ndas_type": "unicode", "metadata": {"encoding": "UTF-8"}, "name"' + b': null, "numpy_type": "object"}], "pandas_version": "0.22.0", "i' + b'ndex_columns": ["__index_level_0__"]}'} + """ + pyarrow_deathstar_table = pa.Table.from_pandas(df) # Create PyArrow Table from Pandas DF + print(pyarrow_deathstar_table) + pq.write_table(pyarrow_deathstar_table, 'deathstar.parquet') # Convert PyArrow Table to Parquet Table / File + parquet_table = pq.read_table('deathstar.parquet') # Read back Parquet File as a Table + parquet_table = pq.ParquetFile('deathstar.parquet') # Read back Parquet File as a ParquetFile for finer-grained read and write + print(parquet_table.metadata) + # + # created_by: parquet-cpp version 1.4.1-SNAPSHOT + # num_columns: 19 + # num_rows: 1 + # num_row_groups: 1 + # format_version: 1.0 + # serialized_size: 4574 + + print(parquet_table.schema) + # + #MGLT: BYTE_ARRAY UTF8 + #cargo_capacity: BYTE_ARRAY UTF8 + #consumables: BYTE_ARRAY UTF8 + #cost_in_credits: BYTE_ARRAY UTF8 + #created: BYTE_ARRAY UTF8 + #crew: BYTE_ARRAY UTF8 + #edited: BYTE_ARRAY UTF8 + #films: BYTE_ARRAY UTF8 + #hyperdrive_rating: BYTE_ARRAY UTF8 + #length: BYTE_ARRAY UTF8 + #manufacturer: BYTE_ARRAY UTF8 + #max_atmosphering_speed: BYTE_ARRAY UTF8 + #model: BYTE_ARRAY UTF8 + #name: BYTE_ARRAY UTF8 + #passengers: BYTE_ARRAY UTF8 + #pilots: DOUBLE + #starship_class: BYTE_ARRAY UTF8 + #url: BYTE_ARRAY UTF8 + #__index_level_0__: INT64 + return parquet_table + + +def write_parquet_table_as_partitioned_dataset(parquet_file) -> pq.ParquetDataset: + """ Write a parquet table as a parititioned dataset (i.e. multiple Parquet files) + An example of a dataset partitioned by year and month on disk might look like: + dataset_name/ + year=2018/ + month=09/ + 0.parq + 1.parq + month=10/ + 0.parq + 1.parq + """ + parquet_table = pq.read_table(parquet_file) # Read back Parquet File as a Table + #pq.write_to_dataset(parquet_table, root_path='starships', partition_cols=['created']) + pq.write_to_dataset(parquet_table, root_path='starships', partition_cols=['year', 'month', 'day'], flavor='spark') + dataset = pq.ParquetDataset('starships') + return dataset + + +if __name__ == '__main__': + + # Basics of get request, save to DataFrame, PyArrow Table, Parquet File + df_deathstar = get_api_endpoint('https://swapi.co/api/starships/9/') + df_deathstar = df_add_partition_columns(df_deathstar, 'created') + parquet_deathstar_table = df_to_parquet_table(df_deathstar) + + # Write to and Read from Partitioned Datasets + write_parquet_table_as_partitioned_dataset('deathstar.parquet') + print("Done") diff --git a/pyarrow/starships/year=2014/month=12/day=10/69a1f1872d854c39a69ff24c0820889c.parquet b/pyarrow/starships/year=2014/month=12/day=10/69a1f1872d854c39a69ff24c0820889c.parquet new file mode 100644 index 0000000..798be30 Binary files /dev/null and b/pyarrow/starships/year=2014/month=12/day=10/69a1f1872d854c39a69ff24c0820889c.parquet differ diff --git a/pyarrow/starships/year=2014/month=12/day=10/7026c2af982a4564a92c7602b43976cf.parquet b/pyarrow/starships/year=2014/month=12/day=10/7026c2af982a4564a92c7602b43976cf.parquet new file mode 100644 index 0000000..b242a4d Binary files /dev/null and b/pyarrow/starships/year=2014/month=12/day=10/7026c2af982a4564a92c7602b43976cf.parquet differ diff --git a/pyarrow/starships/year=2014/month=12/day=10/94d8ecfeb23642c4a73779e0427ce653.parquet b/pyarrow/starships/year=2014/month=12/day=10/94d8ecfeb23642c4a73779e0427ce653.parquet new file mode 100644 index 0000000..4162568 Binary files /dev/null and b/pyarrow/starships/year=2014/month=12/day=10/94d8ecfeb23642c4a73779e0427ce653.parquet differ diff --git a/pyarrow/starships/year=2014/month=12/day=10/9ef633fb96d04eebb9a20c845a620b97.parquet b/pyarrow/starships/year=2014/month=12/day=10/9ef633fb96d04eebb9a20c845a620b97.parquet new file mode 100644 index 0000000..c37a70d Binary files /dev/null and b/pyarrow/starships/year=2014/month=12/day=10/9ef633fb96d04eebb9a20c845a620b97.parquet differ diff --git a/pyglet/helloworld.py b/pyglet/helloworld.py new file mode 100644 index 0000000..723353c --- /dev/null +++ b/pyglet/helloworld.py @@ -0,0 +1,31 @@ +import pyglet +from pyglet.window import key +from pyglet.window import mouse + + +window = pyglet.window.Window() +label = pyglet.text.Label( + 'Hello world', x=window.width//2, y=window.height//2, + anchor_x='center', anchor_y='center') + + +@window.event +def on_draw(): + window.clear() + label.draw() + + +@window.event +def on_key_press(symbol, modifiers): + """ Detect Key Press """ + if symbol == key.A: + print "The 'A' key was pressed." + elif symbol == key.LEFT: + print "The left arrow key was pressed." + elif symbol == key.RIGHT: + print "The right arrow key was pressed." + elif symbol == key.ENTER: + print "The enter key was pressed" + + +pyglet.app.run() diff --git a/rabbitmq/example_producer_consumer.py b/rabbitmq/example_producer_consumer.py new file mode 100644 index 0000000..fa0ab60 --- /dev/null +++ b/rabbitmq/example_producer_consumer.py @@ -0,0 +1,37 @@ +import pika + + +credentials = pika.PlainCredentials('guest', 'guest') +parameters = pika.ConnectionParameters(host="localhost", + credentials=credentials, + heartbeat_interval=600) + + +def test_simple_producer_consumer(): + print("Creating Connection, testing simple producer and consumer") + + # Create Connection + connection = pika.BlockingConnection(parameters) + channel = connection.channel() + channel.queue_declare(queue='hello-queue') + + # Publish to Queue + print("About to publish to queue") + channel.basic_publish(exchange='', routing_key='hello-queue', body='test') # publish to default exchange + print "After Channel Publish" + + # Consume from Queue and run callback + channel.basic_consume(callback, queue='hello-queue', no_ack=True) # acknowledge + print(' [*] Waiting for messages. To exit press CTRL+C') + channel.start_consuming() + connection.close() + + +def callback(ch, method, properties, body): + """ Callback from Consumer """ + print(" [x] Received %r" % body) + + +if __name__ == '__main__': + test_simple_producer_consumer() + diff --git a/requirements.txt b/requirements.txt index 283aea4..b6286b4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ PyYAML==3.10 -UNKNOWN==0.0.0 beautifulsoup4==4.3.2 chardet==2.2.1 colorama==0.2.7 @@ -12,7 +11,6 @@ lxml==3.2.4 matplotlib==1.3.1 mechanize==0.2.5 mock==1.0.1 -mysql-connector-python==1.0.12 nltk==2.0.4 numpy==1.7.1 openpyxl==1.7.0 diff --git a/selenium/simple_search.py b/selenium/simple_search.py new file mode 100644 index 0000000..a5888b6 --- /dev/null +++ b/selenium/simple_search.py @@ -0,0 +1,15 @@ +from selenium import webdriver +from selenium.webdriver.common.keys import Keys + + +#driver = webdriver.Firefox() +driver = webdriver.Chrome() +driver.get("http://www.python.org") +assert "Python" in driver.title +elem = driver.find_element_by_name("q") +elem.clear() +elem.send_keys("pycon") +elem.send_keys(Keys.RETURN) +assert "No results found." not in driver.page_source +driver.close() +