├── .gitignore ├── CATEGORIES ├── README.md ├── abc └── interface.py ├── algorythms └── binary_search.py ├── commandline ├── options_example.py └── title_bar.py ├── core ├── arguments.py ├── argv.py ├── columnar_display.py ├── descriptors │ ├── descriptors.py │ └── descriptors_example.py ├── dicts101.py ├── filter_map_reduce.py ├── generatelist.py ├── generators.py ├── introspection101.py ├── listit.py ├── lists101.py ├── looptimes.py ├── reversetext.py ├── show_getopt.py ├── show_optparse.py ├── simple_metaclass.py ├── sortlistbygroup.py ├── stringcontainsstringtest.py ├── stringnumberconcat.py ├── tee.py └── threaded_server.py ├── cryptography ├── create_random_crap.py ├── dumbcrypt.py ├── encrypt.py ├── hexmap.py ├── hexmap2.py └── randpool.py ├── csv ├── csv101.csv ├── csv101.py ├── csv2dict.csv ├── csv2dict.py ├── vmstat-reader.py └── vmstat.log ├── data_types ├── int_to_binary.py ├── list_comparison.py ├── mappingdoc.py └── string_to_binary.py ├── database ├── couch │ └── desktop │ │ ├── addattachment.py │ │ ├── addrecord.py │ │ ├── createdatabase.py │ │ ├── deleterecords.py │ │ ├── fetchrecords.py │ │ └── listenchanges.py └── psql_notify.py ├── decorators ├── decorator.py ├── decorator2.py ├── decorator_example.py ├── django_login_decorators.py ├── parameterized_decorator.py └── wraps_decorator.py ├── descriptors ├── descriptors.py └── descriptors_example.py ├── django ├── JSONField.py ├── TaskViewMixin.py ├── add_attributes_to_formfields.py ├── admin.py ├── class_based_view.py ├── django-email.backend.py ├── email_as_username_13.py ├── get_generic_relations.py ├── query_count_decorator.py ├── sslmiddleware.py ├── syncdata.py ├── taskbaseview.py ├── test_settings_manager.py ├── testing │ ├── fast_test_runner.py │ └── remove_media_testrunner.py ├── urls_template.py └── usersonline.py ├── dynamic_classes ├── class_property.py └── get_all_class_properties.py ├── email ├── EmailServer.py ├── mail.py └── simpleemail.py ├── exercises └── fibonacci.py ├── expert_programming_book_code └── scripts │ ├── ._.DS_Store │ ├── all-scripts.txt │ ├── appendixA-scripts.txt │ ├── appendixB-scripts.txt │ ├── chapter1-scripts.txt │ ├── chapter10-scripts.txt │ ├── chapter11-scripts.txt │ ├── chapter12-scripts.txt │ ├── chapter13-scripts.txt │ ├── chapter14-scripts.txt │ ├── chapter3-scripts.txt │ ├── chapter4-scripts.txt │ ├── chapter5-scripts.txt │ ├── chapter6-scripts.txt │ ├── chapter7-scripts.txt │ ├── chapter8-scripts.txt │ ├── chapter9-scripts.txt │ └── introduction-scripts.txt ├── feedparser └── show_feedparser.py ├── functools ├── autopartial.py ├── recursion.py └── recursive.py ├── generators └── generator1.py ├── geo_tools ├── get_google_geolocation.py └── haversine.py ├── get-site-packages.sh ├── gpodder └── extension_example.py ├── http └── post_binary_data_via_http.py ├── idomatic └── forloop_else.py ├── inspect └── getmembers.py ├── introspection ├── introspect.py └── print_function_info.py ├── itertools └── itertools_example.py ├── lambda └── lambda_example.py ├── lexical_parsing └── stringparse.py ├── localization └── locale.py ├── logging └── logger_setup.py ├── meta_programming └── proxy_obj.py ├── mocking └── nested_calls.py ├── musicbrainz_picard └── plugins │ └── nofeaturing.py ├── ncurses └── ncurses_example.py ├── network └── proxy.py ├── notify-osd └── showbubble.py ├── objects └── sort_object_by_element.py ├── os ├── envvar.py ├── find_file.py ├── list_directory.py ├── parse_files.py ├── path │ ├── add_cwd_to_syspath │ └── add_cwd_to_syspath.py ├── projectpath.py └── projectpath.pyc ├── parse_files.py ├── path ├── add_cwd_tosyspath.py ├── filtered_walkdir.py ├── get_current_parent.py └── list_directory.py ├── patterns ├── borg.py ├── facade.py ├── factory.py ├── observer.py ├── patternmatching.py └── pubsub.py ├── pexpect └── pexpect_example.py ├── pmtool └── create_change_record.py ├── postgres └── connect_database.py ├── recipes ├── reverse.py └── simple_logging_setup.py ├── regex ├── simple_phone.py └── tester.py ├── scripts └── syspath.sh ├── socket ├── socket_client.py └── socketserver.py ├── sorting └── sort_iter.py ├── sphinx ├── autodoc_example.py └── django_autodoc_models.py ├── string └── regex_strip.py ├── system └── vmstat-reader.py ├── tarfile ├── createtar1.py └── opentar.py ├── templates ├── script_skeleton.py └── sheshebang.py ├── test-name-conflicts.sh ├── text └── parse_files.py ├── type_conversions ├── hextext_to_integers.py ├── list_flatterner.py └── list_to_string.py ├── unicode ├── to_unicode_with_unknown_charset.py └── utf8_example.py ├── unittest ├── nested_calls.py ├── test_method.py └── unittests.py ├── waterworks ├── AIMA.py ├── ClusterMetrics.py ├── ExitCodes.py ├── FigUtil.py ├── FunctionPickler.py ├── HeapQueue.py ├── Histogram.py ├── IntRange.py ├── IntShelve.py ├── LazyList.py ├── MANIFEST ├── PrecRec.py ├── Probably.py ├── QuickColorize.py ├── README.html ├── Selectron.py ├── Tailer.py ├── TeXTable.py ├── TerminalTitle.py ├── ThreadedJobs.py ├── TkGeomSavers.py ├── ansi.py ├── cookbook │ ├── Memoize.py │ ├── __init__.py │ ├── backwardsreader.py │ ├── baseconverter.py │ ├── cartesianproduct.py │ ├── defaultdict.py │ ├── frange.py │ ├── hungarian_method.py │ ├── pcolor.py │ ├── pdbhelper.py │ ├── print_exc_plus.py │ ├── progress.py │ ├── pushback_wrapper.py │ ├── reload_all_mods.py │ ├── terminal_size.py │ └── zip_exc.py ├── diffprint.py ├── iterextras.py ├── rad_util.py ├── robust_apply.py ├── setup.py ├── test │ └── test_new_isinstance.py ├── topsort.py ├── vimdiff.py └── waterworks │ ├── Dictionaries.py │ ├── Files.py │ ├── Numbers.py │ ├── Processes.py │ ├── Sequences.py │ ├── Streams.py │ ├── Strings.py │ ├── Tools.py │ ├── __init__.py │ └── new_isinstance.py ├── web ├── color.py └── simple_flask_server.py ├── webkit ├── webkit_browser.py ├── webkit_button.py ├── webkit_preso.py ├── webkit_table-business.css ├── webkit_table-colourful.css ├── webkit_table-plain.css ├── webkit_table-rounded.css ├── webkit_table.csv └── webkit_table.py ├── wsgi └── rewritingproxy.py ├── wxBased ├── Data │ └── file.db ├── create_phone_book.py ├── phone_book.py └── wxframe.py ├── xml └── parse.py └── xmpp └── xmpp_client.py /.gitignore: -------------------------------------------------------------------------------- 1 | ### /Users/brent/.gitignore-boilerplates/Python.gitignore 2 | .DS_Store 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Packages 9 | *.egg 10 | *.egg-info 11 | dist 12 | build 13 | eggs 14 | parts 15 | bin 16 | var 17 | sdist 18 | develop-eggs 19 | .installed.cfg 20 | lib 21 | lib64 22 | __pycache__ 23 | 24 | # Installer logs 25 | pip-log.txt 26 | 27 | # Unit test / coverage reports 28 | .coverage 29 | .tox 30 | nosetests.xml 31 | 32 | # Translations 33 | *.mo 34 | 35 | # Mr Developer 36 | .mr.developer.cfg 37 | .project 38 | .pydevproject 39 | 40 | 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #Welcome to the Python Snippets archive! 2 | 3 | __(ok, **a** python snippet archive)__ 4 | 5 | This collection was originally rescued from the abandoned project Acire, and I have been adding to it sporadically ever since. 6 | I hope to build some sort of browser/search soon. 7 | 8 | These are all release under GPL so steal at will. 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /abc/interface.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | from datetime import datetime 3 | 4 | 5 | class ApiInterface(object): 6 | """ Define the interface that all implementations of the interface must implement """ 7 | 8 | __metaclass__ = ABCMeta 9 | 10 | @abstractmethod 11 | def get_provider_lots(self, urls): 12 | pass 13 | 14 | @abstractmethod 15 | def fetch_rates(self, master_dict): 16 | pass 17 | 18 | 19 | class BaseAPI(object): 20 | """ A common parent class that contains utility functions and initalizes variables """ 21 | 22 | assumptions = { 23 | 'prefix': '', 24 | 'postfix': '' 25 | } 26 | 27 | def __init__(self): 28 | self.lots = {} 29 | self.start_time = datetime.now() 30 | 31 | 32 | class MyAPI(BaseAPI, ApiInterface): 33 | """ A specific implementation of the parent class that implements the interface 34 | It will not let you instantiate the class if you have not implemented all abstract methods 35 | """ 36 | def get_provider_lots(self, urls): 37 | provider_lots = list() 38 | return provider_lots 39 | 40 | def fetch_rates(self, master_dict): 41 | rates = list() 42 | return rates 43 | 44 | 45 | myapi = MyAPI() 46 | print(vars(myapi)) 47 | print(dir(myapi)) 48 | -------------------------------------------------------------------------------- /algorythms/binary_search.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | __name__ = 'Binary Search' 4 | __category__ = 'algorythms' 5 | __type__ = 'example' 6 | 7 | 8 | def search(x, nums): 9 | low = 0 10 | high = len(nums) - 1 11 | while low <= high: 12 | mid = (low + high) / 2 13 | item = nums[mid] 14 | if x == item: 15 | return mid 16 | elif x < item: 17 | high = mid - 1 18 | else: 19 | low = mid + 1 20 | return -1 21 | 22 | def main(): 23 | x = input('What is the number?') 24 | nums = range(1,101) 25 | print('Element is at index:%s' % search(x, nums)) 26 | 27 | if __name__ == '__main__': 28 | main() -------------------------------------------------------------------------------- /commandline/options_example.py: -------------------------------------------------------------------------------- 1 | #/bin/env python 2 | 3 | import getopt 4 | import sys 5 | import os 6 | 7 | def main(argv): 8 | # check arguments and options 9 | try: 10 | opts, args = getopt.getopt(argv, "hd:", ["help", "database"]) 11 | # show usage printout if something is bad 12 | for opt, arg in opts: 13 | if opt in ("-h, --help"): 14 | printUsage() 15 | sys.exit(2) 16 | if opt in ("-d, --db"): 17 | print "Supposed to create db ''" + arg +"''" 18 | MAIN_PROGRAM_METHOD(args[0]) 19 | except getopt.GetoptError: 20 | printUsage(True) 21 | sys.exit(2) 22 | except IndexError: 23 | printUsage(True) 24 | sys.exit(2) 25 | -------------------------------------------------------------------------------- /commandline/title_bar.py: -------------------------------------------------------------------------------- 1 | # Title_Bar.py 2 | # DEMO to show how to write some text into the Title Bar... 3 | # Original code, (C)2013, B.Walker, G0LCU. 4 | # Issued as Public Domain. 5 | # 6 | # Tested on OSX 10.7.5, Debian 6.0.x and PCLinuxOS 2009 using the default 7 | # terminals running at least Python 2.5.2 to 3.3.2... 8 | # 9 | # A snapshot of the Title Bar here:- 10 | # 11 | # http://wisecracker.host22.com/public/Title_Bar.jpg 12 | # To launch the DEMO enter:- 13 | # 14 | # >>> exec(open("/your/full/path/to/Title_Bar.py").read()) 15 | # 16 | import time 17 | # 18 | print("Writing to the Title Bar...") 19 | print("\x1B]0;THIS IS A TITLE BAR DEMO...\x07") 20 | print("Wait for 5 seconds...") 21 | time.sleep(5) 22 | print("\x1B]0;\x07") 23 | print("Title Bar returned to normal...") 24 | # 25 | # End of Title_Bar.py DEMO... 26 | # Enjoy finding simple solutions to often very difficult problems... 27 | -------------------------------------------------------------------------------- /core/argv.py: -------------------------------------------------------------------------------- 1 | 2 | # [SNIPPET_NAME: Command line arguments] 3 | # [SNIPPET_CATEGORIES: Python Core] 4 | # [SNIPPET_DESCRIPTION: Prints each command line argument per line] 5 | # [SNIPPET_AUTHOR: Tiago Boldt Sousa ] 6 | # [SNIPPET_LICENSE: GPL] 7 | 8 | import sys 9 | 10 | for x in sys.argv: 11 | print x 12 | -------------------------------------------------------------------------------- /core/columnar_display.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Columnar display] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: Print a list of arguments in several columns] 6 | # [SNIPPET_AUTHOR: Akkana Peck ] 7 | # [SNIPPET_LICENSE: GPL] 8 | def columnar_display(list, pagewidth=77) : 9 | maxlen = 0 10 | for item in list : 11 | l = len(str(item)) 12 | if l > maxlen : 13 | maxlen = l 14 | maxlen += 2 # space it out a little more 15 | numcol = int(pagewidth / maxlen) 16 | 17 | i = 0 18 | for item in list : 19 | print '{0:{1}}'.format(item, maxlen), 20 | i += 1 21 | if i % numcol == 0 : 22 | print '\n', 23 | 24 | list = [ 'Python Core', 'Python VTE', 'Regular Expression', 'socket', 25 | 'tarfile', 'Testing', 'threading', 'twitter', 'unittest', 26 | 'Upstart', 'Webkit', 'Zeitgeist' ] 27 | 28 | columnar_display(list) 29 | -------------------------------------------------------------------------------- /core/descriptors/descriptors.py: -------------------------------------------------------------------------------- 1 | class NameDescriptor(object): 2 | 3 | def __get__(self, instance, owner): 4 | return instance._value 5 | 6 | def __set__(self, instance, value): 7 | ucase_name = value.upper() 8 | instance._value = ucase_name 9 | 10 | def __delete__(self, instance): 11 | del(instance._value) 12 | 13 | def __len__(self, instance): 14 | return len(instance._value) 15 | 16 | 17 | 18 | class Person(object): 19 | 20 | name = NameDescriptor() 21 | 22 | 23 | 24 | if __name__ == '__main__': 25 | n = Person() 26 | n.name = 'Brent' 27 | n.hat = 'bowler' 28 | print(dir(n)) 29 | print(vars(n)) 30 | print(n.name) 31 | print(n.hat) 32 | print(len(n.name)) 33 | -------------------------------------------------------------------------------- /core/descriptors/descriptors_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | class MyPropertyDescriptor(object): 5 | """ 6 | This object defines how the property "neighborhoods" will be used on 7 | any object that uses it. 8 | """ 9 | 10 | def __get__(self, instance, owner): 11 | # Use a private variable to avoid calling the getter recursively 12 | if hasattr(instance, '_greetings'): 13 | return instance._greetings 14 | else: 15 | return None 16 | 17 | def __set__(self, instance, value): 18 | # Custom code can go here to transform the value on set 19 | # Code is slightly naive since it assume value is string 20 | instance._greetings = value.upper() 21 | 22 | def __delete__(self, instance): 23 | del instance._neighborhoods 24 | 25 | 26 | class ObjectWithDescriptor(object): 27 | neighborhoods = MyPropertyDescriptor() 28 | 29 | def __init__(self): 30 | self.other_property = None 31 | 32 | def print_values(self): 33 | print(self.neighborhoods) 34 | print(self.other_property) 35 | 36 | if __name__ == '__main__': 37 | owd = ObjectWithDescriptor() 38 | owd.print_values() 39 | owd.neighborhoods = 'hello monkey' 40 | owd.other_property = 'hello banana' 41 | owd.print_values() 42 | -------------------------------------------------------------------------------- /core/filter_map_reduce.py: -------------------------------------------------------------------------------- 1 | # [SNIPPET_NAME: Filter, Map & Reduce of lists] 2 | # [SNIPPET_CATEGORIES: Python Core] 3 | # [SNIPPET_DESCRIPTION: Simple examples to show common features in everyday work with lists] 4 | # [SNIPPET_AUTHOR: Benjamin Klueglein ] 5 | # [SNIPPET_LICENSE: GPL] 6 | 7 | numbers = range(1, 20, 1) # Numbers from 1 to 20 8 | ######################### 9 | # Filtering of lists: 10 | # Pick a amount of items which match a certain condition 11 | ######################### 12 | # e.g. Get all odd numbers 13 | odd_numbers = filter(lambda n: n % 2, numbers) 14 | print odd_numbers 15 | # prints [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] 16 | ######################### 17 | 18 | ######################### 19 | # Mapping of lists: 20 | # Apply a function to each item and return the result of each invocation in a list 21 | ######################### 22 | # Calculate the square of two for each number 23 | squared_numbers = map(lambda n: n ** 2, numbers) 24 | # Alternate approach: 25 | squared_numbers = [n ** 2 for n in numbers] 26 | print squared_numbers 27 | # prints [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361] 28 | ######################### 29 | 30 | ######################### 31 | # Reducing of lists 32 | # Apply a function of two arguments cumulatively to the items of a sequence, 33 | # from left to right, so as to reduce the sequence to a single value. 34 | # (Taken from reduce docstring) 35 | ######################### 36 | # Sum up all numbers 37 | sum_of_numbers = reduce(lambda n, m: n + m, numbers) 38 | print sum_of_numbers 39 | # prints 190 40 | ######################### 41 | -------------------------------------------------------------------------------- /core/generatelist.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Generate modified list] 4 | # [SNIPPET_CATEGORIES: core] 5 | # [SNIPPET_DESCRIPTION: How to generate a modified list by iterating over each element of another list] 6 | # [SNIPPET_AUTHOR: Scott Ferguson ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | first_list = range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 10 | 11 | result = [x + 1 for x in first_list] # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 12 | 13 | print result 14 | -------------------------------------------------------------------------------- /core/generators.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # [SNIPPET_NAME: Writing generators] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: How to write a function you can iterate on] 6 | # [SNIPPET_AUTHOR: Josh Holland 7 | # [SNIPPET_LICENSE: GPL] 8 | # [SNIPPET_DOCS: http://docs.python.org/reference/simple_stmts.html#the-yield-statement, http://www.python.org/dev/peps/pep-0255/] 9 | 10 | def fibonacci(start=(0, 1), stop=10): 11 | """Generate an iterator on the first 'stop' Fibonacci numbers, starting with 12 | the optional pair of numbers given as an argument. 13 | 14 | """ 15 | a, b = start 16 | while stop: 17 | yield a # the magic happens here. 18 | # yield acts like return, but control resumes there 19 | # on the next iteration 20 | a, b = b, a + b 21 | stop -= 1 22 | 23 | for num in fibonacci(): 24 | print num 25 | -------------------------------------------------------------------------------- /core/introspection101.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- encoding: utf8 -*- 3 | # 4 | # [SNIPPET_NAME: Introspection 101] 5 | # [SNIPPET_CATEGORIES: Python Core] 6 | # [SNIPPET_DESCRIPTION: Quick example of Python introspection capabilities] 7 | # [SNIPPET_AUTHOR: Gonzalo Núñez ] 8 | # [SNIPPET_LICENSE: GPL] 9 | import types 10 | import sys 11 | import os 12 | 13 | def query( item ): 14 | ''' Retrieve all attributes from the given element and display their name and docstring.''' 15 | if None == item: 16 | return 17 | 18 | itemDir = dir( item ) 19 | item_name = item.__name__ 20 | 21 | for elem in itemDir: 22 | element_name = str(elem) 23 | elment_doc = '' 24 | # 25 | # Get the element 26 | # this is equivalent to writing: 'element = item.' (eg. if elem = '__doc__', item.__doc__) 27 | # 28 | element = getattr( item, elem ) 29 | # 30 | # if element has a __name__ attribute, get it and store it in element_name 31 | # this is equivalent to writing: 'element_name = element.__name__' 32 | # 33 | if hasattr( element, '__name__' ): 34 | element_name = getattr( element, '__name__' ) 35 | element_name = element.__name__ 36 | # 37 | # if element has a __doc__ attribute, get it and store it in element_doc 38 | # 39 | if hasattr( element, '__doc__' ): 40 | element_doc = element.__doc__ 41 | 42 | print 43 | print "%s.%s [%s]\n\n%s" %( item_name, element_name, type( elem ), element_doc ) 44 | print 45 | print 46 | 47 | query( os ) 48 | -------------------------------------------------------------------------------- /core/listit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Iterating Numeric Lists] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: How to iterate a list with numbers inside] 6 | # [SNIPPET_AUTHOR: Jono Bacon ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | items = [ 1, 2, 3, 4, 5 ] 10 | 11 | for i in items: 12 | print i 13 | -------------------------------------------------------------------------------- /core/lists101.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Lists 101] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: Basic and not so basic list operations] 6 | # [SNIPPET_AUTHOR: Bruno Girin ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | # This snippet demonstrates how the basics on lists: how to create, add, 10 | # remove items, get items or slices, sort, etc. 11 | 12 | # 13 | # First, let's create simple list 14 | # 15 | print "Create a simple list" 16 | simpleList = ["Karmic", "Lucid", "Hardy", "Jaunty", "Intrepid"] 17 | # print it 18 | print simpleList 19 | # 20 | # Interrogate the list 21 | # 22 | print "\nInterrogate the list" 23 | # get item 3: lists start counting at 0 so it should be Jaunty 24 | print simpleList[3] 25 | # we can also get a slice 26 | print simpleList[2:4] 27 | # get the first three items 28 | print simpleList[:3] 29 | # or all items from number index 3 (which is the fourth item) to the end 30 | print simpleList[3:] 31 | # we can also take every other item, as a slice is defined 32 | # like this: start:stop:step 33 | print simpleList[::2] 34 | # get the length of the list 35 | print len(simpleList) 36 | # we can get the index of an item in the list 37 | print simpleList.index("Hardy") 38 | # and when the list doesn't contain the item, we get an error that we can catch 39 | try: 40 | print simpleList.index("Feisty") 41 | except ValueError: 42 | print "The list doesn't contain the item Feisty" 43 | # 44 | # Modify the list 45 | # 46 | print "\nModify the list" 47 | # add another item 48 | simpleList.append("Twisty") 49 | print simpleList 50 | # oops! let's sort this out by replacing in place 51 | simpleList[5] = "Gutsy" 52 | print simpleList 53 | # extend the list with another one 54 | otherList = ["Edgy", "Breezy"] 55 | simpleList.extend(otherList) 56 | print simpleList 57 | # remove an item from the list (Hardy should not be in the list anymore) 58 | del simpleList[2] 59 | print simpleList 60 | # insert an item in the middle of the list 61 | simpleList.insert(4, "Hardy") 62 | print simpleList 63 | # remove an item by its value rather than its index 64 | simpleList.remove("Edgy") 65 | print simpleList 66 | # 67 | # Create modified copies of the list 68 | # 69 | print "\nCreate modified copies of the list" 70 | # sort it 71 | print sorted(simpleList) 72 | # join it to produce a custom print 73 | print ' => '.join(sorted(simpleList)) 74 | # lists can contain the same item several times so if we add Lucid again: 75 | simpleList.append("Lucid") 76 | # we have it twice in the list (easier to see if we sort it) 77 | print sorted(simpleList) 78 | # but we can get round that by transforming it into a set, which is a list 79 | # with no duplicates; and of course we can also sort the set 80 | print sorted(set(simpleList)) 81 | # 82 | # Iterate over the list 83 | # 84 | print "\nIterate over the list" 85 | for i in simpleList: 86 | print i.upper() 87 | # but if we want to create another list by applying the same expression to 88 | # each item, we can use a list comprehension 89 | upList = [i.upper() for i in sorted(set(simpleList))] 90 | print upList 91 | 92 | -------------------------------------------------------------------------------- /core/looptimes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Numeric loop] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: Loop a specified set of times] 6 | # [SNIPPET_AUTHOR: Jono Bacon ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | for x in range(10): 10 | print x 11 | 12 | -------------------------------------------------------------------------------- /core/reversetext.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Reversing strings] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: How to reverse the contents of a string] 6 | # [SNIPPET_AUTHOR: Scott Ferguson ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | text = ['Hello', 'world', 'Go hang a salami I\'m a lasagna hog'] 10 | 11 | for word in text: 12 | print word[::-1] 13 | -------------------------------------------------------------------------------- /core/show_getopt.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Show getopt] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: GNU style arguments via getopt] 6 | # [SNIPPET_AUTHOR: Jurjen Stellingwerff ] 7 | # [SNIPPET_LICENSE: GPL] 8 | import getopt, sys 9 | 10 | program = "getopt" 11 | version = "0.1" 12 | 13 | def do_version(): 14 | """ Show version information of this program """ 15 | print program+" "+version 16 | print """Copyright (C) 2010 Jurjen Stellingwerff 17 | Lisense GPL: GNU GPL version 2 or later . 18 | This is free software: you are free to change and redistribute it. 19 | There is NO WARRENTY, to the extent permittable by law. 20 | 21 | Written by Jurjen Stellingwerff.""" 22 | exit() 23 | 24 | def do_help(): 25 | """ Prints a list of arguments for this program. Normally you would change this function to include more info like examples and another program descriptor. """ 26 | print "Usage: "+program+" [OPTION]... [REST]" 27 | print """Demonstrates python code for GNU style arguments. 28 | Mandatory arguments to long options are mandatory for short options too. 29 | -f, --file=FILE display this file 30 | -h, -?, --help display this help and exit 31 | -q, --quiet don't print status messages to stdout 32 | --this-is-a-bit-too-long-argument=SOMETHING 33 | when arguments get too long the line splits 34 | and lines can contain newlines 35 | --version output version information and exit 36 | 37 | Report arguments bugs to jurjen@stwerff.xs4all.nl 38 | Python-snippets homepage: """ 39 | exit() 40 | 41 | def main(): 42 | files = [] 43 | try: 44 | opts, args = getopt.gnu_getopt(sys.argv[1:], "?qhf:", ["help", "file=", "version", "this-is-a-bit-too-long-argument="]) 45 | except getopt.GetoptError, err: 46 | # print help information and exit: 47 | print str(err) # will print something like "option -a not recognized" 48 | sys.exit(2) 49 | output = None 50 | verbose = True 51 | for option, argument in opts: 52 | if option in ("-f", "--file"): 53 | files.append(argument) 54 | elif option in ("-h", "-?", "--help"): 55 | do_help() 56 | elif option in ("--version"): 57 | do_version() 58 | elif option in ("-q", "--quiet"): 59 | verbose = False 60 | elif option in ("--this-is-a-bit-too-long-argument"): 61 | print argument 62 | else: 63 | assert False, "unhandled option" 64 | 65 | for filename in files: 66 | if verbose: 67 | print "Printing file",filename 68 | try: 69 | f = open(filename, 'r') 70 | for line in f.readlines(): 71 | print line.rstrip() 72 | except IOError: 73 | print "No such file: '"+filename+"'" 74 | 75 | for text in args: 76 | print "Rest:",text 77 | 78 | # demonstrate the help page when there are no parameters given 79 | # not very useful in an actual program 80 | 81 | if len(sys.argv)==1: 82 | do_help() 83 | 84 | if __name__ == "__main__": 85 | main() 86 | 87 | -------------------------------------------------------------------------------- /core/show_optparse.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Show optparse] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: GNU style arguments via optparse] 6 | # [SNIPPET_AUTHOR: Jurjen Stellingwerff ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | # example show_optparse.py 10 | 11 | import optparse, sys 12 | 13 | program = "show_optparse.py" 14 | # usefull when the program alters from the normal python file 15 | 16 | version = """%prog 0.1 17 | Copyright (C) 2010 Jurjen Stellingwerff 18 | Lisense GPL: GNU GPL version 2 or later . 19 | This is free software: you are free to change and redistribute it. 20 | There is NO WARRENTY, to the extent permittable by law. 21 | 22 | Written by Jurjen Stellingwerff. 23 | """ 24 | 25 | usage = "Usage: %prog [OPTION]... [REST]" 26 | description = "Demonstrates python code for GNU style arguments." 27 | 28 | def do_single(option, opt, value, parser): 29 | print value 30 | 31 | parser = optparse.OptionParser(usage=usage, version=version, description=description, prog=program) 32 | parser.add_option("-?", action="help", help=optparse.SUPPRESS_HELP) 33 | parser.add_option("-f", "--file", dest="filename", action="append", 34 | help="display this file", metavar="FILE") 35 | parser.add_option("-q", "--quiet", 36 | action="store_false", dest="verbose", default=True, 37 | help="don't print status messages to stdout") 38 | parser.add_option("--this-is-a-bit-too-long-argument", metavar="SOMETHING", 39 | help="when arguments get too long the line splits, this also works for the longer help strings") 40 | parser.add_option("-s", metavar="SOMETHING", action="callback", callback=do_single, type="string", 41 | help="single token argument with a parameter") 42 | group = optparse.OptionGroup(parser, "\nReport arguments bugs to jurjen@stwerff.xs4all.nl\nPython-snippets homepage: ") 43 | parser.add_option_group(group) 44 | 45 | (options, args) = parser.parse_args() 46 | 47 | if options.this_is_a_bit_too_long_argument: 48 | print options.this_is_a_bit_too_long_argument 49 | 50 | if options.filename: 51 | for filename in options.filename: 52 | if options.verbose: 53 | print "Printing file",filename 54 | try: 55 | f = open(filename) 56 | for line in f.readlines(): 57 | print line.rstrip() 58 | except IOError: 59 | print "No such file: "+options.filename 60 | 61 | for rest in args: 62 | print "Rest:",rest 63 | 64 | # demonstrate the help page when there are no parameters given 65 | # not very useful in an actual program 66 | 67 | if len(sys.argv)==1: 68 | parser.print_help() 69 | 70 | -------------------------------------------------------------------------------- /core/sortlistbygroup.py: -------------------------------------------------------------------------------- 1 | # [SNIPPET_NAME: Sort list by group] 2 | # [SNIPPET_CATEGORIES: Python Core] 3 | # [SNIPPET_DESCRIPTION: This function takes a list of string and sorts them based on their similarity. The percent at which they match can be defined in the second parameter (default 75%)] 4 | # [SNIPPET_AUTHOR: John Turek ] 5 | # [SNIPPET_LICENSE: GPL] 6 | 7 | 8 | def sortByGroup(lst, percent=75): 9 | groups = [] 10 | for item in lst: 11 | match = False 12 | 13 | for g in xrange(len(groups)): 14 | group = groups[g] 15 | parent = group[0] 16 | points = 0.0 17 | 18 | try: 19 | for x in xrange(len(parent)): 20 | if parent[x] == item[x]: 21 | points += 1 22 | 23 | if (points / len(parent)) * 100 >= percent: 24 | group.append(item) 25 | group.sort() 26 | match = True 27 | except: 28 | pass 29 | 30 | if not match: 31 | groups.append([item]) 32 | 33 | return groups 34 | 35 | 36 | # Example: 37 | random = [ 38 | 'bob1', 39 | 'frank2', 40 | 'bob3', 41 | 'joe2', 42 | 'frank1', 43 | 'bob2', 44 | 'joe1', 45 | 'joe3' 46 | ] 47 | groups = sortByGroup(random) 48 | for g in groups: 49 | for i in g: 50 | print i 51 | print '-' * 30 52 | -------------------------------------------------------------------------------- /core/stringcontainsstringtest.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: String contains string test] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: Test if a string contains another string] 6 | # [SNIPPET_AUTHOR: Jono Bacon ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | 10 | my_string = "abcdef" 11 | 12 | if "abc" in my_string: 13 | has_abc = True 14 | 15 | if has_abc == True: 16 | print "String contains string." 17 | -------------------------------------------------------------------------------- /core/stringnumberconcat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: String and number concatenation] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: Concatenate a string and a number] 6 | # [SNIPPET_AUTHOR: Jono Bacon ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | 10 | x = 1 11 | 12 | print str(x) + " is a string" 13 | print "%s is a string" % x 14 | print x, "is a string" 15 | -------------------------------------------------------------------------------- /core/tee.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Tee] 4 | # [SNIPPET_CATEGORIES: Python Core] 5 | # [SNIPPET_DESCRIPTION: Duplicate output to two file descriptors, like Unix tee] 6 | # [SNIPPET_AUTHOR: Akkana Peck ] 7 | # [SNIPPET_DOCS: http://shallowsky.com/blog/programming/python-tee.html] 8 | # [SNIPPET_LICENSE: GPL] 9 | import sys 10 | 11 | class tee : 12 | def __init__(self, _fd1, _fd2) : 13 | self.fd1 = _fd1 14 | self.fd2 = _fd2 15 | 16 | def __del__(self) : 17 | if self.fd1 != sys.stdout and self.fd1 != sys.stderr : 18 | self.fd1.close() 19 | if self.fd2 != sys.stdout and self.fd2 != sys.stderr : 20 | self.fd2.close() 21 | 22 | def write(self, text) : 23 | self.fd1.write(text) 24 | self.fd2.write(text) 25 | 26 | def flush(self) : 27 | self.fd1.flush() 28 | self.fd2.flush() 29 | 30 | if len(sys.argv) <= 1 : 31 | print "Usage:", sys.argv[0], 'outputfile' 32 | sys.exit(1) 33 | 34 | outputlog = open(sys.argv[1], "w") 35 | stderrsav = sys.stderr 36 | sys.stderr = tee(stderrsav, outputlog) 37 | 38 | print >> sys.stderr, "Test 1" 39 | sys.stderr.write("Test 2\n") 40 | -------------------------------------------------------------------------------- /cryptography/create_random_crap.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import random 3 | import string 4 | import hashlib 5 | import binascii 6 | 7 | def create_random_crap(num_chars): 8 | crap_string = '' 9 | seed = string.ascii_letters + string.digits + string.punctuation 10 | for x in range(0, num_chars): 11 | rnd = int(random.uniform(0, 52)) 12 | random_char = seed[rnd:rnd+1] 13 | crap_string += random_char 14 | return crap_string 15 | 16 | if __name__ == '__main__': 17 | x = create_random_crap(512) 18 | print(x) -------------------------------------------------------------------------------- /cryptography/dumbcrypt.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] 4 | 5 | def crypt(to_crypt): 6 | pass 7 | 8 | def decrypt(to_decrypt): 9 | pass 10 | 11 | 12 | if __name__ == '__main__': 13 | assert(len(alphabet == 26)) -------------------------------------------------------------------------------- /cryptography/encrypt.py: -------------------------------------------------------------------------------- 1 | from Crypto.Cipher import AES 2 | from Crypto import Random 3 | import sys 4 | from struct import pack, pack_into 5 | from ctypes import create_string_buffer 6 | 7 | test = "SixTeen Byte Dat" 8 | key = b'SixTeen Byte Key' 9 | 10 | 11 | def pad_data(unpadded_data): 12 | x = len(unpadded_data) 13 | closest_multiple = ((x / 16) + 1) * 16 14 | length_of_pad = closest_multiple - x 15 | pad = ' ' * length_of_pad 16 | padded_data = unpadded_data + pad 17 | return padded_data 18 | 19 | def ascii_to_bin(text): 20 | int_list = list() 21 | bin_list = list() 22 | buff = create_string_buffer(len(text)) 23 | for x in text: 24 | int_val = ord(x) 25 | bin_val = bin(int_val) 26 | bin_list.append(bin_val) 27 | return bin_list 28 | 29 | 30 | def main(text_to_crypt): 31 | assert(sys.byteorder == 'little') 32 | iv = Random.new().read(AES.block_size) 33 | #cipher = AES.new(buff, AES.MODE_CBC, iv) 34 | cipher = AES.new(key, AES.MODE_CFB, iv) 35 | 36 | msg = iv + cipher.encrypt(text_to_crypt) 37 | print(msg) 38 | 39 | denc = cipher.decrypt(msg) 40 | print(denc[16:]) 41 | 42 | if __name__ == '__main__': 43 | d = pad_data('I have a bunch of hair and its mostly brown') 44 | main(d) -------------------------------------------------------------------------------- /cryptography/hexmap.py: -------------------------------------------------------------------------------- 1 | from Crypto.Cipher import AES 2 | my_hex = "00" * 32 3 | import struct 4 | import sys 5 | 6 | 7 | def main(): 8 | my_binary = '' 9 | for (op, code) in zip(my_hex[0::2], my_hex[1::2]): 10 | x = op + code 11 | y = int(x, 16) 12 | z = struct.pack('B', y) 13 | my_binary += z 14 | print my_binary 15 | print(len(my_binary)) 16 | aes = AES.new(key=my_binary, mode=AES.MODE_CBC) 17 | encry = aes.encrypt('deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef') 18 | print(encry) 19 | print aes.decrypt(encry) 20 | happy_hex = '' 21 | for t in encry: 22 | h = '%x' % struct.unpack('B', t) 23 | if len(h) == 1: 24 | h = '0' + h 25 | happy_hex += h 26 | print happy_hex 27 | 28 | if __name__ == '__main__': 29 | if len(sys.args) < 1: 30 | print('bad paramters') 31 | if sys.args[0]: 32 | main() 33 | -------------------------------------------------------------------------------- /csv/csv101.csv: -------------------------------------------------------------------------------- 1 | "Line","Route Length","Tunnel","Open","Stations Served","No of Escalators","No of Lifts" 2 | "Bakerloo","23 kms","11 kms","12 kms",25,29,11 3 | "Central","74 kms","23 kms","51 kms",49,72,12 4 | "Circle","21 kms","18 kms","3 kms",27,"See Metropolitan","See Metropolitan" 5 | "District","64 kms","17 kms","47 kms",60,21,6 6 | "East London","8 kms","4 kms","4 kms",8,2,4 7 | "Hammersmith & City","27 kms","12 kms","15 kms",28,"See Metropolitan","See Metropolitan" 8 | "Jubilee","38 kms","19 kms","19 kms",27,127,34 9 | "Metropolitan","67 kms","10 kms","57 kms",34,30,2 10 | "Northern","58 kms","39 kms","19 kms",51,54,21 11 | "Piccadilly","71 kms","21 kms","50 kms",52,43,17 12 | "Victoria","21 kms","21 kms","0 kms",16,31,1 13 | "Waterloo & City","2 kms","2 kms","0 kms",2,"see Central","see Central" 14 | -------------------------------------------------------------------------------- /csv/csv101.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: CSV 101] 4 | # [SNIPPET_CATEGORIES: csv] 5 | # [SNIPPET_DESCRIPTION: Basic CSV file reading example] 6 | # [SNIPPET_AUTHOR: Bruno Girin ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | # This snippet demonstrates how the basics on how to read CSV files using 10 | # the Python csv module. 11 | # The full documentation for the csv module is available here: 12 | # http://docs.python.org/library/csv.html 13 | # 14 | # The data used in the companion csv101.csv file was taken from here: 15 | # http://www.trainweb.org/tubeprune/Statistics.htm 16 | # See, you can even learn some interesting facts about the London Underground 17 | # network while learning Python. 18 | 19 | # 20 | # First things first, we need to import the csv module 21 | # Also import sys to get argv[0], which holds the name of the script 22 | # 23 | import csv 24 | import sys 25 | 26 | # Derive the name of the CSV file from the name of the script and initialise 27 | # the content list 28 | csvFile = sys.argv[0].replace('.py', '.csv') 29 | content = [] 30 | 31 | print('Reading file %s' % csvFile) 32 | # And the rest is really easy as csv.reader can be iterated upon 33 | reader=csv.reader(open(csvFile)) 34 | for row in reader: 35 | """ 36 | For each row, we append the row to the content list, which will 37 | produce a list of lists. 38 | """ 39 | content.append(row) 40 | print content 41 | 42 | -------------------------------------------------------------------------------- /csv/csv2dict.csv: -------------------------------------------------------------------------------- 1 | "Line","Peak Service","Off Peak Service","Trains required","Stabling","Control Centre" 2 | "Bakerloo",23,15,"32 x 7-cars","Stonebridge Pk, London Rd, Elephant, Queens Pk.","Baker Street" 3 | "Central",30,18,"72 x 8-cars","Hainault, White City, West Ruislip, Loughton, Woodford","Wood Lane" 4 | "Circle",7,7,"14 x 6-cars","Hammersmith, Edgware Rd, Farringdon","Baker Street" 5 | "District","23 plus 7 Circles","14 plus 7 Circles ","74 x 6-cars","Ealing Common, Parsons Green, Triangle Sdgs, Barking, Upminster","Earls Court" 6 | "East London",6,6,"6 x 4-cars","New Cross","New Cross Depot" 7 | "Hammersmith & City",7,7,"17 x 6-cars","Hammersmith, Barking","Baker Street" 8 | "Jubilee",24,15,"46 x 6-cars","Neasden, Stratford, Stanmore","Neasden" 9 | "Metropolitan","16 plus 14 C & H trains","20 incl C & H lines","44 x 8-cars","Uxbridge, Rickmansworth, Neasden","Neasden" 10 | "Northern","30 max. 20 (branches)","20 max. 15 (branches)","84 x 6-cars","Morden, Golders Green, Highgate, High Barnet, Edgware","Coburg Street" 11 | "Piccadilly",24,18,"76 x 6-cars","Northfields, Cockfosters, Arnos Grove","Earls Court" 12 | "Victoria",28.5,18,"37 x 8-cars","Northumberland Park, Brixton, Walthamstow","Coburg Street" 13 | "Waterloo & City",19,12,"4 x 4-cars","Waterloo, Bank","Waterloo" 14 | -------------------------------------------------------------------------------- /csv/csv2dict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: CSV to Dictionary] 4 | # [SNIPPET_CATEGORIES: csv] 5 | # [SNIPPET_DESCRIPTION: Read a CSV file to a dictionary of dictionaries] 6 | # [SNIPPET_AUTHOR: Bruno Girin ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | # This snippet demonstrates how to read a CSV file into a dictionary of 10 | # dictionaries in order to be able to query it easily. 11 | # The full documentation for the csv module is available here: 12 | # http://docs.python.org/library/csv.html 13 | # 14 | # The data used in the companion csv2dict.csv file was taken from here: 15 | # http://www.trainweb.org/tubeprune/Statistics.htm 16 | # See, you can even learn some interesting facts about the London Underground 17 | # network while learning Python. 18 | 19 | # 20 | # First things first, we need to import the csv module 21 | # Also import sys to get argv[0], which holds the name of the script 22 | # 23 | import csv 24 | import sys 25 | 26 | # Derive the name of the CSV file from the name of the script and initialise 27 | # the headers list and content dictionary 28 | csvFile = sys.argv[0].replace('.py', '.csv') 29 | headers = None 30 | content = {} 31 | 32 | print('Reading file %s' % csvFile) 33 | reader=csv.reader(open(csvFile)) 34 | for row in reader: 35 | if reader.line_num == 1: 36 | """ 37 | If we are on the first line, create the headers list from the first row 38 | by taking a slice from item 1 as we don't need the very first header. 39 | """ 40 | headers = row[1:] 41 | else: 42 | """ 43 | Otherwise, the key in the content dictionary is the first item in the 44 | row and we can create the sub-dictionary by using the zip() function. 45 | We also know that the stabling entry is a comma separated list of names 46 | so we split it into a list for easier processing. 47 | """ 48 | content[row[0]] = dict(zip(headers, row[1:])) 49 | content[row[0]]['Stabling'] = [s.strip() for s in content[row[0]]['Stabling'].split(',')] 50 | 51 | # We can know get to the content by using the resulting dictionary, so to see 52 | # the list of lines, we can do: 53 | print "\nList of lines" 54 | print content.keys() 55 | # To see the list of statistics available for each line 56 | print "\nAvailable statistics for each line" 57 | print headers 58 | # To see any statistic for a line, we can just request it by name 59 | print "\nPeak hourly train frequency for the Piccadilly line" 60 | print content['Piccadilly']['Peak Service'] 61 | # Or we can use list comprehensions to filter the list 62 | print "\nThe list of lines that have Earl's Court as a control centre" 63 | print [k for k, v in content.items() if v['Control Centre'] == 'Earls Court'] 64 | print "\nThe list of lines that have Hammersmith as one of their stabling stations" 65 | print [k for k, v in content.items() if 'Hammersmith' in v['Stabling']] 66 | 67 | -------------------------------------------------------------------------------- /csv/vmstat.log: -------------------------------------------------------------------------------- 1 | procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu---- 2 | r b swpd free buff cache si so bi bo in cs us sy id wa 3 | 5 1 16272 309800 202736 712492 0 0 31 24 497 192 18 5 77 1 4 | 0 0 16272 309784 202736 712552 0 0 7 0 103 224 1 1 98 0 5 | 0 0 16272 309784 202752 712552 0 0 0 8 112 216 1 1 98 0 6 | 0 0 16272 309412 202756 712552 0 0 0 4 407 675 11 5 84 0 7 | 1 0 16272 302576 202756 716008 0 0 690 0 509 945 14 5 80 1 8 | 0 0 16272 282292 202812 719208 0 0 574 5 474 3026 31 8 60 1 9 | 0 0 16272 280184 202820 719316 0 0 22 26 433 996 27 5 68 0 10 | 8 0 16272 260288 202828 720344 0 0 130 9 370 4638 32 9 59 0 11 | 2 0 16268 226656 202852 722984 6 0 454 77 524 2484 47 12 41 0 12 | 1 0 16268 208024 202860 730784 0 0 1550 11 571 6092 93 7 0 0 13 | 2 0 16268 146360 202860 734772 0 0 794 0 705 3879 83 16 1 0 14 | 0 1 16268 123132 202864 750356 0 0 3116 8 660 1356 29 62 4 4 15 | 3 0 16264 93936 202864 764216 0 0 2714 5 640 1428 26 72 0 2 16 | 1 0 17192 53500 188852 783932 0 186 6819 202 669 1049 25 69 2 4 17 | 0 0 18028 55152 176676 786992 0 167 2153 191 703 1112 13 39 47 1 18 | 5 0 18724 54216 158244 791824 0 139 3410 146 655 1434 11 30 47 12 19 | 0 0 18948 53236 155644 791812 0 45 572 52 619 1277 6 14 79 0 20 | 0 0 18948 53236 155656 791812 0 0 0 144 571 1265 5 3 91 0 21 | 0 1 18948 52988 155660 792028 0 0 0 5 550 1217 4 3 91 3 22 | 0 0 18948 52864 155668 792148 0 0 0 10 568 1291 5 4 73 18 23 | 0 0 18948 52920 155676 792160 0 0 0 6 562 1292 6 5 89 0 24 | 2 0 18960 56156 155688 788076 0 2 38 14 565 1149 16 53 29 1 25 | 0 0 18960 264784 155696 786928 0 0 0 166 449 3601 18 16 66 0 26 | 0 0 18960 264808 155708 786928 0 0 0 19 430 746 13 7 77 4 27 | 0 0 18960 264776 155708 786928 0 0 0 2 236 389 5 2 94 0 28 | -------------------------------------------------------------------------------- /data_types/int_to_binary.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | def main(int_to_binary): 6 | x = '{0:08b}'.format(int_to_binary) 7 | if int_to_binary == 6: 8 | assert(x == '00000110') 9 | return x 10 | 11 | 12 | def main_other(int_to_binary): 13 | bin8 = lambda x: ''.join(reversed([str((x >> i) & 1) for i in range(8)])) 14 | x = bin8(6) 15 | if int_to_binary == 6: 16 | assert(x == '00000110') 17 | return x 18 | 19 | if __name__ == '__main__': 20 | main(6) 21 | main_other(6) 22 | -------------------------------------------------------------------------------- /data_types/mappingdoc.py: -------------------------------------------------------------------------------- 1 | class DictDoc(object): 2 | """ Adds a mapping interface to a document. Supports ``__getitem__`` and 3 | ``__contains__``. Both methods will only retrieve values assigned to 4 | a field, not methods or other attributes. 5 | """ 6 | def __getitem__(self, name): 7 | """ Gets the field ``name`` from the document """ 8 | fields = self.get_fields() 9 | if name in fields: 10 | return getattr(self, name) 11 | raise KeyError(name) 12 | 13 | def __setitem__(self, name, value): 14 | """ Sets the field ``name`` on the document """ 15 | setattr(self, name, value) 16 | 17 | def setdefault(self, name, value): 18 | """ if the ``name`` is set, return its value. Otherwse set ``name`` to 19 | ``value`` and return ``value``""" 20 | if name in self: 21 | return self[name] 22 | self[name] = value 23 | return self[name] 24 | 25 | def __contains__(self, name): 26 | """ Return whether a field is present. Fails if ``name`` is not a 27 | field or ``name`` is not set on the document or if ``name`` was 28 | not a field retrieved from the database 29 | """ 30 | try: 31 | self[name] 32 | except FieldNotRetrieved: 33 | return False 34 | except AttributeError: 35 | return False 36 | except KeyError: 37 | return False 38 | return True 39 | -------------------------------------------------------------------------------- /data_types/string_to_binary.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | 6 | 7 | 8 | def main(str_to_binary): 9 | '{0:08b}'.format(6) -------------------------------------------------------------------------------- /database/couch/desktop/addattachment.py: -------------------------------------------------------------------------------- 1 | # [SNIPPET_NAME: Add an attachment to a Desktopcouch Record] 2 | # [SNIPPET_CATEGORIES: DesktopCouch] 3 | # [SNIPPET_DESCRIPTION: Shows how to add an attachment to a Desktopcouch Record that will be stored and replicated by CouchDB] 4 | # [SNIPPET_AUTHOR: Manuel de la Pena ] 5 | # [SNIPPET_DOCS: http://www.themacaque.com/wiki/doku.php?id=desktopcouch:create_attachments] 6 | # [SNIPPET_LICENSE: GPL] 7 | import sys 8 | from desktopcouch.records.record import Record 9 | from desktopcouch.records.server import CouchDatabase 10 | 11 | # get the jpg to add from the command line 12 | 13 | if len(sys.argv) > 1: 14 | path = sys.argv[0] 15 | db = CouchDatabase("addattachment", create=True) 16 | record = Record(record_type="url") 17 | record.attach(path, "blob", "image/jpg") 18 | db.put_record(record) 19 | else: 20 | print "Please pass the path of the jpg to add." 21 | 22 | # got to /home/$USER/.local/share/desktop-couch/couchdb.html to see the 23 | # attached file 24 | -------------------------------------------------------------------------------- /database/couch/desktop/addrecord.py: -------------------------------------------------------------------------------- 1 | # [SNIPPET_NAME: Add record] 2 | # [SNIPPET_CATEGORIES: DesktopCouch] 3 | # [SNIPPET_DESCRIPTION: Add a new record into the database] 4 | # [SNIPPET_AUTHOR: Huntly Cameron ] 5 | # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] 6 | # [SNIPPET_LICENSE: GPL] 7 | 8 | from desktopcouch.records.server import CouchDatabase 9 | from desktopcouch.records.record import Record 10 | 11 | #create the database 12 | db = CouchDatabase("addrecordexample", create=True) 13 | 14 | #Records work like python dictionaries, and *should* have an 15 | #online description of how the record should look like. 16 | record_type = "http://example.com/somerecordtype.html" 17 | new_record = Record({"a field" : "a value", 18 | "another field" : "another value"}, record_type) 19 | 20 | #put our new record into the datbase 21 | db.put_record(new_record) 22 | 23 | #run xdg-open /home/$USER/.local/share/desktop-couch/couchdb.html 24 | #from the terminal to view the desktopcouch 25 | -------------------------------------------------------------------------------- /database/couch/desktop/createdatabase.py: -------------------------------------------------------------------------------- 1 | # [SNIPPET_NAME: Create a Database] 2 | # [SNIPPET_CATEGORIES: DesktopCouch] 3 | # [SNIPPET_DESCRIPTION: Creating a new database in desktopcouch] 4 | # [SNIPPET_AUTHOR: Huntly Cameron ] 5 | # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] 6 | # [SNIPPET_LICENSE: GPL] 7 | 8 | from desktopcouch.records.server import CouchDatabase 9 | 10 | #Create a database object. Your database needs to exist. 11 | #If it doesn't, you can create it by passing create=True. 12 | db = CouchDatabase('createdatabaseexample', create=True) 13 | 14 | #run xdg-open /home/$USER/.local/share/desktop-couch/couchdb.html 15 | #from the terminal to view the desktopcouch 16 | -------------------------------------------------------------------------------- /database/couch/desktop/deleterecords.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Delete Records] 4 | # [SNIPPET_CATEGORIES: DesktopCouch] 5 | # [SNIPPET_DESCRIPTION: Delete records from the desktop couch database] 6 | # [SNIPPET_AUTHOR: Andy Breiner ] 7 | # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] 8 | # [SNIPPET_LICENSE: GPL] 9 | 10 | from desktopcouch.records.server import CouchDatabase 11 | from desktopcouch.records.record import Record 12 | 13 | #Similar to the fetchrecord.py by Huntly Cameron 14 | #See fetchrecord.py for additional information 15 | 16 | db = CouchDatabase("deleterecordsexample", create=True) 17 | record_type = "http://example.com/delete-record-type.html" 18 | record = Record({"first" : "Andy", 19 | "last" : "Breiner"}, record_type) 20 | db.put_record(record) 21 | 22 | record = Record({"first" : "Jono", 23 | "last" : "Bacon"}, record_type) 24 | db.put_record(record) 25 | 26 | results = db.get_records(record_type = record_type, create_view = True) 27 | 28 | for records in results: 29 | record = records.value 30 | print "First: %s" % record["first"] 31 | print "Last : %s" % record["last"] 32 | if record["first"] == "Andy": 33 | db.delete_record(record["_id"]) 34 | 35 | -------------------------------------------------------------------------------- /database/couch/desktop/fetchrecords.py: -------------------------------------------------------------------------------- 1 | # [SNIPPET_NAME: Fetch Records] 2 | # [SNIPPET_CATEGORIES: DesktopCouch] 3 | # [SNIPPET_DESCRIPTION: Fetch records from the desktop couch database] 4 | # [SNIPPET_AUTHOR: Huntly Cameron ] 5 | # [SNIPPET_DOCS: http://www.freedesktop.org/wiki/Specifications/desktopcouch/Documentation/SimpleGuide] 6 | # [SNIPPET_LICENSE: GPL] 7 | 8 | from desktopcouch.records.server import CouchDatabase 9 | from desktopcouch.records.record import Record 10 | 11 | #First, we want to put a record into a database and then get it out 12 | #create the database 13 | db = CouchDatabase("fetchrecordsexample", create=True) 14 | 15 | #Create some records 16 | record_type = "http://example.com/fetch-record-type.html" 17 | new_record = Record({"afield" : "a value", 18 | "anotherfield" : "another value"}, record_type) 19 | another_record = Record({"afield" : "some value", 20 | "anotherfield" : "some other value"}, record_type) 21 | 22 | #put our new records into the datbase 23 | db.put_record(new_record) 24 | db.put_record(another_record) 25 | 26 | #Fetch all the records in the database and display them 27 | results = db.get_records(record_type = record_type, create_view = True) 28 | 29 | for records in results: 30 | record = records.value 31 | print "a field: %s" % record["afield"] 32 | print "another field: %s" % record["anotherfield"] 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /database/couch/desktop/listenchanges.py: -------------------------------------------------------------------------------- 1 | # [SNIPPET_NAME: Listen to changes in CouchDatabase] 2 | # [SNIPPET_CATEGORIES: DesktopCouch] 3 | # [SNIPPET_DESCRIPTION: Register a callback that will listen to changes in the CouchDatabase] 4 | # [SNIPPET_AUTHOR: Manuel de la Pena ] 5 | # [SNIPPET_DOCS: http://www.themacaque.com/wiki/doku.php?id=desktopcouch:listen_to_changes] 6 | # [SNIPPET_LICENSE: GPL] 7 | import time 8 | from desktopcouch.records.server import CouchDatabase 9 | 10 | # we are going to be listening to the changes in this callback 11 | def changes_cb(seq=None, id=None, changes=None): 12 | print seq 13 | print id 14 | print changes 15 | 16 | db = CouchDatabase("listenchangesexample", create=True) 17 | 18 | # is better, use glib main loop or twisted task! 19 | while True: 20 | db.report_changes(changes_cb) 21 | time.sleep(30) 22 | 23 | # got to /home/$USER/.local/share/desktop-couch/couchdb.html and make 24 | # changes to the listenchangesexample to see the callback output 25 | -------------------------------------------------------------------------------- /database/psql_notify.py: -------------------------------------------------------------------------------- 1 | #!/usr/env python 2 | 3 | import select 4 | import psycopg2 5 | import psycopg2.extensions 6 | 7 | DSN = '' 8 | 9 | conn = psycopg2.connect(DSN) 10 | conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) 11 | 12 | curs = conn.cursor() 13 | curs.execute("LISTEN test;") 14 | 15 | print "Waiting for notifications on channel 'test'" 16 | while 1: 17 | if select.select([conn], [], [], 5) == ([], [], []): 18 | print "Timeout" 19 | else: 20 | conn.poll() 21 | while conn.notifies: 22 | notify = conn.notifies.pop() 23 | print "Got NOTIFY:", notify.pid, notify.channel, notify.payload 24 | -------------------------------------------------------------------------------- /decorators/decorator.py: -------------------------------------------------------------------------------- 1 | 2 | def expose(template): 3 | def mark_exposed(func): 4 | func.exposed = True 5 | func.template = template 6 | return func 7 | return mark_exposed 8 | 9 | @expose('template.html') 10 | def myview(): 11 | return_value=dict(value1=6, value2="food") 12 | return return_value 13 | 14 | if __name__ == '__main__': 15 | print(myview.exposed) 16 | print(myview.template) 17 | -------------------------------------------------------------------------------- /decorators/decorator2.py: -------------------------------------------------------------------------------- 1 | 2 | class memoize(object): 3 | def __init__(self, max): 4 | self.max = max 5 | 6 | def __call__(self, func): 7 | return decorator(self.check_cache, func) 8 | 9 | def check_cache(self, func, *args): 10 | if not hasattr(func, 'results'): 11 | func.results = dict() 12 | if args not in func.results: 13 | func.results[args] = func(*args) 14 | return func.results[args] 15 | 16 | 17 | 18 | @memoize 19 | def find_user(user_id): 20 | #query database and find user object 21 | return User.m_get(_id=user_id) 22 | -------------------------------------------------------------------------------- /decorators/decorator_example.py: -------------------------------------------------------------------------------- 1 | # a decorator receives the method it's wrapping as a variable 'f' 2 | def increment(f): 3 | # we use arbitrary args and keywords to 4 | # ensure we grab all the input arguments. 5 | def wrapped_f(*args, **kw): 6 | # note we call f against the variables passed into the wrapper, 7 | # and cast the result to an int and increment . 8 | return int(f(*args, **kw)) + 1 9 | return wrapped_f # the wrapped function gets returned. 10 | 11 | 12 | @increment 13 | def plus(a, b): 14 | return a + b 15 | 16 | if __name__ == '__main__': 17 | result = plus(4, 6) 18 | print(result) 19 | -------------------------------------------------------------------------------- /decorators/django_login_decorators.py: -------------------------------------------------------------------------------- 1 | def check_basic_auth(request): 2 | if 'HTTP_AUTHORIZATION' in request.META: 3 | auth = request.META['HTTP_AUTHORIZATION'].split() 4 | if len(auth) == 2: 5 | # NOTE: We are only support basic authentication for now. 6 | # 7 | if auth[0].lower() == "basic": 8 | uname, passwd = base64.b64decode(auth[1]).split(':') 9 | user = authenticate(username=uname, password=passwd) 10 | if user is not None: 11 | if user.is_active: 12 | login(request, user) 13 | request.user = user 14 | return request 15 | 16 | 17 | def check_session_auth(request): 18 | if request.user.is_authenticated(): 19 | return True 20 | 21 | 22 | def check_auth(request): 23 | if request.user.is_authenticated(): 24 | return True 25 | else: 26 | # They are not logged in. See if they provided login credentials 27 | request = check_basic_auth(request) 28 | if request.user.is_authenticated(): 29 | return True 30 | else: 31 | return False 32 | 33 | def view_or_basicauth(view, request, test_func, realm = "", *args, **kwargs): 34 | """ 35 | This is a helper function used by both 'logged_in_or_basicauth' and 36 | 'has_perm_or_basicauth' that does the nitty of determining if they 37 | are already logged in or if they have provided proper http-authorization 38 | and returning the view if all goes well, otherwise responding with a 401. 39 | """ 40 | if check_auth(request): 41 | return view(request, *args, **kwargs) 42 | 43 | # Either they did not provide an authorization header or 44 | # something in the authorization attempt failed. Send a 401 45 | # back to them to ask them to authenticate. 46 | # 47 | response = HttpResponse('Auth Required') 48 | response.status_code = 401 49 | response['WWW-Authenticate'] = 'Basic realm="%s"' % realm 50 | return response 51 | 52 | ############################################################################# 53 | 54 | 55 | def logged_in_or_basicauth(view_func): 56 | """ 57 | The existing loggged_in_or_basic does not properly pass along arguments 58 | so I wrote this one temporarily to see if I can replace it 59 | :param view_func: 60 | :return: 61 | """ 62 | @wraps(view_func) 63 | def wrapper(request, *args, **kwargs): 64 | checked_basic_auth = None 65 | if request.user.is_anonymous(): 66 | request = check_basic_auth(request) 67 | checked_basic_auth = True 68 | if check_session_auth(request): 69 | return view_func(request, *args, **kwargs) 70 | else: 71 | response = HttpResponse('Auth Required: user was %s and check_basic: %s' % (request.user, checked_basic_auth)) 72 | response.status_code = 401 73 | return response 74 | return wrapper 75 | -------------------------------------------------------------------------------- /decorators/parameterized_decorator.py: -------------------------------------------------------------------------------- 1 | def minimum_x(x): 2 | ''' 3 | Write a function decorator that takes an argument, and returns a decorator 4 | that can be used to decorate a function, which verifies that the first 5 | argument to a decorated function is at least the given value, raising a 6 | ValueError on failure. 7 | >>> @minimum_x(6) 8 | ... def test(arg): 9 | ... print arg 10 | ... 11 | >>> test(6) 12 | 6 13 | >>> test(5) # raises ValueError 14 | ''' 15 | def minimum(mnm): 16 | def decorator(func): 17 | def wrapper(val): 18 | if val < mnm: 19 | raise ValueError() 20 | result = func(val) 21 | return result 22 | return wrapper 23 | return decorator 24 | -------------------------------------------------------------------------------- /decorators/wraps_decorator.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from functools import wraps 5 | 6 | 7 | def my_decorator(view_func): 8 | @wraps(view_func) 9 | def wrapper(request, *args, **kwargs): 10 | print('calling wrapper') 11 | if request.get('some_data'): 12 | request['user'] = 'brent' 13 | return view_func(request, *args, **kwargs) 14 | return wrapper 15 | 16 | 17 | @my_decorator 18 | def example(request): 19 | """ Docstring """ 20 | print('calling example function') 21 | print(request) 22 | 23 | if __name__ == '__main__': 24 | request = dict(some_data='has_value') 25 | example(request) 26 | -------------------------------------------------------------------------------- /descriptors/descriptors.py: -------------------------------------------------------------------------------- 1 | class NameDescriptor(object): 2 | 3 | def __get__(self, instance, owner): 4 | return instance._value 5 | 6 | def __set__(self, instance, value): 7 | ucase_name = value.upper() 8 | instance._value = ucase_name 9 | 10 | def __delete__(self, instance): 11 | del(instance._value) 12 | 13 | def __len__(self, instance): 14 | return len(instance._value) 15 | 16 | 17 | 18 | class Person(object): 19 | 20 | name = NameDescriptor() 21 | 22 | 23 | 24 | if __name__ == '__main__': 25 | n = Person() 26 | n.name = 'Brent' 27 | n.hat = 'bowler' 28 | print(dir(n)) 29 | print(vars(n)) 30 | print(n.name) 31 | print(n.hat) 32 | print(len(n.name)) 33 | -------------------------------------------------------------------------------- /descriptors/descriptors_example.py: -------------------------------------------------------------------------------- 1 | class MyPropertyDescriptor(object): 2 | 3 | def __get__(self, instance, owner): 4 | # Use a private variable to avoid calling the getter recursively 5 | if hasattr(instance, '_greetings'): 6 | return instance._greetings 7 | else: 8 | return None 9 | 10 | def __set__(self, instance, value): 11 | # Custom code can go here to transform the value on set 12 | # Code is slightly naive since it assume value is string 13 | instance._greetings = value.upper() 14 | 15 | def __delete__(self, instance): 16 | del instance._neighborhoods 17 | 18 | 19 | class ObjectWithDescriptor(object): 20 | neighborhoods = MyPropertyDescriptor() 21 | 22 | def __init__(self): 23 | self.other_property = None 24 | 25 | def print_values(self): 26 | print(self.neighborhoods) 27 | print(self.other_property) 28 | 29 | if __name__ == '__main__': 30 | owd = ObjectWithDescriptor() 31 | owd.print_values() 32 | owd.neighborhoods = 'hello monkey' 33 | owd.other_property = 'hello banana' 34 | owd.print_values() 35 | -------------------------------------------------------------------------------- /django/JSONField.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | from django.db import models 3 | from django.db.models import signals 4 | from django.conf import settings 5 | from django.utils import simplejson as json 6 | from django.dispatch import dispatcher 7 | 8 | class JSONEncoder(json.JSONEncoder): 9 | def default(self, obj): 10 | if isinstance(obj, datetime.datetime): 11 | return obj.strftime('%Y-%m-%d %H:%M:%S') 12 | elif isinstance(obj, datetime.date): 13 | return obj.strftime('%Y-%m-%d') 14 | elif isinstance(obj, datetime.time): 15 | return obj.strftime('%H:%M:%S') 16 | return json.JSONEncoder.default(self, obj) 17 | 18 | def dumps(data): 19 | return JSONEncoder().encode(data) 20 | 21 | def loads(str): 22 | return json.loads(str, encoding=settings.DEFAULT_CHARSET) 23 | 24 | class JSONField(models.TextField): 25 | def db_type(self): 26 | return 'text' 27 | 28 | def pre_save(self, model_instance, add): 29 | value = getattr(model_instance, self.attname, None) 30 | return dumps(value) 31 | 32 | def contribute_to_class(self, cls, name): 33 | super(JSONField, self).contribute_to_class(cls, name) 34 | dispatcher.connect(self.post_init, signal=signals.post_init, sender=cls) 35 | 36 | def get_json(model_instance): 37 | return dumps(getattr(model_instance, self.attname, None)) 38 | setattr(cls, 'get_%s_json' % self.name, get_json) 39 | 40 | def set_json(model_instance, json): 41 | return setattr(model_instance, self.attname, loads(json)) 42 | setattr(cls, 'set_%s_json' % self.name, set_json) 43 | 44 | def post_init(self, instance=None): 45 | value = self.value_from_object(instance) 46 | if (value): 47 | setattr(instance, self.attname, loads(value)) 48 | else: 49 | setattr(instance, self.attname, None) 50 | -------------------------------------------------------------------------------- /django/TaskViewMixin.py: -------------------------------------------------------------------------------- 1 | from django.views.generic import TemplateView 2 | 3 | from celery.result import AsyncResult 4 | from celery.registry import tasks 5 | 6 | class BaseResponseView(TemplateView): 7 | result = None 8 | 9 | def get_context_data(self, **kwargs): 10 | context = super(BaseResponseView, self).get_context_data(**kwargs) 11 | context['result'] = self.result 12 | return context 13 | 14 | class WaitForResponseView(BaseResponseView): 15 | template_name = 'tasks/wait_for_response.html' 16 | 17 | class TaskFailedResponseView(BaseResponseView): 18 | template_name = 'tasks/task_failed_response.html' 19 | 20 | class TaskViewMixin(object): 21 | session_key = 'asynctask_token' 22 | wait_for_response_view = WaitForResponseView 23 | task_failed_response_view = TaskFailedResponseView 24 | 25 | task_name = None #please define this 26 | 27 | def get_task_kwargs(self): 28 | return {} 29 | 30 | def schedule_task(self): 31 | task = tasks[self.task_name] 32 | kwargs = self.get_task_kwargs() 33 | result = task.apply_async(kwargs=kwargs) 34 | self.set_task_token(result.task_id) 35 | return result 36 | 37 | def get_task_token(self): 38 | return self.request.session.get(self.session_key, None) 39 | 40 | def set_task_token(self, task_id): 41 | self.request.session[self.session_key] = task_id 42 | 43 | def clear_task_token(self): 44 | del self.request.session[self.session_key] 45 | 46 | def task_status(self): 47 | task_id = self.get_task_token() 48 | if task_id is None: 49 | return None 50 | return AsyncResult(task_id) 51 | 52 | def get(self, request, **kwargs): 53 | result = self.task_status() 54 | if result is None or request.REQUEST.get('recreate', False): 55 | result = self.schedule_task() 56 | if result.failed(): 57 | self.clear_task_token() 58 | return self.render_task_failed_response(result) 59 | elif result.successful(): 60 | self.clear_task_token() 61 | return self.render_task_success_response(result) 62 | else: 63 | return self.render_wait_for_response(result) 64 | 65 | def render_wait_for_response(self, result): 66 | return self.wait_for_response_view(result=result).dispatch(self.request) 67 | 68 | def render_task_failed_response(self, result): 69 | return self.task_failed_response_view(result=result).dispatch(self.request) 70 | 71 | def render_task_success_response(self, result): 72 | return self.render_to_response({'result':result}) 73 | 74 | class ExampleTaskView(TaskViewMixin, TemplateView): 75 | template_name = 'tasks/task_success.html' 76 | task_name = 'celery.ping' 77 | 78 | example_view = ExampleTaskView.as_view() 79 | -------------------------------------------------------------------------------- /django/add_attributes_to_formfields.py: -------------------------------------------------------------------------------- 1 | from django import template 2 | 3 | register = template.Library() 4 | 5 | @register.simple_tag 6 | def formfield(field,**kwargs): 7 | field.field.widget.attrs.update(kwargs) 8 | return field 9 | -------------------------------------------------------------------------------- /django/admin.py: -------------------------------------------------------------------------------- 1 | from commerce.catalog.models import Product 2 | from commerce.order.models import Order, OrderItem, OrderStatus 3 | from commerce.checkout.models import Customer, Address 4 | from commerce.order.forms import OrderHeaderAdminForm 5 | from django.contrib import admin 6 | 7 | 8 | class OrderHeaderAdmin(admin.ModelAdmin): 9 | list_display = ('id','order_created','finalized','exported') 10 | list_display_links = ('id',) 11 | #inlines = [OrderItemInline] 12 | exclude = ('alchemy_user',) 13 | 14 | admin.site.register(Order, OrderHeaderAdmin) 15 | 16 | -------------------------------------------------------------------------------- /django/class_based_view.py: -------------------------------------------------------------------------------- 1 | class MyView(View): 2 | """Displays the details of a BlogPost""" 3 | 4 | def get(self, request, *args, **kwargs): 5 | return TemplateResponse(request, self.get_template_name(), 6 | self.get_context_data()) 7 | 8 | def get_template_name(self): 9 | """Returns the name of the template we should render""" 10 | return "blog/blogpost_detail.html" 11 | 12 | def get_context_data(self): 13 | """Returns the data passed to the template""" 14 | return { 15 | "blogpost": self.get_object(), 16 | } 17 | 18 | def get_object(self): 19 | """Returns the BlogPost instance that the view displays""" 20 | return get_object_or_404(BlogPost, pk=self.kwargs.get("pk")) -------------------------------------------------------------------------------- /django/django-email.backend.py: -------------------------------------------------------------------------------- 1 | from django.contrib.auth.models import User 2 | from django.core.validators import email_re 3 | 4 | class BasicBackend: 5 | def get_user(self, user_id): 6 | try: 7 | return User.objects.get(pk=user_id) 8 | except User.DoesNotExist: 9 | return None 10 | 11 | class EmailBackend(BasicBackend): 12 | def authenticate(self, username=None, password=None): 13 | #If username is an email address, then try to pull it up 14 | if email_re.search(username): 15 | try: 16 | user = User.objects.get(email=username) 17 | except User.DoesNotExist: 18 | return None 19 | else: 20 | #We have a non-email address username we should try username 21 | try: 22 | user = User.objects.get(username=username) 23 | except User.DoesNotExist: 24 | return None 25 | if user.check_password(password): 26 | return user 27 | -------------------------------------------------------------------------------- /django/email_as_username_13.py: -------------------------------------------------------------------------------- 1 | """ 2 | @author: chris http://djangosnippets.org/snippets/1845/ 3 | updated to 1.3 by Łukasz Kidziński 4 | """ 5 | from django.contrib.auth.models import User 6 | from django.core.validators import email_re 7 | 8 | class EmailBackend: 9 | """ 10 | Authenticate with e-mail. 11 | 12 | Use the e-mail, and password 13 | 14 | Should work with django 1.3 15 | """ 16 | 17 | supports_object_permissions = False 18 | supports_anonymous_user = False 19 | supports_inactive_user = False 20 | 21 | def authenticate(self, username=None, password=None): 22 | if email_re.search(username): 23 | try: 24 | user = User.objects.get(email=username) 25 | except User.DoesNotExist: 26 | return None 27 | else: 28 | #We have a non-email address username we should try username 29 | try: 30 | user = User.objects.get(username=username) 31 | except User.DoesNotExist: 32 | return None 33 | if user.check_password(password): 34 | return user 35 | return None 36 | 37 | def get_user(self, user_id): 38 | try: 39 | return User.objects.get(pk=user_id) 40 | except User.DoesNotExist: 41 | return None 42 | -------------------------------------------------------------------------------- /django/query_count_decorator.py: -------------------------------------------------------------------------------- 1 | def check_query_count(num_queries): 2 | def decorator(func): 3 | @wraps(func) 4 | def inner(self, *args, **kwargs): 5 | initial_queries = len(connection.queries) 6 | ret = func(self, *args, **kwargs) 7 | final_queries = len(connection.queries) 8 | if settings.DEBUG: 9 | self.assertEqual(final_queries - initial_queries, num_queries) 10 | return ret 11 | return inner 12 | return decorator 13 | -------------------------------------------------------------------------------- /django/taskbaseview.py: -------------------------------------------------------------------------------- 1 | from django.views.generic import TemplateView 2 | 3 | from celery.result import AsyncResult 4 | from celery.registry import tasks 5 | 6 | class BaseResponseView(TemplateView): 7 | result = None 8 | 9 | def get_context_data(self, **kwargs): 10 | context = super(BaseResponseView, self).get_context_data(**kwargs) 11 | context['result'] = self.result 12 | return context 13 | 14 | class WaitForResponseView(BaseResponseView): 15 | template_name = 'tasks/wait_for_response.html' 16 | 17 | class TaskFailedResponseView(BaseResponseView): 18 | template_name = 'tasks/task_failed_response.html' 19 | 20 | class TaskViewMixin(object): 21 | session_key = 'asynctask_token' 22 | wait_for_response_view = WaitForResponseView 23 | task_failed_response_view = TaskFailedResponseView 24 | 25 | task_name = None #please define this 26 | 27 | def get_task_kwargs(self): 28 | return {} 29 | 30 | def schedule_task(self): 31 | task = tasks[self.task_name] 32 | kwargs = self.get_task_kwargs() 33 | result = task.apply_async(kwargs=kwargs) 34 | self.set_task_token(result.task_id) 35 | return result 36 | 37 | def get_task_token(self): 38 | return self.request.session.get(self.session_key, None) 39 | 40 | def set_task_token(self, task_id): 41 | self.request.session[self.session_key] = task_id 42 | 43 | def clear_task_token(self): 44 | del self.request.session[self.session_key] 45 | 46 | def task_status(self): 47 | task_id = self.get_task_token() 48 | if task_id is None: 49 | return None 50 | return AsyncResult(task_id) 51 | 52 | def get(self, request, **kwargs): 53 | result = self.task_status() 54 | if result is None or request.REQUEST.get('recreate', False): 55 | result = self.schedule_task() 56 | if result.failed(): 57 | self.clear_task_token() 58 | return self.render_task_failed_response(result) 59 | elif result.successful(): 60 | self.clear_task_token() 61 | return self.render_task_success_response(result) 62 | else: 63 | return self.render_wait_for_response(result) 64 | 65 | def render_wait_for_response(self, result): 66 | return self.wait_for_response_view(result=result).dispatch(self.request) 67 | 68 | def render_task_failed_response(self, result): 69 | return self.task_failed_response_view(result=result).dispatch(self.request) 70 | 71 | def render_task_success_response(self, result): 72 | return self.render_to_response({'result':result}) 73 | 74 | class ExampleTaskView(TaskViewMixin, TemplateView): 75 | template_name = 'tasks/task_success.html' 76 | task_name = 'celery.ping' 77 | 78 | example_view = ExampleTaskView.as_view() 79 | -------------------------------------------------------------------------------- /django/test_settings_manager.py: -------------------------------------------------------------------------------- 1 | from django.conf import settings 2 | from django.core.management import call_command 3 | from django.db.models import loading 4 | from django.test import TestCase 5 | 6 | NO_SETTING = ('!', None) 7 | 8 | class TestSettingsManager(object): 9 | """ 10 | A class which can modify some Django settings temporarily for a 11 | test and then revert them to their original values later. 12 | 13 | Automatically handles resyncing the DB if INSTALLED_APPS is 14 | modified. 15 | 16 | """ 17 | def __init__(self): 18 | self._original_settings = {} 19 | 20 | def set(self, **kwargs): 21 | for k,v in kwargs.iteritems(): 22 | self._original_settings.setdefault(k, getattr(settings, k, 23 | NO_SETTING)) 24 | setattr(settings, k, v) 25 | if 'INSTALLED_APPS' in kwargs: 26 | self.syncdb() 27 | 28 | def syncdb(self): 29 | loading.cache.loaded = False 30 | call_command('syncdb', verbosity=0) 31 | 32 | def revert(self): 33 | for k,v in self._original_settings.iteritems(): 34 | if v == NO_SETTING: 35 | delattr(settings, k) 36 | else: 37 | setattr(settings, k, v) 38 | if 'INSTALLED_APPS' in self._original_settings: 39 | self.syncdb() 40 | self._original_settings = {} 41 | 42 | 43 | class SettingsTestCase(TestCase): 44 | """ 45 | A subclass of the Django TestCase with a settings_manager 46 | attribute which is an instance of TestSettingsManager. 47 | 48 | Comes with a tearDown() method that calls 49 | self.settings_manager.revert(). 50 | 51 | """ 52 | def __init__(self, *args, **kwargs): 53 | super(SettingsTestCase, self).__init__(*args, **kwargs) 54 | self.settings_manager = TestSettingsManager() 55 | 56 | def tearDown(self): 57 | self.settings_manager.revert() 58 | -------------------------------------------------------------------------------- /django/testing/fast_test_runner.py: -------------------------------------------------------------------------------- 1 | from django.test import TransactionTestCase 2 | try: 3 | from django.test.runner import DiscoverRunner as BaseRunner 4 | except ImportError: 5 | # Django < 1.6 fallback 6 | from django.test.simple import DjangoTestSuiteRunner as BaseRunner 7 | 8 | from mock import patch 9 | 10 | 11 | class NoDatabaseMixin(object): 12 | """ 13 | Test runner mixin which skips the DB setup/teardown 14 | when there are no subclasses of TransactionTestCase to improve the speed 15 | of running the tests. 16 | """ 17 | 18 | def build_suite(self, *args, **kwargs): 19 | """ 20 | Check if any of the tests to run subclasses TransactionTestCase. 21 | """ 22 | suite = super(NoDatabaseMixin, self).build_suite(*args, **kwargs) 23 | self._needs_db = any([isinstance(test, TransactionTestCase) for test in suite]) 24 | return suite 25 | 26 | def setup_databases(self, *args, **kwargs): 27 | """ 28 | Skip test creation if not needed. Ensure that touching the DB raises and 29 | error. 30 | """ 31 | if self._needs_db: 32 | return super(NoDatabaseMixin, self).setup_databases(*args, **kwargs) 33 | if self.verbosity >= 1: 34 | print 'No DB tests detected. Skipping Test DB creation...' 35 | self._db_patch = patch('django.db.backends.util.CursorWrapper') 36 | self._db_mock = self._db_patch.start() 37 | self._db_mock.side_effect = RuntimeError('No testing the database!') 38 | return None 39 | 40 | def teardown_databases(self, *args, **kwargs): 41 | """ 42 | Remove cursor patch. 43 | """ 44 | if self._needs_db: 45 | return super(NoDatabaseMixin, self).teardown_databases(*args, **kwargs) 46 | self._db_patch.stop() 47 | return None 48 | 49 | 50 | class FastTestRunner(NoDatabaseMixin, BaseRunner): 51 | """Actual test runner sub-class to make use of the mixin.""" -------------------------------------------------------------------------------- /django/testing/remove_media_testrunner.py: -------------------------------------------------------------------------------- 1 | import shutil 2 | import tempfile 3 | 4 | from django.conf import settings 5 | from django.test.simple import DjangoTestSuiteRunner 6 | 7 | 8 | class TempMediaMixin(object): 9 | "Mixin to create MEDIA_ROOT in temp and tear down when complete." 10 | 11 | def setup_test_environment(self): 12 | "Create temp directory and update MEDIA_ROOT and default storage." 13 | super(TempMediaMixin, self).setup_test_environment() 14 | settings._original_media_root = settings.MEDIA_ROOT 15 | settings._original_file_storage = settings.DEFAULT_FILE_STORAGE 16 | self._temp_media = tempfile.mkdtemp() 17 | settings.MEDIA_ROOT = self._temp_media 18 | settings.DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage' 19 | 20 | def teardown_test_environment(self): 21 | "Delete temp storage." 22 | super(TempMediaMixin, self).teardown_test_environment() 23 | shutil.rmtree(self._temp_media, ignore_errors=True) 24 | settings.MEDIA_ROOT = settings._original_media_root 25 | del settings._original_media_root 26 | settings.DEFAULT_FILE_STORAGE = settings._original_file_storage 27 | del settings._original_file_storage 28 | 29 | 30 | class CusomTestSuiteRunner(TempMediaMixin, DjangoTestSuiteRunner): 31 | "Local test suite runner." -------------------------------------------------------------------------------- /django/urls_template.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import patterns, url 2 | from views import BlogPostDetailView 3 | 4 | urlpatterns = patterns('', 5 | url(r'^(?P.*)', BlogPostDetailView.as_view(), {}, 'post-view'), 6 | ) 7 | 8 | -------------------------------------------------------------------------------- /django/usersonline.py: -------------------------------------------------------------------------------- 1 | 2 | class UsersOnline(models.Model): 3 | # Showing users online 4 | username = models.CharField(max_length=100) 5 | time = models.DateTimeField() 6 | url = models.URLField() 7 | 8 | def __unicode__(self): 9 | return self.username 10 | 11 | def recountUsersOnline(): 12 | users = UsersOnline.objects.all() 13 | date_now = datetime.now() 14 | # Deleting expired users 15 | for user in users: 16 | dt = date_now - user.time 17 | if (dt.seconds > 300): 18 | # User expired 19 | user.delete() 20 | 21 | def addUserOnline(username, url): 22 | try: 23 | user = UsersOnline.objects.get(username=username) 24 | user.time = datetime.now() 25 | except: 26 | user = UsersOnline(username=username, time=datetime.now(), url=url) 27 | -------------------------------------------------------------------------------- /dynamic_classes/class_property.py: -------------------------------------------------------------------------------- 1 | """ 2 | Class/metaclass version of property. 3 | 4 | The replacement 'property' class acts much like the original -- you 5 | can call it with four optional arguments (get/set/del/doc), and it 6 | returns a descriptor which will use those arguments. However, you can 7 | also subclass it, defining the optional member functions 8 | fget/fset/fdel and it uses those to create a descriptor. 9 | """ 10 | 11 | real_property = property 12 | 13 | class property_meta(type): 14 | 15 | def __new__(meta, class_name, bases, new_attrs): 16 | if bases == (object,): 17 | # The property class itself 18 | return type.__new__(meta, class_name, bases, new_attrs) 19 | fget = new_attrs.get('fget') 20 | fset = new_attrs.get('fset') 21 | fdel = new_attrs.get('fdel') 22 | fdoc = new_attrs.get('__doc__') 23 | return real_property(fget, fset, fdel, fdoc) 24 | 25 | class property(object): 26 | 27 | __metaclass__ = property_meta 28 | 29 | def __new__(cls, fget=None, fset=None, fdel=None, fdoc=None): 30 | if fdoc is None and fget is not None: 31 | fdoc = fget.__doc__ 32 | return real_property(fget, fset, fdel, fdoc) 33 | 34 | __test__ = { 35 | 'normal': r""" 36 | >>> class X(object): 37 | ... class double(property): 38 | ... 'Double the value of obj.value' 39 | ... def fget(self): 40 | ... return self.value * 2 41 | ... def fset(self, value): 42 | ... self.value = value / 2 43 | ... def fdel(self): 44 | ... del self.value 45 | ... def _get_half(self): 46 | ... return self.value / 2 47 | ... half = property(_get_half) 48 | >>> x = X() 49 | >>> x.value = 1 50 | >>> x.double 51 | 2 52 | >>> x.double = 4 53 | >>> x.double 54 | 4 55 | >>> x.value 56 | 2 57 | >>> del x.double 58 | >>> x.value 59 | Traceback (most recent call last): 60 | AttributeError: 'X' object has no attribute 'value' 61 | >>> x.value = 100 62 | >>> x.half 63 | 50 64 | """} 65 | 66 | if __name__ == '__main__': 67 | import doctest 68 | doctest.testmod() 69 | 70 | -------------------------------------------------------------------------------- /dynamic_classes/get_all_class_properties.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | Get all properties of a class 6 | """ 7 | 8 | 9 | def iter_properties_of_class(cls): 10 | for varname in vars(cls): 11 | value = getattr(cls, varname) 12 | if isinstance(value, property): 13 | yield varname 14 | 15 | def properties(inst): 16 | result = {} 17 | for cls in inst.__class__.mro(): 18 | for varname in iter_properties_of_class(cls): 19 | result[varname] = getattr(inst, varname) 20 | return result 21 | 22 | >>> a = MyClass() 23 | >>> a.x = 5 24 | Setting x to 5 25 | >>> properties(a) 26 | {'x': 5} 27 | 28 | class MyClass(object): 29 | @property 30 | def x(self): 31 | pass 32 | 33 | class MyDataClass(object): 34 | 35 | def __init__(self): 36 | self.value1 = None 37 | self.value2 = None 38 | 39 | def __iter__(self): 40 | for varname in vars(self): 41 | yield varname 42 | 43 | 44 | # this get the names of properties including __dunder__ types 45 | print vars(MyClass).keys() 46 | mdc = MyDataClass() 47 | for x in mdc: 48 | print(x) 49 | 50 | -------------------------------------------------------------------------------- /email/EmailServer.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | import asyncore 3 | from smtpd import SMTPServer 4 | 5 | class EmlServer(SMTPServer): 6 | no = 0 7 | def process_message(self, peer, mailfrom, rcpttos, data): 8 | filename = ''%s-%d.eml'' % (datetime.now().strftime(''%Y%m%d%H%M%S''), 9 | self.no) 10 | f = open(filename, ''w'') 11 | f.write(data) 12 | f.close 13 | print ''%s saved.'' % filename 14 | self.no += 1 15 | 16 | 17 | def run(): 18 | foo = EmlServer((''localhost'', 25), None) 19 | try: 20 | asyncore.loop() 21 | except KeyboardInterrupt: 22 | pass 23 | 24 | 25 | if __name__ == ''__main__: 26 | -------------------------------------------------------------------------------- /email/mail.py: -------------------------------------------------------------------------------- 1 | # [SNIPPET_NAME: Sending Email] 2 | # [SNIPPET_CATEGORIES: smtplib, email] 3 | # [SNIPPET_DESCRIPTION: Sending mail from gmail account with many attachements and to_many_mails_ids] 4 | # [SNIPPET_AUTHOR: kutuma] 5 | # [SNIPPET_LICENSE: GPL] 6 | # [SNIPPET_UPLOADED_BY: Arulalan.T ] 7 | 8 | # you need to set the gmail user name and its password at the line of 22 and 23 st (in gedit, line number) of this snippet. 9 | 10 | # you need to set the to_mail_ids in a string array , subject, body , attachements_absolute_path in a string array from the line of 26 of this snippet. 11 | 12 | 13 | #!/usr/bin/python 14 | # ref : http://kutuma.blogspot.com/2007/08/sending-emails-via-gmail-with-python.html 15 | import smtplib 16 | from email.MIMEMultipart import MIMEMultipart 17 | from email.MIMEBase import MIMEBase 18 | from email.MIMEText import MIMEText 19 | from email import Encoders 20 | import os 21 | 22 | gmail_user = "username@gmail.com" 23 | gmail_pwd = "gmail_password" 24 | 25 | 26 | body=" I am Body " 27 | subject="I am Subject " 28 | 29 | to_mail_ids=["friend1_mail_id","friend2_mail_id"] 30 | attachements_path=["absolute_attachement1_with_extension","absolute_attachement2_with_extension"]# for eg : /home/arul/z/nnk.txt, /home/arul/python/sam.py 31 | 32 | 33 | 34 | def mail(to, subject, text, attach=[]): 35 | msg = MIMEMultipart() 36 | 37 | msg['From'] = gmail_user 38 | msg['To'] = to 39 | msg['Subject'] = subject 40 | 41 | msg.attach(MIMEText(text)) 42 | 43 | try: 44 | 45 | for i in range(len(attach)): 46 | part = MIMEBase('application', 'octet-stream') 47 | part.set_payload(open(attach[i], 'rb').read()) 48 | Encoders.encode_base64(part) 49 | part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(str(attach[i]))) 50 | msg.attach(part) 51 | except: 52 | print " The attachments doesnt exist in the path %s and %s" % (attach[0],attach[1]) 53 | 54 | 55 | mailServer = smtplib.SMTP("smtp.gmail.com", 587) 56 | mailServer.ehlo() 57 | mailServer.starttls() 58 | mailServer.ehlo() 59 | mailServer.login(gmail_user, gmail_pwd) 60 | mailServer.sendmail(gmail_user, to, msg.as_string()) 61 | # Should be mailServer.quit(), but that crashes... 62 | mailServer.close() 63 | 64 | 65 | 66 | for to_mail_id in (to_mail_ids): 67 | mail(to_mail_id, 68 | subject, 69 | body, 70 | attachements_path) 71 | print "mail sent to :"+to_mail_id 72 | 73 | print "\nmail sent successfully to all\n" 74 | -------------------------------------------------------------------------------- /email/simpleemail.py: -------------------------------------------------------------------------------- 1 | import smtplib 2 | 3 | def prompt(prompt): 4 | return raw_input(prompt).strip() 5 | 6 | fromaddr = 'brent@autoshepherd.com' 7 | toaddrs = 'brent@autoshepherd.com' 8 | 9 | 10 | # Add the From: and To: headers at the start! 11 | msg = ("From: %s\r\nTo: %s\r\n\r\n" 12 | % (fromaddr, ", ".join(toaddrs))) 13 | line = 'Test Message' 14 | msg = msg + line 15 | 16 | server = smtplib.SMTP('localhost') 17 | server.set_debuglevel(1) 18 | server.sendmail(fromaddr, toaddrs, msg) 19 | server.quit() 20 | -------------------------------------------------------------------------------- /exercises/fibonacci.py: -------------------------------------------------------------------------------- 1 | def fib(): 2 | i1 = 0 3 | i2 = 1 4 | yield i1 5 | yield i2 6 | while True: 7 | yield i1 + i2 8 | i1 = i2 9 | i2 = i1 + i2 10 | 11 | y = iter(fib()) 12 | while True: 13 | print '%s, ' % y.next() -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/._.DS_Store: -------------------------------------------------------------------------------- 1 | Mac OS X  2 R@ -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/appendixA-scripts.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/expert_programming_book_code/scripts/appendixA-scripts.txt -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/appendixB-scripts.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/expert_programming_book_code/scripts/appendixB-scripts.txt -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/chapter1-scripts.txt: -------------------------------------------------------------------------------- 1 | # python startup file 2 | import readline 3 | import rlcompleter 4 | import atexit 5 | import os 6 | # tab completion 7 | readline.parse_and_bind('tab: complete') 8 | # history file 9 | histfile = os.path.join(os.environ['HOME'], '.pythonhistory') 10 | try: 11 | readline.read_history_file(histfile) 12 | except IOError: 13 | pass 14 | atexit.register(readline.write_history_file, histfile) 15 | 16 | 17 | del os, histfile, readline, rlcompleter 18 | >>> import md5 19 | >>> md5. 20 | md5.__class__ md5.__file__ md5.__name__ md5.__repr__ md5.digest_size 21 | md5.__delattr__ md5.__getattribute__ 22 | md5.__new__ md5.__setattr__ md5.md5 23 | md5.__dict__ md5.__hash__ md5.__reduce__ md5.__str__ md5.new 24 | md5.__doc__ md5.__init__ md5.__reduce_ex__ md5.blocksize 25 | tarek@luvdit:~$ ipython 26 | Python 2.4.4 (#2, Apr 27 | 5 2007, 20:11:18) 28 | Type "copyright", "credits" or "license" for more information. 29 | IPython 0.7.2 -- An enhanced Interactive Python. 30 | ? -> Introduction to IPython's features. 31 | %magic 32 | -> Information about IPython's 'magic' % functions. 33 | help -> Python's own help system. 34 | object? -> Details about 'object'. ?object also works, ?? prints more. 35 | In [1]: 36 | [build] 37 | compiler = mingw32 38 | 39 | ./configure --enable-multibyte 40 | make 41 | sudo make install 42 | set encoding=utf8 43 | set paste 44 | set expandtab 45 | set textwidth=0 46 | set tabstop=4 47 | set softtabstop=4 48 | set shiftwidth=4 49 | set autoindent 50 | set backspace=indent,eol,start 51 | set incsearch 52 | set ignorecase 53 | set ruler 54 | set wildmenu 55 | set commentstring=\ #\ %s 56 | set foldlevel=0 57 | set clipboard+=unnamed 58 | syntax on 59 | -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/chapter10-scripts.txt: -------------------------------------------------------------------------------- 1 | We have a parse function:: 2 | The parser knows how to return a feed content, with 3 | the `parse` function, available as a top-level function:: 4 | ===== 5 | Title 6 | ===== 7 | 8 | 9 | Section 1 10 | ========= 11 | This *word* has emphasis. 12 | Section 2 13 | ========= 14 | Subsection 15 | :::::::::: 16 | Text. 17 | $ more text.txt 18 | Title 19 | ===== 20 | content. 21 | $ rst2html.py text.txt > text.html 22 | $ more text.html 23 | 24 | ... 25 | 26 | 27 | ... 28 | 29 | 30 |
31 |

Title

32 |

content.

33 |
34 | 35 | 36 | ===== 37 | Title 38 | ===== 39 | Section 1 40 | ========= 41 | xxx 42 | Subsection A 43 | ------------ 44 | xxx 45 | Subsection B 46 | ------------ 47 | xxx 48 | Section 2 49 | ========= 50 | xxx 51 | Subsection C 52 | ------------ 53 | xxx 54 | Bullet list: 55 | - one 56 | - two 57 | - three 58 | Enumerated list: 59 | 60 | 61 | 1. one 62 | 2. two 63 | #. auto-enumerated 64 | Definition list: 65 | one 66 | one is a number. 67 | two 68 | two is also a number. 69 | This is a code example 70 | :: 71 | >>> 1 + 1 72 | 2 73 | Let's continue our text 74 | This is a code example:: 75 | 76 | 77 | >>> 1 + 1 78 | 2 79 | Try `Plone CMS`_, it is great ! It is based on Zope_. 80 | .. _`Plone CMS`: http://plone.org 81 | .. _Zope: http://zope.org 82 | ===== 83 | Title 84 | ===== 85 | Section 1 86 | ========= 87 | xxx 88 | 89 | 90 | Subsection A 91 | ------------ 92 | xxx 93 | Subsection B 94 | ------------ 95 | -> go back to `Subsection A`_ 96 | Section 2 97 | ========= 98 | xxx 99 | ========================================= 100 | Database specifications for atomisator.db 101 | ========================================= 102 | :Author: Tarek 103 | :Tags: database mapping sql 104 | :abstract: 105 | Write here a small abstract about your design document. 106 | .. contents :: 107 | Who should read this ? 108 | :::::::::::::::::::::: 109 | Explain here who is the target readership. 110 | Content 111 | ::::::: 112 | Write your document here. Do not hesitate to split it in several sections. 113 | References 114 | :::::::::: 115 | Put here references, and links to other documents. 116 | ======================== 117 | How to use atomisator.db 118 | ======================== 119 | :Author: Tarek 120 | :Tags: atomisator db 121 | .. contents :: 122 | Who should read this ? 123 | :::::::::::::::::::::: 124 | Explain here who is the target readership. 125 | 126 | 127 | 128 | Prerequisites 129 | ::::::::::::: 130 | Put here the prerequisites for people to follow this recipe. 131 | Problem 132 | ::::::: 133 | Explain here the problem resolved in a few sentences. 134 | Solution 135 | :::::::: 136 | Put here the solution. 137 | References 138 | :::::::::: 139 | Put here references, and links to other recipes. 140 | ========== 141 | Operations 142 | ========== 143 | 144 | 145 | This section contains operations documents : 146 | ======== 147 | Cookbook 148 | ======== 149 | Welcome to the CookBook. 150 | Available recipes: 151 | .. toctree:: 152 | 153 | 154 | :glob: 155 | * 156 | ======= 157 | session 158 | ======= 159 | .. module:: db.session 160 | The module session... 161 | ======= 162 | session 163 | ======= 164 | .. module:: db.session 165 | .. index:: 166 | Database Access 167 | Session 168 | The module session... 169 | :mod:`db.session` 170 | -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/chapter7-scripts.txt: -------------------------------------------------------------------------------- 1 | [buildout] 2 | parts = 3 | part1 4 | part2 5 | [part1] 6 | recipe = my.recipe1 7 | [part2] 8 | recipe = my.recipe2 9 | [buildout] 10 | parts = 11 | develop = 12 | /home/tarek/dev/atomisator.feed 13 | find-links = 14 | http://acme.com/packages/index 15 | [buildout] 16 | parts = 17 | develop = 18 | /home/tarek/dev/atomisator.feed 19 | [buildout] 20 | parts = 21 | test 22 | develop = 23 | /home/tarek/dev/atomisator.feed 24 | [test] 25 | recipe = zc.recipe.egg 26 | eggs = 27 | nose 28 | [buildout] 29 | parts = 30 | test 31 | other 32 | develop = 33 | /home/tarek/dev/atomisator.feed 34 | 35 | 36 | 37 | [test] 38 | recipe = zc.recipe.egg 39 | eggs = 40 | nose 41 | [other] 42 | recipe = zc.recipe.egg 43 | eggs = 44 | elementtree 45 | PIL 46 | ... 47 | [buildout] 48 | develop = 49 | ../packages/atomisator.main 50 | ../packages/atomisator.db 51 | ../packages/atomisator.feed 52 | ../packages/atomisator.parser 53 | parts = 54 | test 55 | [test] 56 | recipe = pbp.recipe.noserunner 57 | eggs = 58 | atomisator.main 59 | atomisator.db 60 | atomisator.feed 61 | atomisator.parser 62 | ... 63 | [atomisator-configuration] 64 | recipe = atomisator.recipe.installer 65 | 66 | sites = 67 | sample1.xml 68 | sample2.xml 69 | 70 | database = sqlite:/// 71 | 72 | title = My Feed 73 | description = The feed 74 | link = the link 75 | 76 | file = ${buildout:directory}/var/atomisator.xml 77 | ... 78 | $ python setup.py sdist 79 | running sdist 80 | ... 81 | Writing atomisator.db-0.1.0/setup.cfg 82 | tar -cf dist/atomisator.db-0.1.0.tar atomisator.db-0.1.0 83 | gzip -f9 dist/atomisator.db-0.1.0.tar 84 | removing 'atomisator.db-0.1.0' (and everything under it) 85 | $ ls dist/ 86 | atomisator.db-0.1.0.tar.gz 87 | [buildout] 88 | extends = buildout.cfg 89 | develop = 90 | parts = 91 | atomisator 92 | eggs 93 | download-cache = downloads 94 | [atomisator] 95 | recipe = zc.recipe.eggs 96 | eggs = 97 | atomisator.main 98 | atomisator.db 99 | atomisator.feed 100 | atomisator.parser 101 | $ bin/buildout -c release.cfg -v 102 | Installing 'zc.buildout', 'setuptools'. 103 | 104 | 105 | ... 106 | Installing atomisator. 107 | Installing 'atomisator.db', 'atomisator.feed', 'atomisator.parser', 'atomisator.main'. 108 | ... 109 | Picked: setuptools = 0.6c8 110 | -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/chapter8-scripts.txt: -------------------------------------------------------------------------------- 1 | [collections] 2 | repositories/ = repositories/ 3 | [web] 4 | style = gitweb 5 | push_ssl = false 6 | AddHandler cgi-script .cgi 7 | ScriptAliasMatch ^/hg(.*) /home/mercurial/atomisator/hgwebdir.cgi$1 8 | 9 | Options ExecCGI FollowSymLinks 10 | 11 | 12 | AllowOverride None 13 | AuthType Basic 14 | AuthName "Mercurial" 15 | AuthUserFile /home/mercurial/atomisator/passwords 16 | 17 | Require valid-user 18 | 19 | 20 | 21 | ServerName hg-atomisator.ziade.org 22 | CustomLog /home/mercurial/atomisator/access_log combined 23 | ErrorLog 24 | /home/mercurial/atomisator/error.org.log 25 | AddHandler cgi-script .cgi 26 | RewriteEngine On 27 | DocumentRoot /home/mercurial/atomisator 28 | ScriptAliasMatch ^/(.*) /home/mercurial/atomisator/hgwebdir.cgi/$1 29 | 30 | Options ExecCGI FollowSymLinks 31 | AllowOverride None 32 | AuthType Basic 33 | AuthName "Mercurial" 34 | AuthUserFile /home/mercurial/atomisator/passwords 35 | 36 | Require valid-user 37 | 38 | 39 | 40 | [ui] 41 | username = Tarek Ziade 42 | 43 | 44 | 45 | [paths] 46 | default 47 | = http://tarek:secret@atomisator.ziade.org/hg/unstable 48 | unstable = http://tarek:secret@atomisator .ziade.org/hg/unstable 49 | stable = http://tarek:secret@atomisator.ziade.org/hg/stable 50 | [buildout] 51 | parts = 52 | buildmaster 53 | linux 54 | atomisator 55 | [buildmaster] 56 | recipe = collective.buildbot:master 57 | project-name = Atomisator project buildbot 58 | project-url = http://atomisator.ziade.org 59 | port = 8999 60 | wport = 9000 61 | url = http://atomisator.ziade.org/buildbot 62 | slaves = 63 | linux ty54ddf32 64 | [linux] 65 | recipe = collective.buildbot:slave 66 | host = localhost 67 | port = ${buildmaster:port} 68 | password = ty54ddf32 69 | 70 | 71 | [atomisator] 72 | recipe = collective.buildbot:project 73 | slave-names = linux 74 | repository=http://hg-atomisator.ziade.org/unstable 75 | vcs = hg 76 | build-sequence = 77 | ./build 78 | test-sequence = 79 | buildout/bin/nosetests 80 | 81 | email-notification-sender = tarek@ziade.org 82 | email-notification-recipient = tarek@ziade.org 83 | [poller] 84 | recipe = collective.buildbot:poller 85 | repository=http://hg-atomisator.ziade.org/unstable 86 | vcs = hg 87 | user=anonymous 88 | #!/bin/sh 89 | cd buildout 90 | python bootstrap.py 91 | bin/buildout -v 92 | [web] 93 | style = gitweb 94 | description = Unstable branch 95 | contact = Tarek 96 | push_ssl = false 97 | allow_push = * 98 | [hooks] 99 | changegroup.buildbot = python:buildbot.changes.hgbuildbot.hook 100 | [hgbuildbot] 101 | master = atomisator.ziade.org:8999 102 | 103 | ServerName atomisator-buildbot.ziade.org 104 | CustomLog /var/log/apache2/bot-access_log combined 105 | ErrorLog 106 | /var/log/apache2/bot-error.org.log 107 | RewriteEngine On 108 | RewriteRule ^(.*) http://localhost:9000/$1 109 | 110 | -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/chapter9-scripts.txt: -------------------------------------------------------------------------------- 1 | [buildout] 2 | parts = 3 | buildmaster 4 | linux 5 | trac 6 | [buildmaster] 7 | ... 8 | [buildslave] 9 | ... 10 | [trac] 11 | recipe = pbp.recipe.trac 12 | project-name = Atomisator 13 | project-url = ${buildmaster:project-url} 14 | repos-type = hg 15 | repos-path = /home/mercurial/atomisator/repositories/unstable 16 | buildbot-url = http://buildbot-atomisator.ziade.org/ 17 | components = 18 | atomisator.db tarek 19 | atomisator.feed tarek 20 | atomisator.main tarek 21 | atomisator.parser tarek 22 | pbp.recipe.trac tarek 23 | header-logo = atomisator.png 24 | 25 | ServerName atomisator.ziade.org 26 | 27 | SetHandler mod_python 28 | PythonHandler trac.web.modpython_frontend 29 | PythonOption TracEnv /home/mercurial/atomisator/buildbot/parts/trac 30 | PythonOption TracUriRoot / 31 | 32 | 33 | PythonPath "sys.path + ['/home/mercurial/atomisator/buildbot/parts/trac', '/home/mercurial/atomisator/buildbot/eggs']" 34 | 35 | 36 | AuthType Basic 37 | AuthName "Trac login" 38 | AuthUserFile /home/mercurial/atomisator/passwords 39 | Require valid-user 40 | 41 | 42 | -------------------------------------------------------------------------------- /expert_programming_book_code/scripts/introduction-scripts.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/expert_programming_book_code/scripts/introduction-scripts.txt -------------------------------------------------------------------------------- /feedparser/show_feedparser.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # [SNIPPET_NAME: Parse an RSS feed] 3 | # [SNIPPET_CATEGORIES: feedparser] 4 | # [SNIPPET_DESCRIPTION: Parse and iterate over the items in an RSS feed] 5 | # [SNIPPET_AUTHOR: Tim Voet ] 6 | # [SNIPPET_DOCS: http://www.feedparser.org/docs/introduction.html] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | import feedparser 10 | 11 | feed_url = 'http://www.jonobacon.org/feed/' 12 | 13 | f = feedparser.parse( feed_url ) 14 | print "Feed Title %s" % f.feed.title 15 | for entry in f.entries: 16 | print "Title: %s" % entry.title 17 | print "link: %s" % entry.link 18 | print "Title: %s" % entry.title 19 | 20 | -------------------------------------------------------------------------------- /functools/autopartial.py: -------------------------------------------------------------------------------- 1 | import inspect 2 | from functools import partial 3 | 4 | def autopartial(func): 5 | """ 6 | Will make a function return a partial if not all the arguments are 7 | returned:: 8 | 9 | >>> @autopartial 10 | ... def squares(x, y, z): 11 | ... return x**2 + y**2 + z**2 12 | >>> squares(2, 3) 13 | 14 | >>> squares(1, 2, 3) 15 | 14 16 | >>> squares(1)(2)(3) 17 | 14 18 | >>> squares(1, 2, z=3) 19 | 14 20 | >>> squares(1, 2)(z=3) 21 | 14 22 | """ 23 | argnames, vararg, kwarg, defaults = inspect.getargspec(func) 24 | def replacement(*args, **kw): 25 | unused = argnames[len(args):] 26 | for name in kw: 27 | if name in unused: 28 | unused.remove(name) 29 | if unused: 30 | return partial(replacement, *args, **kw) 31 | return func(*args, **kw) 32 | return replacement 33 | 34 | if __name__ == '__main__': 35 | import doctest 36 | doctest.testmod(optionflags=doctest.ELLIPSIS) 37 | 38 | -------------------------------------------------------------------------------- /functools/recursion.py: -------------------------------------------------------------------------------- 1 | def sort( a ): 2 | if len(a) == 1: return a 3 | part1= sort( a[:len(a)//2] ) 4 | part2= sort( a[len(a)//2:] ) 5 | return merge( part1, part2 ) 6 | 7 | #Merge, defined recursively. 8 | 9 | def merge( a, b ): 10 | if len(b) == 0: return a 11 | if len(a) == 0: return b 12 | if a[0] < b[0]: 13 | return [ a[0] ] + merge(a[1:], b) 14 | else: 15 | return [ b[0] ] + merge(a, b[1:]) 16 | 17 | #Linear search, defined recursively. 18 | 19 | def find( element, sequence ): 20 | if len(sequence) == 0: return False 21 | if element == sequence[0]: return True 22 | return find( element, sequence[1:] ) 23 | 24 | #Binary search, defined recursively. 25 | 26 | def binsearch( element, sequence ): 27 | if len(sequence) == 0: return False 28 | mid = len(sequence)//2 29 | if element < mid: 30 | return binsearch( element, sequence[:mid] ) 31 | else: 32 | return binsearch( element, sequence[mid:] ) 33 | 34 | -------------------------------------------------------------------------------- /functools/recursive.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding:utf-8 3 | 4 | def fact(n): 5 | if n == 0: 6 | return 1 7 | else: 8 | return n * fact(n-1) 9 | 10 | def main(): 11 | find_factorial = input('Find the Factorial for what number? ') 12 | print(fact(find_factorial)) 13 | 14 | if __name__ == '__main__': 15 | main() 16 | -------------------------------------------------------------------------------- /generators/generator1.py: -------------------------------------------------------------------------------- 1 | def firstn(n): 2 | num = 0 3 | while num < n: 4 | yield num 5 | num += 1 6 | 7 | if __name__ == '__main__': 8 | sum_of_first_n = sum(firstn(1000000)) 9 | print(sum_of_first_n) -------------------------------------------------------------------------------- /geo_tools/get_google_geolocation.py: -------------------------------------------------------------------------------- 1 | def get_geolocation(result): 2 | address = ["", "", "", ""] 3 | for obj in result["address_components"]: 4 | if "locality" in obj["types"]: 5 | address[0] = obj["long_name"] 6 | if "administrative_area_level_1" in obj["types"]: 7 | address[1] = obj["long_name"] 8 | if "country" in obj["types"]: 9 | address[2] = obj["long_name"] 10 | return tuple(address) 11 | 12 | import requests 13 | import json 14 | 15 | url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=40,-76&sensor=false" 16 | print get_geolocation(json.loads(requests.get(url).text)["results"][0]) 17 | url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=50,13&sensor=false" 18 | print get_geolocation(json.loads(requests.get(url).text)["results"][0]) -------------------------------------------------------------------------------- /geo_tools/haversine.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import math 4 | 5 | def haversine(lat1, lon1, lat2, lon2): 6 | R = 6372800 7 | # In meters 8 | dLat = math.radians(lat2 - lat1) 9 | dLon = math.radians(lon2 - lon1) 10 | lat1 = math.radians(lat1) 11 | lat2 = math.radians(lat2) 12 | 13 | a = math.sin(dLat / 2) * math.sin(dLat / 2) + math.sin(dLon / 2) * math.sin(dLon / 2) * math.cos(lat1) * math.cos(lat2) 14 | c = 2 * math.asin(math.sqrt(a)) 15 | return R * c 16 | 17 | if __name__ == '__main__': 18 | lat1 = 34.091510 19 | lon1 = -118.364829 20 | lat2 = 64.806881 21 | lon2 = -18.083496 22 | print(haversine(lat1, lon1, lat2, lon2)) 23 | -------------------------------------------------------------------------------- /get-site-packages.sh: -------------------------------------------------------------------------------- 1 | python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" 2 | -------------------------------------------------------------------------------- /gpodder/extension_example.py: -------------------------------------------------------------------------------- 1 | # Use a logger for debug output - this will be managed by gPodder 2 | import logging 3 | logger = logging.getLogger(__name__) 4 | 5 | # Provide some metadata that will be displayed in the gPodder GUI 6 | __title__ = 'Hello World Extension' 7 | __description__ = 'Explain in one sentence what this extension does.' 8 | __only_for__ = 'gtk, cli, qml' 9 | __authors__ = 'Thomas Perl ' 10 | 11 | class gPodderExtension: 12 | # The extension will be instantiated the first time it's used 13 | # You can do some sanity checks here and raise an Exception if 14 | # you want to prevent the extension from being loaded.. 15 | def __init__(self, container): 16 | self.container = container 17 | 18 | # This function will be called when the extension is enabled or 19 | # loaded. This is when you want to create helper objects or hook 20 | # into various parts of gPodder. 21 | def on_load(self): 22 | logger.info('Extension is being loaded.') 23 | print '='*40 24 | print 'container:', self.container 25 | print 'container.manager:', self.container.manager 26 | print 'container.config:', self.container.config 27 | print 'container.manager.core:', self.container.manager.core 28 | print 'container.manager.core.db:', self.container.manager.core.db 29 | print 'container.manager.core.config:', self.container.manager.core.config 30 | print 'container.manager.core.model:', self.container.manager.core.model 31 | print '='*40 32 | 33 | # This function will be called when the extension is disabled or 34 | # when gPodder shuts down. You can use this to destroy/delete any 35 | # objects that you created in on_load(). 36 | def on_unload(self): 37 | logger.info('Extension is being unloaded.') -------------------------------------------------------------------------------- /http/post_binary_data_via_http.py: -------------------------------------------------------------------------------- 1 | req = urllib2.Request("http://example.com", data, {'Content-Type': 'application/octet-stream'}) 2 | urllib2.urlopen(req) 3 | -------------------------------------------------------------------------------- /idomatic/forloop_else.py: -------------------------------------------------------------------------------- 1 | my_items = [1, 2, 3, 4, 5] 2 | 3 | for myi in my_items: 4 | if isinstance(myi, basestring): 5 | print('is a string') 6 | break 7 | else: 8 | print('no items are strings') -------------------------------------------------------------------------------- /inspect/getmembers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Inspect object at runtime] 4 | # [SNIPPET_CATEGORIES: inspect] 5 | # [SNIPPET_DESCRIPTION: inspect the objects live at runtime. In .NET world it is called Reflection] 6 | # [SNIPPET_AUTHOR: Manish Sinha ] 7 | # [SNIPPET_DOCS: http://docs.python.org/library/inspect.html] 8 | # [SNIPPET_LICENSE: GPL] 9 | 10 | import inspect 11 | 12 | # Define a sample class 13 | class Example(): 14 | """A method named sample""" 15 | def sample(self): 16 | pass 17 | 18 | """A method named foo and takes on argument""" 19 | def foo(self, arg): 20 | return "Testing"; 21 | 22 | """A Field named a""" 23 | a = "foo" 24 | 25 | objInst= Example() 26 | 27 | # Get a list of members of this instance including those from base object 28 | memberList= inspect.getmembers(objInst) 29 | for a in memberList: 30 | print(a) 31 | 32 | print(inspect.ismethod(objInst.sample)) # Returns True 33 | 34 | print(inspect.isfunction(objInst.sample)) # Returns False. Method and Function are different 35 | 36 | print(inspect.ismodule(objInst)) # Returns False 37 | 38 | print(inspect.isclass(Example)) # Returns true 39 | -------------------------------------------------------------------------------- /introspection/introspect.py: -------------------------------------------------------------------------------- 1 | def info(object, spacing=10, collapse=1): 2 | """Print methods and doc strings. 3 | 4 | Takes module, class, list, dictionary, or string.""" 5 | methodList = [method for method in dir(object) if callable(getattr(object, method))] 6 | processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s) 7 | print "\n".join(["%s %s" % 8 | (method.ljust(spacing), 9 | processFunc(str(getattr(object, method).__doc__))) 10 | for method in methodList]) 11 | 12 | if __name__ == "__main__": 13 | print info.__doc__ 14 | -------------------------------------------------------------------------------- /introspection/print_function_info.py: -------------------------------------------------------------------------------- 1 | def f_info(): 2 | print "Running function: '%s' Argument: %s"%(sys._getframe(1).f_code.co_name,sys._getframe(1).f_locals) 3 | 4 | def check_for_dir(d): 5 | f_info() 6 | return os.path.isdir(dir) 7 | -------------------------------------------------------------------------------- /itertools/itertools_example.py: -------------------------------------------------------------------------------- 1 | # Enjoy Doing It Wrong, 2009. 2 | # License: WTFPL 3 | 4 | import itertools 5 | 6 | # found using stackoverflow.com/users 7 | bounds = [i*35 for i in (3307, 186, 93, 59, 41, 31, 23)] +\ 8 | [19*35+20, 16*35+20, 13*35+20, 12*35+15, 6*35+4, 4*35-4] + [42, 8, 0] 9 | 10 | # His stats 11 | joel_stats = [8, 10, 11, 11, 13, 14, 14, 15, 15, 16, 17, 19, 21, 26, 30] 12 | labels = [i for i in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 50)] 13 | 14 | gaps = [] 15 | prev = bounds[0] 16 | for bound in bounds: 17 | gaps.append(prev-bound) 18 | prev = bound 19 | 20 | less_than = gaps[1:-1] 21 | 22 | def reputation_repartition(l): 23 | def careers_subscribers(l): 24 | return [n*s*.01 for n, s in itertools.izip(l, joel_stats)] 25 | subscribers = careers_subscribers(l) 26 | -------------------------------------------------------------------------------- /lambda/lambda_example.py: -------------------------------------------------------------------------------- 1 | """ 2 | The lambda operator built in to the Python language provides a method to create 3 | anonymous functions. This makes it easier to pass simple functions as parameters 4 | or assign them to variable names. The lambda operator uses the following syntax 5 | to define the function: lambda : The term args refers to a list of arguments 6 | that get passed to the function. The term expression can be any legal Python 7 | expression. The following code shows an example of using the lambda operator to 8 | assign an anonymous function to a variable:'); 9 | """ 10 | >>>bigger = lambda a, b : a > b 11 | >>>print bigger(1,2) 12 | False 13 | >>>print bigger(2,1) 14 | True 15 | cat >> outfile.txt 2>&1 16 | -------------------------------------------------------------------------------- /localization/locale.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | # ex: 09-06-12-10-01 4 | print datetime.datetime.now().strftime("%y-%m-%d-%H-%M") 5 | 6 | #Directive Meaning Notes 7 | #%a Locale’s abbreviated weekday name. 8 | #%A Locale’s full weekday name. 9 | #%b Locale’s abbreviated month name. 10 | #%B Locale’s full month name. 11 | #%c Locale’s appropriate date and time representation. 12 | #%d Day of the month as a decimal number [01,31]. 13 | #%f Microsecond as a decimal number [0,999999], zero-padded on the left (1) 14 | #%H Hour (24-hour clock) as a decimal number [00,23]. 15 | #%I Hour (12-hour clock) as a decimal number [01,12]. 16 | #%j Day of the year as a decimal number [001,366]. 17 | #%m Month as a decimal number [01,12]. 18 | #%M Minute as a decimal number [00,59]. 19 | #%p Locale’s equivalent of either AM or PM. (2) 20 | #%S Second as a decimal number [00,61]. (3) 21 | #%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (4) 22 | #%w Weekday as a decimal number [0(Sunday),6]. 23 | #%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (4) 24 | #%x Locale’s appropriate date representation. 25 | #%X Locale’s appropriate time representation. 26 | #%y Year without century as a decimal number [00,99]. 27 | #%Y Year with century as a decimal number. 28 | #%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). (5) 29 | #%Z Time zone name (empty string if the object is naive). 30 | #%% A literal ''%'' character. 31 | -------------------------------------------------------------------------------- /logging/logger_setup.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import logging 3 | DEBUG=True 4 | 5 | 6 | def main(): 7 | logger = logging.getLogger('my_app') 8 | 9 | logtofile = logging.FileHandler('logs.log', mode='w+', encoding=None, delay=False) 10 | logtofile.setLevel(logging.INFO) 11 | 12 | logtoconsole = logging.StreamHandler(stream=sys.stdout) 13 | logtoconsole.setLevel(logging.DEBUG) 14 | 15 | formatter = logging.Formatter('[WEBDEPLOY] %(message)s') 16 | second_formatter = logging.Formatter('[SECOND_FORMATTER] %(message)s') 17 | 18 | logtoconsole.setFormatter(formatter) 19 | logtofile.setFormatter(second_formatter) 20 | 21 | logger.addHandler(logtoconsole) 22 | logger.addHandler(logtofile) 23 | 24 | logger.setLevel(logging.DEBUG) 25 | logger.info('The Info Logging') 26 | logger.debug('Debug logging (should not show up in File)') 27 | 28 | if __name__ == '__main__': 29 | main() 30 | -------------------------------------------------------------------------------- /meta_programming/proxy_obj.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | def get_proxy(obj): 5 | class Proxy(object): 6 | def __getattr__(self, name): 7 | return getattr(obj, name) 8 | def __setattr__(self, name, value): 9 | setattr(obj, name, value) 10 | def __delattr__(self, name): 11 | delattr(obj, name) 12 | return Proxy() -------------------------------------------------------------------------------- /mocking/nested_calls.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | #! /usr/bin/python 5 | 6 | import unittest 7 | from mock import Mock, MagicMock 8 | from itertools import chain 9 | 10 | 11 | def trigger_handlers(top_level): 12 | ctx = {} 13 | for event in chain([top_level], top_level.extra): 14 | if event.handler: 15 | ctx.update({'event': event}) 16 | event.handler.trigger(ctx) 17 | 18 | 19 | class TestNestedCalls(unittest.TestCase): 20 | 21 | def test_top_level(self): 22 | top_level = MagicMock() 23 | top_level.extra = [] 24 | 25 | trigger_handlers(top_level) 26 | 27 | ctx = {'event': top_level} 28 | top_level.handler.trigger.assert_called_once_with(ctx) 29 | 30 | def test_extra(self): 31 | top_level = MagicMock() 32 | top_level.handler = None 33 | nested = Mock() 34 | top_level.extra = [nested] 35 | 36 | trigger_handlers(top_level) 37 | 38 | ctx = {'event': nested} 39 | nested.handler.trigger.assert_called_once_with(ctx) 40 | 41 | def test_top_level_and_extra(self): 42 | top_level = MagicMock() 43 | nested = Mock() 44 | top_level.extra = [nested] 45 | 46 | trigger_handlers(top_level) 47 | 48 | print 49 | print "Mock: top_level: {0}".format(top_level) 50 | print "Call Args, top_level.handler.trigger: {0}".format(top_level.handler.trigger.call_args_list) 51 | print "Mock: nested: {0}".format(nested) 52 | print "Call Args, nested.handler.trigger: {0}".format(nested.handler.trigger.call_args_list) 53 | print 54 | 55 | ctx = {'event': top_level} 56 | top_level.handler.trigger.assert_called_once_with(ctx) 57 | 58 | ctx = {'event': nested} 59 | nested.handler.trigger.assert_called_once_with(ctx) 60 | 61 | 62 | if __name__ == '__main__': 63 | unittest.main() 64 | -------------------------------------------------------------------------------- /musicbrainz_picard/plugins/nofeaturing.py: -------------------------------------------------------------------------------- 1 | PLUGIN_NAME = 'Feat. Artists Removed' 2 | PLUGIN_AUTHOR = 'Lukas Lalinsky, Bryan Toth' 3 | PLUGIN_DESCRIPTION = 'Removes feat. artists from track titles. Substitution is case insensitive.' 4 | PLUGIN_VERSION = "0.2" 5 | PLUGIN_API_VERSIONS = ["0.9.0", "0.10", "0.15", "0.16"] 6 | 7 | from picard.metadata import register_track_metadata_processor 8 | import re 9 | 10 | def remove_featartists(tagger, metadata, release, track): 11 | metadata["title"] = re.sub(r"\s+\(feat. [^)]*\)", "", metadata["title"], flags=re.IGNORECASE) 12 | 13 | register_track_metadata_processor(remove_featartists) -------------------------------------------------------------------------------- /notify-osd/showbubble.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Show notification bubble] 4 | # [SNIPPET_CATEGORIES: Notify OSD] 5 | # [SNIPPET_DESCRIPTION: Show a simple notification bubble] 6 | # [SNIPPET_AUTHOR: Jono Bacon ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | import pynotify 10 | import os 11 | 12 | pynotify.init('someName') 13 | 14 | imageURI = 'file://' + os.path.abspath(os.path.curdir) + '/logo.png' 15 | 16 | n = pynotify.Notification("message name", "message", imageURI) 17 | n.show() 18 | -------------------------------------------------------------------------------- /objects/sort_object_by_element.py: -------------------------------------------------------------------------------- 1 | import operator 2 | 3 | class Thing(object): 4 | 5 | def __init__(self, num): 6 | self.num = num 7 | 8 | def __repr__(self): 9 | return str(self.num) 10 | 11 | def nonesorter(a): 12 | if a.num is None: 13 | return "" 14 | return a.num 15 | 16 | thing_list = list() 17 | for x in range(10, 0, -1): 18 | thing_list.append(Thing(x)) 19 | thing_list.append(Thing(None)) 20 | 21 | print(thing_list) 22 | 23 | sorted_things = sorted(thing_list, key=nonesorter) 24 | print(sorted_things) -------------------------------------------------------------------------------- /os/envvar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Read an environmental variable] 4 | # [SNIPPET_CATEGORIES: os] 5 | # [SNIPPET_DESCRIPTION: Read and print a given environmental variable] 6 | # [SNIPPET_DOCS: http://docs.python.org/library/os.html] 7 | # [SNIPPET_AUTHOR: Jono Bacon ] 8 | # [SNIPPET_LICENSE: GPL] 9 | 10 | import os 11 | 12 | # display the USERNAME environmental variable 13 | print os.getenv('USERNAME') 14 | 15 | -------------------------------------------------------------------------------- /os/find_file.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Find a file] 4 | # [SNIPPET_CATEGORIES: os] 5 | # [SNIPPET_DESCRIPTION: Recursively search directories for a file] 6 | # [SNIPPET_AUTHOR: Andy Breiner ] 7 | # [SNIPPET_LICENSE: GPL] 8 | # [SNIPPET_DOCS: http://docs.python.org/library/os.html, http://diveintopython.org/file_handling/os_module.html] 9 | 10 | import os 11 | import sys 12 | 13 | def look_in_directory(directory, file_to_find): 14 | """Loop through the current directory for the file, if the current item 15 | is a directory, it recusively looks through that folder""" 16 | 17 | # Loop over all the items in the directory 18 | for f in os.listdir(directory): 19 | # Uncomment the line below to see how the files/folders are searched 20 | # print "Looking in " + directory 21 | 22 | # If the item is a file check to see if it is what we are looking 23 | # for, if it is, print that we found it and return true 24 | if os.path.isfile(os.path.join(directory, f)): 25 | if f == file_to_find: 26 | print "Found file: " + os.path.join(directory, f) 27 | return True 28 | 29 | # If the item is a directory, we recursivley look through that 30 | # directory if it is found, we again return true 31 | if os.path.isdir(os.path.join(directory, f)): 32 | if look_in_directory(os.path.join(directory, f)): 33 | return True 34 | 35 | if __name__ == '__main__': 36 | 37 | # we will look for the file recursively 38 | file_to_find = sys.argv[1] 39 | 40 | # Start looking in the home directory (~) 41 | # If it is not found, ie it did not return True, tell the user it was "Not 42 | # Found" 43 | if look_in_directory(os.path.expanduser("~"), file_to_find) != True: 44 | print "Not Found" 45 | -------------------------------------------------------------------------------- /os/list_directory.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: List Directories Content] 4 | # [SNIPPET_CATEGORIES: os] 5 | # [SNIPPET_DESCRIPTION: List the content of the home directory] 6 | # [SNIPPET_AUTHOR: Andy Breiner ] 7 | # [SNIPPET_LICENSE: GPL] 8 | # [SNIPPET_DOCS: http://docs.python.org/library/os.html, http://diveintopython.org/file_handling/os_module.html] 9 | 10 | import os 11 | 12 | # expand ~ to /home/ 13 | # also print out the content of the home directory as a list 14 | print os.listdir(os.path.expanduser("~")) 15 | 16 | # Loop over all the items and determine if they are a file or directory and 17 | # then print them out 18 | directory = os.path.expanduser("~") 19 | for f in os.listdir(directory): 20 | if os.path.isfile(os.path.join(directory, f)): 21 | print "File: " + f 22 | if os.path.isdir(os.path.join(directory, f)): 23 | print "Directory: " + f 24 | -------------------------------------------------------------------------------- /os/path/add_cwd_to_syspath: -------------------------------------------------------------------------------- 1 | import os, sys 2 | 3 | def main(): 4 | sys.path.insert(0, os.path.abspath(".") 5 | 6 | if __name__ == '__main__': 7 | print(sys.path) 8 | main() 9 | print(sys.path) -------------------------------------------------------------------------------- /os/path/add_cwd_to_syspath.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | sys.path.insert(0, os.path.abspath(".") 9 | 10 | if __name__ == '__main__': 11 | print(sys.path) 12 | main() 13 | print(sys.path) 14 | -------------------------------------------------------------------------------- /os/projectpath.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Project Path] 4 | # [SNIPPET_CATEGORIES: os] 5 | # [SNIPPET_DESCRIPTION: The common idiom for setting the root path dynamically from settings.python] 6 | # [SNIPPET_DOCS: ] 7 | # [SNIPPET_AUTHOR: Brent Hoover ] 8 | # [SNIPPET_LICENSE: GPL] 9 | 10 | import os 11 | 12 | PROJECT_PATH = os.path.abspath(os.path.dirname(__file__)) 13 | 14 | -------------------------------------------------------------------------------- /os/projectpath.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/os/projectpath.pyc -------------------------------------------------------------------------------- /path/add_cwd_tosyspath.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | sys.path.insert(0, os.path.abspath(".")) 9 | 10 | if __name__ == '__main__': 11 | print(sys.path) 12 | main() 13 | print(sys.path) 14 | -------------------------------------------------------------------------------- /path/get_current_parent.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def get_parent(start_path=None): 5 | if start_path is not None: 6 | current_dir = os.path.abspath(start_path) 7 | else: 8 | current_dir = os.path.abspath(os.path.dirname(__file__)) 9 | base_dir = os.path.split(current_dir)[0] 10 | return 11 | 12 | if __name__ == '__main__': 13 | main() 14 | -------------------------------------------------------------------------------- /path/list_directory.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | 5 | def main(): 6 | # expand ~ to /home/ 7 | # also print out the content of the home directory as a list 8 | print os.listdir(os.path.expanduser("~")) 9 | 10 | # Loop over all the items and determine if they are a file or directory and 11 | # then print them out 12 | directory = os.path.expanduser("~") 13 | for f in os.listdir(directory): 14 | if os.path.isfile(os.path.join(directory, f)): 15 | print "File: " + f 16 | if os.path.isdir(os.path.join(directory, f)): 17 | print "Directory: " + f 18 | 19 | if __name__ == '__main__': 20 | main() 21 | -------------------------------------------------------------------------------- /patterns/borg.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Borg] 4 | # [SNIPPET_CATEGORIES: Patterns] 5 | # [SNIPPET_DESCRIPTION: A module providing a basic implementation of the Borg pattern] 6 | # [SNIPPET_AUTHOR: Scott Ferguson ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | """This pattern ensures that all instances of a particular class share the same state""" 10 | 11 | class Borg: 12 | _borg_state = {} 13 | 14 | def __init__(self): 15 | self.__dict__ = self._borg_state 16 | 17 | # Sample code 18 | 19 | class MyObject(Borg): 20 | _someProperty = 0 21 | 22 | @property 23 | def someProperty(self): 24 | return self._someProperty 25 | 26 | @someProperty.setter 27 | def someProperty(self, value): 28 | self._someProperty = value 29 | 30 | if __name__ == '__main__': 31 | objectA = MyObject() 32 | objectB = MyObject() 33 | 34 | # Set the property of only object A 35 | objectA.someProperty = 5 36 | 37 | # Note that both A and B have the same value for someProperty 38 | print objectA.someProperty 39 | print objectB.someProperty 40 | -------------------------------------------------------------------------------- /patterns/factory.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import abc 5 | import datetime 6 | 7 | 8 | class Affiliate(object): 9 | """ Prototype Class. All common attributes/interface here """ 10 | __metaclass__ = abc.ABCMeta 11 | 12 | date_format = '' 13 | user_format = '' 14 | 15 | def add_date(self, date, url): 16 | return url + '?' + date.strftime(self.date_format) 17 | 18 | 19 | class ParkWhiz(Affiliate): 20 | date_format = '%Y-%m-%d %H:%m:%S.%z' 21 | 22 | 23 | class ParkingPanda(Affiliate): 24 | date_format = '%Y-%m-%d %H:%m:%S.%z' 25 | 26 | 27 | class SpotHero(Affiliate): 28 | date_format = '%Y-%m-%d %H:%m:%S.%z' 29 | 30 | 31 | class AffiliateFactory(object): 32 | 33 | affiliate_objects = dict() 34 | for x in Affiliate.__subclasses__(): 35 | affiliate_objects[x.__name__.lower()] = x 36 | 37 | def get_affiliate(self, typ): 38 | return self.affiliate_objects[typ]() 39 | 40 | affiliate_obj = AffiliateFactory() 41 | affiliates = ['parkwhiz', 'parkingpanda', 'spothero'] 42 | for b in affiliates: 43 | but = affiliate_obj.get_affiliate(b) 44 | now = datetime.datetime.now() 45 | print but.add_date(now, 'http://www.example.com/') 46 | 47 | for x in Affiliate.__subclasses__(): 48 | print x.__name__.lower() -------------------------------------------------------------------------------- /patterns/observer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Observer] 4 | # [SNIPPET_CATEGORIES: Patterns] 5 | # [SNIPPET_DESCRIPTION: A module providing a basic implementation of the Observer pattern] 6 | # [SNIPPET_AUTHOR: Scott Ferguson ] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | """This pattern is great for allowing asynchronous modules of your application to communicate 10 | when something happens. This is very useful in threaded GUI applications when you want a 11 | threaded operation to alert the main UI that an operation has been performed and you have a 12 | tangible result.""" 13 | 14 | # wikipedia: http://en.wikipedia.org/wiki/Observer_pattern 15 | 16 | class Observer: 17 | """An observer simply watches a concrete subject, waiting for something to occur. 18 | The update() function will always be defined in the observer.""" 19 | def update(self): 20 | return; 21 | 22 | class Subject: 23 | """The subject is watched by one or many observers. When something occurs within the 24 | subject, all observers are notified.""" 25 | def __init__(self): 26 | self._observers = [] 27 | 28 | def attach(self, observer): 29 | """Add a new observer to self""" 30 | if not observer in self._observers: 31 | self._observers.append(observer) 32 | 33 | def detach(self, observer): 34 | """Remove an observer from self""" 35 | try: 36 | self._observers.remove(observer) 37 | except ValueError: 38 | pass 39 | 40 | def notify(self, *args): 41 | """Notify all observers that something occurred""" 42 | for observer in self._observers: 43 | observer.update(*args) 44 | 45 | # # # # Example Code from here on # # # # 46 | 47 | """In this sample code the application class creates a widget, and then fires off 48 | an event on that widget. It is always watching the widget, waiting for something 49 | to occur.""" 50 | 51 | import time 52 | 53 | class widget(Subject): 54 | _counter = 1 55 | 56 | def __init__(self, client): 57 | Subject.__init__(self) 58 | self.attach(client) 59 | 60 | def _respond(self): 61 | self.notify(self._counter) 62 | self._counter = self._counter + 1 63 | 64 | def wait_for_response(self): 65 | self._respond() 66 | time.sleep(2) 67 | self._respond() 68 | time.sleep(2) 69 | self._respond() 70 | time.sleep(2) 71 | self._respond() 72 | 73 | class application(Observer): 74 | def __init__(self): 75 | self.button = widget(self) 76 | 77 | def click_widget(self): 78 | self.button.wait_for_response() 79 | 80 | def update(self, *args): 81 | print 'Updated! The subject said: %s' % args[0] 82 | 83 | if __name__ == '__main__': 84 | app = application() 85 | app.click_widget() 86 | -------------------------------------------------------------------------------- /pexpect/pexpect_example.py: -------------------------------------------------------------------------------- 1 | import pexpect 2 | password = open("config/qlive.auth", "r").readline().strip() 3 | p = pexpect.spawn("sudo bash install_dynamicctrl.sh") 4 | i = p.expect([".ssword:*", pexpect.EOF]) 5 | p.sendline(password) 6 | -------------------------------------------------------------------------------- /pmtool/create_change_record.py: -------------------------------------------------------------------------------- 1 | change = Change(change1.product_code = product1, change1.change_user_name = 'TestUser', 2 | transaction_type = 'U', table_name = 'prod', column_name = 'our_prc', old_value = '9.99', 3 | new_value = '9.98', content_type = Product, object_id = product1.prod_cd) 4 | 5 | -------------------------------------------------------------------------------- /postgres/connect_database.py: -------------------------------------------------------------------------------- 1 | import psycopg2 2 | 3 | 4 | def main(): 5 | connection = create_connection('pim', 'brent', 'weasel') 6 | get_user(connection, 'brent@parkme.com') 7 | 8 | 9 | def create_connection(database_name, user, password): 10 | conn = psycopg2.connect(database=database_name, user=user, password=password) 11 | connection = conn.cursor() 12 | return connection 13 | 14 | 15 | def get_user(conn, email): 16 | conn.execute("select username from auth_user where email = %(email)s;", {"email": email}) 17 | for record in conn: 18 | print(record) 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /recipes/reverse.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding:utf-8 3 | 4 | def reverse(s): 5 | print('s is:%s' % s) 6 | if s == '': 7 | return s 8 | else: 9 | return reverse(s[1:]) + s[0] 10 | 11 | def anagrams(s): 12 | if s == '': 13 | return [s] 14 | else: 15 | ans = [] 16 | for w in anagrams(s[1:]): 17 | for pos in range(len(w)+1): 18 | ans.append(w[:pos]+s[0]+w[pos:]) 19 | return ans 20 | 21 | def main(): 22 | to_reverse = raw_input('Anagram what string? ') 23 | print(to_reverse) 24 | print(anagrams(to_reverse)) 25 | 26 | if __name__ == '__main__': 27 | main() -------------------------------------------------------------------------------- /recipes/simple_logging_setup.py: -------------------------------------------------------------------------------- 1 | 2 | logger = logging.getLogger('my_app') 3 | 4 | logtofile = logging.FileHandler('logs.log', mode='w+', encoding=None, delay=False) 5 | logtofile.setLevel(logging.INFO) 6 | 7 | logtoconsole = logging.StreamHandler(stream=sys.stdout) 8 | logtoconsole.setLevel(logging.DEBUG) 9 | 10 | logger.addHandler(logtoconsole) 11 | logger.addHandler(logtofile) 12 | 13 | logger.setLevel(logging.DEBUG) 14 | logger.info('The Info Logging') 15 | logger.debug('Debug logging (should not show up in File)') 16 | 17 | -------------------------------------------------------------------------------- /regex/simple_phone.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | def clean_phone(phone): 4 | _phone = '' 5 | pat = re.compile('^(\(?)(\d{3})(\)?)[^0-9]*(\d{3})[^0-9]*(\d{4})') 6 | x = re.search(pat, phone) 7 | if x is None: 8 | return x 9 | for g in x.groups(): 10 | if len(g) > 2: 11 | _phone += g 12 | return _phone 13 | 14 | 15 | assert(len(clean_phone('3236873265')) == 10) 16 | print clean_phone('323 687 3265') 17 | print clean_phone('323-687-3265') 18 | print clean_phone('(323) 687 3265') 19 | 20 | -------------------------------------------------------------------------------- /regex/tester.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import re 4 | import sys 5 | 6 | 7 | 8 | def renamer(title_string): 9 | """ Using the expand function of re.match to do in place string substitution """ 10 | #Example 'Episode 902 - Al Madrigal.mp4' yields 'Never Not Funny s9e02 - Al Madrigal.mp4' 11 | 12 | matches = re.match(r"^(?PEpisode)(\s+)(?P<season>\d{1})(?P<episode>\d{2})\s-\s(?P<guest>[^.]*)(?P<suffix>.*)", title_string) 13 | new_name = m.expand('Never Not Funny s\g<season>e\g<episode> - \g<guest>\g<suffix>') 14 | return new_name 15 | 16 | 17 | -------------------------------------------------------------------------------- /scripts/syspath.sh: -------------------------------------------------------------------------------- 1 | python -c "from pprint import pprint; from distutils.sysconfig import get_config_vars; pprint(get_config_vars())" 2 | -------------------------------------------------------------------------------- /socket/socket_client.py: -------------------------------------------------------------------------------- 1 | # Client program 2 | 3 | from socket import * 4 | 5 | # Set the socket parameters 6 | host = "localhost" 7 | port = 21567 8 | buf = 1024 9 | addr = (host,port) 10 | 11 | # Create socket 12 | UDPSock = socket(AF_INET,SOCK_DGRAM) 13 | 14 | def_msg = "===Enter message to send to server==="; 15 | print "\n",def_msg 16 | 17 | # Send messages 18 | while (1): 19 | data = raw_input(''>> '') 20 | if not data: 21 | break 22 | else: 23 | if(UDPSock.sendto(data,addr)): 24 | print "Sending message ''",data,"''....."' 25 | -------------------------------------------------------------------------------- /socket/socketserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from socket import * 5 | 6 | for port in range(int(sys.argv[2].split(''-'')[0]), int(sys.argv[2].split(''-'')[1])+1): 7 | try:socket(AF_INET, SOCK_STREAM).connect((sys.argv[1], port)); print "Able to connect to port:", port 8 | except: 9 | pass 10 | -------------------------------------------------------------------------------- /sorting/sort_iter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import bisect 3 | 4 | def sorted_result(iters, cmp_func=None): 5 | """ 6 | Given N sorted iterators, returns an iterator that combines the input in a 7 | sorted manner (according to cmp_func). Continues until all iterators are 8 | exhausted. 9 | 10 | Requires O(N) extra storage space. 11 | """ 12 | if not cmp_func: 13 | func = lambda lhs, rhs: cmp(lhs[0], rhs[0]) 14 | insert = bisect.insort_right 15 | else: 16 | func = lambda lhs, rhs: cmp_func(lhs[0], rhs[0]) 17 | insert = gen_insert_func(func) 18 | state = [] 19 | for iter in iters: 20 | try: 21 | state.append((iter.next(), iter)) 22 | except StopIteration: 23 | pass 24 | state.sort(func) 25 | while state: 26 | next, iter = state.pop(0) 27 | yield next 28 | try: 29 | val = iter.next() 30 | insert(state, (val, iter)) 31 | except StopIteration: 32 | pass 33 | raise StopIteration 34 | 35 | def gen_insert_func(func): 36 | def inserter(a, x, lo=0, hi=None): 37 | if hi is None: 38 | hi = len(a) 39 | while lo < hi: 40 | mid = (lo+hi)//2 41 | if func(x, a[mid]) < 0: 42 | hi = mid 43 | else: 44 | lo = mid+1 45 | a.insert(lo, x) 46 | 47 | return inserter 48 | 49 | if __name__ == '__main__': 50 | l1 = range(0, 20, 2) 51 | l2 = range(1, 20, 5) 52 | l3 = range(1, 20, 3) 53 | print l1 54 | print l2 55 | print l3 56 | print list(sorted_result((iter(l1), iter(l2), iter(l3)))) 57 | 58 | -------------------------------------------------------------------------------- /sphinx/django_autodoc_models.py: -------------------------------------------------------------------------------- 1 | THIS_DIR = os.path.dirname(__file__) 2 | PROJECT_DIR = os.path.join(THIS_DIR, 'relative/path/to/your/project/') 3 | sys.path.append(PROJECT_DIR) 4 | 5 | import inspect 6 | import settings 7 | from django.core.management import setup_environ 8 | from django.utils.html import strip_tags 9 | from django.utils,encoding import force_unicode 10 | 11 | setup_environ(settings) 12 | 13 | 14 | def process_docstring(app, what, name, obj, options, lines): 15 | # This causes import errors if left outside the function 16 | from django.db import models 17 | 18 | # Only look at objects that inherit from Django's base model class 19 | if inspect.isclass(obj) and issubclass(obj, models.Model): 20 | # Grab the field list from the meta class 21 | fields = obj._meta._fields() 22 | 23 | for field in fields: 24 | # Decode and strip any html out of the field's help text 25 | help_text = strip_tags(force_unicode(field.help_text)) 26 | 27 | # Decode and capitalize the verbose name, for use if there isn't 28 | # any help text 29 | verbose_name = force_unicode(field.verbose_name).capitalize() 30 | 31 | if help_text: 32 | # Add the model field to the end of the docstring as a param 33 | # using the help text as the description 34 | lines.append(u':param %s: %s' % (field.attname, help_text)) 35 | else: 36 | # Add the model field to the end of the docstring as a param 37 | # using the verbose name as the description 38 | lines.append(u':param %s: %s' % (field.attname, verbose_name)) 39 | 40 | # Add the field's type to the docstring 41 | lines.append(u':type %s: %s' % (field.attname, type(field).__name__)) 42 | 43 | # Return the extended docstring 44 | return lines 45 | 46 | def setup(app): 47 | # Register the docstring processor with sphinx 48 | app.connect('autodoc-process-docstring', process_docstring) -------------------------------------------------------------------------------- /string/regex_strip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Regex String Stip] 4 | # [SNIPPET_CATEGORIES: string] 5 | # [SNIPPET_DESCRIPTION: Strip characters from a string using regex in one line] 6 | # [SNIPPET_DOCS: None] 7 | # [SNIPPET_AUTHOR: Brent Hoover <brent@hoover.net>] 8 | # [SNIPPET_LICENSE: GPL] 9 | import re 10 | 11 | 12 | def strip_chars_for_url(self, string_to_clean): 13 | return ''.join(re.findall(r'[A-Za-z0-9\-\._]', string_to_clean.replace(' ', '-'))) 14 | 15 | -------------------------------------------------------------------------------- /tarfile/createtar1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # [SNIPPET_NAME: Create a tar file from path] 3 | # [SNIPPET_CATEGORIES: tarfile] 4 | # [SNIPPET_DESCRIPTION: Create a tar file from a path, including compression] 5 | # [SNIPPET_AUTHOR: Tim Voet <tim.voet@gmail.com>] 6 | # [SNIPPET_DOCS: http://docs.python.org/library/tarfile.html#module-tarfile] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | import tarfile 10 | import os 11 | import sys 12 | 13 | user = os.getenv('USERNAME') 14 | 15 | filename = '/home/%s/tmp.tgz' % user 16 | print 'The tar file was created here: %s' % filename 17 | mode = 'w:gz' 18 | 19 | file = tarfile.open( filename, mode ) 20 | 21 | file.add( '/var/log/auth.log' ) 22 | file.add( '/var/log/messages' ) 23 | 24 | file.close() 25 | print 'done' 26 | -------------------------------------------------------------------------------- /tarfile/opentar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # [SNIPPET_NAME: Open a tar file] 3 | # [SNIPPET_CATEGORIES: tarfile] 4 | # [SNIPPET_DESCRIPTION: Open's a tar file and list the entries] 5 | # [SNIPPET_AUTHOR: Tim Voet <tim.voet@gmail.com>] 6 | # [SNIPPET_DOCS: http://docs.python.org/library/tarfile.html#module-tarfile] 7 | # [SNIPPET_LICENSE: GPL] 8 | 9 | import tarfile 10 | import os 11 | import sys 12 | 13 | user = os.getenv('USERNAME') 14 | 15 | 16 | filename = '/home/%s/tmp.tgz' % user 17 | 18 | print 'about to open %s' % filename 19 | mode = 'r:gz' 20 | 21 | if os.path.isfile( filename ) and tarfile.is_tarfile( filename ): 22 | tf = tarfile.open( filename, mode ) 23 | tf.list() 24 | tf.close() 25 | 26 | -------------------------------------------------------------------------------- /templates/script_skeleton.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | def main(): 5 | pass 6 | 7 | if __name__ == ''__main__'': 8 | main() 9 | -------------------------------------------------------------------------------- /templates/sheshebang.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 -------------------------------------------------------------------------------- /test-name-conflicts.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # find snippets whose filename conflicts with module names in the Python 4 | # Standard Library. This is problematic if a snippet wonts to import a 5 | # standard module and wrongly imports a snippet. 6 | 7 | # try to import snippets in python, if an ImportError is raised, 8 | # the filename is OK. 9 | cd $(dirname $0) 10 | files=$(find "." -type f | 11 | sed 's#.*/##' | 12 | sed -n 's/^\([a-zA-Z0-9_]*\)\.py$/try:\n import \1\nexcept ImportError:\n pass\nelse:\n print "\1"/p' | 13 | python) 14 | 15 | [ "$files" ] && { 16 | echo "Files conflicts with module names in the Python Standard Library:" 17 | echo "$files" 18 | exit 1 19 | } 20 | 21 | -------------------------------------------------------------------------------- /type_conversions/hextext_to_integers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | 5 | def hextext_to_integers(htext): 6 | """ 7 | htext is a text representation of binary data in hex format 8 | to make all remaining operations sane, we convert this to a list(array) of ints 9 | """ 10 | junk_list_as_ints = list() 11 | #iterate over htext two characters at a time then convert that 12 | #2 character "chunk" to an integer to build a "junk_list" of integers 13 | #representing the binary data that the clients send 14 | for (op, code) in zip(htext[0::2], htext[1::2]): 15 | x = op + code 16 | y = int(x, 16) 17 | junk_list_as_ints.append(y) 18 | return junk_list_as_ints 19 | 20 | if __name__ == '__main__': 21 | hextext = "2a" 22 | x = hextext_to_integers(hextext) 23 | assert x[0] == 42 -------------------------------------------------------------------------------- /type_conversions/list_flatterner.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # List flattener 3 | 4 | 5 | 6 | def flatten_list(list_to_flatten): 7 | for i, f in enumerate(list_to_flatten): 8 | if hasattr(f, '__iter__'): 9 | del list_to_flatten[i] 10 | list_to_flatten[i:i] = f 11 | return list_to_flatten 12 | 13 | 14 | if __name__ == '__main__': 15 | list_to_flatten = ['one', 'two', 'three', ['a', 'b', 'c'], 'four'] 16 | print(flatten_list(list_to_flatten)) 17 | -------------------------------------------------------------------------------- /type_conversions/list_to_string.py: -------------------------------------------------------------------------------- 1 | """ 2 | Examples of how to turn lists to strings 3 | """ 4 | mylist = ['spam', 'ham', 'eggs'] 5 | print ', '.join(mylist) 6 | print '\n'.join(mylist) 7 | 8 | #However, this simple method does not work if the list contains non-string objects, such as integers. 9 | #If you just want to obtain a comma-separated string, you may use this shortcut: 10 | 11 | list_of_ints = [80, 443, 8080, 8081] 12 | print str(list_of_ints).strip('[]') 13 | 14 | print str(list_of_ints)[1:-1] 15 | 16 | #Finally, you may use map() to convert each item in the list to a string, and then join them: 17 | 18 | print ', '.join(map(str, list_of_ints)) 19 | print '\n'.join(map(str, list_of_ints)) 20 | 21 | -------------------------------------------------------------------------------- /unicode/to_unicode_with_unknown_charset.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import collections 5 | from locale import getpreferredencoding 6 | 7 | 8 | def get_encoding(data): 9 | for cp in (getpreferredencoding(), 'cp1255', 'cp1250'): 10 | try: 11 | _ = unicode(data, cp) 12 | return cp 13 | except UnicodeDecodeError: 14 | pass 15 | 16 | raise Exception('Cannot determine codeset') 17 | 18 | 19 | def to_unicode(data): 20 | """ return a version of data where str objects are converted to unicode """ 21 | 22 | if isinstance(data, unicode): 23 | return data 24 | if isinstance(data, str): 25 | cp = get_encoding(data) 26 | return unicode(data, cp) 27 | elif isinstance(data, collections.Mapping): 28 | return dict(map(to_unicode, data.iteritems())) 29 | elif isinstance(data, collections.Iterable): 30 | return type(data)(map(to_unicode, data)) 31 | else: 32 | return data 33 | 34 | if __name__ == '__main__': 35 | unicoded = to_unicode('Joffre Saint-Thiébaut') 36 | assert(unicoded == u'Joffre Saint-Thiébaut') 37 | print(unicoded) 38 | -------------------------------------------------------------------------------- /unicode/utf8_example.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | 3 | print u' ᓱᓴᓐ ᐊᒡᓗᒃᑲᖅ' 4 | print u' ᓱᓴᓐ ᐊᒡᓗᒃᑲᖅ' 5 | -------------------------------------------------------------------------------- /unittest/nested_calls.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | #! /usr/bin/python 5 | 6 | import unittest 7 | from mock import Mock, MagicMock 8 | from itertools import chain 9 | 10 | 11 | def trigger_handlers(top_level): 12 | ctx = {} 13 | for event in chain([top_level], top_level.extra): 14 | if event.handler: 15 | ctx.update({'event': event}) 16 | event.handler.trigger(ctx) 17 | 18 | 19 | class TestNestedCalls(unittest.TestCase): 20 | 21 | def test_top_level(self): 22 | top_level = MagicMock() 23 | top_level.extra = [] 24 | 25 | trigger_handlers(top_level) 26 | 27 | ctx = {'event': top_level} 28 | top_level.handler.trigger.assert_called_once_with(ctx) 29 | 30 | def test_extra(self): 31 | top_level = MagicMock() 32 | top_level.handler = None 33 | nested = Mock() 34 | top_level.extra = [nested] 35 | 36 | trigger_handlers(top_level) 37 | 38 | ctx = {'event': nested} 39 | nested.handler.trigger.assert_called_once_with(ctx) 40 | 41 | def test_top_level_and_extra(self): 42 | top_level = MagicMock() 43 | nested = Mock() 44 | top_level.extra = [nested] 45 | 46 | trigger_handlers(top_level) 47 | 48 | print 49 | print "Mock: top_level: {0}".format(top_level) 50 | print "Call Args, top_level.handler.trigger: {0}".format(top_level.handler.trigger.call_args_list) 51 | print "Mock: nested: {0}".format(nested) 52 | print "Call Args, nested.handler.trigger: {0}".format(nested.handler.trigger.call_args_list) 53 | print 54 | 55 | ctx = {'event': top_level} 56 | top_level.handler.trigger.assert_called_once_with(ctx) 57 | 58 | ctx = {'event': nested} 59 | nested.handler.trigger.assert_called_once_with(ctx) 60 | 61 | 62 | if __name__ == '__main__': 63 | unittest.main() 64 | -------------------------------------------------------------------------------- /unittest/test_method.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def E(m): 4 | def _c(*a,**k): 5 | try: 6 | return m(*a,**k) 7 | except: 8 | return sys.exc_info()[0] 9 | return _c 10 | 11 | def methodToTest(x): 12 | return 2/x 13 | 14 | if __name__ == "__main__": 15 | assert methodToTest(2)==1 16 | assert methodToTest(1)==2 17 | assert E(methodToTest)(0)==ZeroDivisionError 18 | 19 | # alternative way : 20 | 21 | methodToTest = E(methodToTest) # force decoration 22 | assert methodToTest(1)==2 23 | assert methodToTest(2)==1 24 | assert methodToTest(0)==ZeroDivisionError 25 | -------------------------------------------------------------------------------- /unittest/unittests.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Unit Tests] 4 | # [SNIPPET_CATEGORIES: Testing, unittest] 5 | # [SNIPPET_DESCRIPTION: Example of basic Python unit testing] 6 | # [SNIPPET_DOCS: http://docs.python.org/library/unittest.html] 7 | # [SNIPPET_AUTHOR: David Futcher <bobbo@ubuntu.com>] 8 | # [SNIPPET_LICENSE: MIT] 9 | 10 | import unittest 11 | 12 | FIB_STOP = 10 13 | 14 | # Adapted from "Writing generators" snippet by Josh Holland <jrh@joshh.co.uk> 15 | def fibonacci(start=(0, 1), stop=FIB_STOP): 16 | a, b = start 17 | while stop: 18 | yield a 19 | a, b = b, a + b 20 | stop -= 1 21 | 22 | class FibonacciGeneratorTest(unittest.TestCase): 23 | """ Basic unit test class to check the above Fibonacci generator """ 24 | 25 | def setUp(self): 26 | self.fibs = [] 27 | self.correct = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] 28 | 29 | for x in fibonacci(): 30 | self.fibs.append(x) 31 | 32 | def testStopping(self): 33 | # Check the generator stopped when it should have 34 | self.assertEqual(FIB_STOP, len(self.fibs)) 35 | 36 | def testNumbers(self): 37 | # Check the generated list against our known correct list 38 | for i in range(len(self.correct)): 39 | self.assertEqual(self.fibs[i], self.correct[i]) 40 | 41 | if __name__ == '__main__': 42 | unittest.main() 43 | 44 | -------------------------------------------------------------------------------- /waterworks/FigUtil.py: -------------------------------------------------------------------------------- 1 | """Tools for creating tables and figures in papers.""" 2 | from sets import Set 3 | import sys 4 | 5 | def dict_to_table(d, headers=True, x_header='', reverse=False): 6 | """Convert dict with (x, y) as keys to a 2D table.""" 7 | all_x = Set() 8 | all_y = Set() 9 | for x, y in d.keys(): 10 | all_x.add(x) 11 | all_y.add(y) 12 | all_x = list(all_x) 13 | all_y = list(all_y) 14 | all_x.sort() 15 | all_y.sort() 16 | if reverse: 17 | if 'x' in reverse: 18 | all_x.reverse() 19 | if 'y' in reverse: 20 | all_y.reverse() 21 | 22 | if headers: 23 | table = [[x_header] + all_y] 24 | else: 25 | table = [] 26 | for i, x in enumerate(all_x): 27 | row = [None] * len(all_y) 28 | for j, y in enumerate(all_y): 29 | row[j] = d.get((x, y)) 30 | if headers: 31 | row.insert(0, x) 32 | table.append(row) 33 | return table 34 | 35 | supported_formats = ('csv', 'tsv', 'tex', 'texbitmap', 'asciiart') 36 | def format_table(table, format='csv', outputstream=sys.stdout, **extra_options): 37 | """table can be a table from dict_to_table() or a dictionary. 38 | The dictionary can have either a single value as a key (for a 39 | one-dimensional table) or 2-tuples (for two-dimensional tables). 40 | format is currently one of csv, tsv, tex, texbitmap, or asciiart. 41 | Values for texbitmap should be floats between 0 and 1 and the output 42 | will be the TeX code for a large-pixeled bitmap.""" 43 | if isinstance(table, dict): 44 | table = dict_to_table(table) 45 | 46 | if format in ('csv', 'tsv'): 47 | import csv 48 | dialect = {'csv' : csv.excel, 'tsv' : csv.excel_tab}[format] 49 | writer = csv.writer(outputstream, dialect=dialect) 50 | for row in table: 51 | writer.writerow(row) 52 | elif format == 'tex': 53 | import TeXTable 54 | print >>outputstream, TeXTable.texify(table, has_header=True) 55 | elif format == 'texbitmap': 56 | import TeXTable 57 | extra_options.setdefault('has_header', True) 58 | print >>outputstream, TeXTable.make_tex_bitmap(table, **extra_options) 59 | elif format == 'asciiart': 60 | from texttable import Texttable 61 | texttable = Texttable(**extra_options) 62 | texttable.add_rows(table) 63 | print >>outputstream, texttable.draw() 64 | else: 65 | raise ValueError("Unsupported format: %r (supported formats: %s)" % \ 66 | (format, ' '.join(supported_formats))) 67 | 68 | if __name__ == "__main__": 69 | print dict_to_table({('add-0', '01') : 7, 70 | ('add-0', '22') : 8, 71 | ('add-0', '24') : 9, 72 | ('add-1', '01') : 10, 73 | ('add-1', '22') : 11, 74 | ('add-1', '24') : 12,}) 75 | format_table(dict_to_table({('add-0', '01') : 7, 76 | ('add-0', '22') : 8, 77 | ('add-0', '24') : 9, 78 | ('add-1', '01') : 10, 79 | ('add-1', '22') : 11, 80 | ('add-1', '24') : 12,}), format='tsv') 81 | -------------------------------------------------------------------------------- /waterworks/FunctionPickler.py: -------------------------------------------------------------------------------- 1 | """Allows you to create pickle-able references to functions. Of course, 2 | this is with the understanding that the underlying implementation of 3 | that function can change, so don't use this if you require future-proof 4 | results.""" 5 | from pickle import PicklingError 6 | 7 | class PickledFunction: 8 | def __init__(self, function): 9 | if not callable(function): 10 | raise ValueError("Not callable: %r" % function) 11 | 12 | self.name = function.__name__ 13 | if self.name == '<lambda>': 14 | raise ValueError("Function cannot be a lambda.") 15 | 16 | self.modulename = function.__module__ 17 | if self.modulename == "__main__": 18 | import inspect, path 19 | modpath = path.path(inspect.getsourcefile(function)) 20 | self.modulename = str(modpath.stripext().basename()) 21 | 22 | try: # make sure we can find the function later 23 | self._import() 24 | except (KeyboardInterrupt, SystemExit): 25 | raise 26 | except: 27 | raise PicklingError("Can't pickle function %r" % function) 28 | def __setstate__(self, state): 29 | self.__dict__.update(state) 30 | self._import() 31 | def _import(self): 32 | mod = __import__(self.modulename) 33 | self._func = getattr(mod, self.name) 34 | def __getstate__(self): 35 | state = dict(self.__dict__) 36 | state.pop('_func', None) 37 | return state 38 | 39 | def __getattr__(self, attr): 40 | """All attributes (__call__ especially) are passed onto the 41 | function.""" 42 | return getattr(self._func, attr) 43 | 44 | if __name__ == "__main__": 45 | import time 46 | import pickle 47 | 48 | func = time.time 49 | def func2(): 50 | "Not pickle-able" 51 | return 'zarg2' 52 | 53 | p = PickledFunction(func) 54 | print 'p', p 55 | pickled = pickle.dumps(p) 56 | unpickled = pickle.loads(pickled) 57 | print 'unpickled', unpickled 58 | print 'doc', unpickled.__doc__ 59 | print unpickled() 60 | print 'done' 61 | -------------------------------------------------------------------------------- /waterworks/HeapQueue.py: -------------------------------------------------------------------------------- 1 | """A more object-oriented wrapper of the heapq module.""" 2 | import heapq 3 | 4 | class HeapQueue: 5 | """Object-oriented way of maintaining heap queue with the heapq module.""" 6 | def __init__(self, initial_list=None): 7 | """Create a heap queue, optionally from an initial list.""" 8 | self._heap = initial_list or [] 9 | heapq.heapify(self._heap) 10 | def __len__(self): 11 | return len(self._heap) 12 | def __iter__(self): 13 | """You can only get the contents as an iterable so that they 14 | will be read only.""" 15 | return iter(self._heap) 16 | def __contains__(self, item): 17 | return item in self._heap 18 | def peek(self): 19 | """Look at the top element.""" 20 | return self._heap[0] 21 | def pop(self): 22 | """Removes and returns the top element.""" 23 | return heapq.heappop(self._heap) 24 | def add(self, item): 25 | """Add an item into the heap.""" 26 | return heapq.heappush(self._heap, item) 27 | -------------------------------------------------------------------------------- /waterworks/LazyList.py: -------------------------------------------------------------------------------- 1 | """Lets you treat an interator as a list by filling in the list on demand.""" 2 | 3 | class LazyList: 4 | def __init__(self, iterator, use_partial_list=False): 5 | self.list_so_far = [] 6 | self.iterator = iterator 7 | self.iterator_exhausted = False 8 | self.use_partial_list = use_partial_list 9 | def __getitem__(self, index): 10 | # TODO doesn't handle slices currently -- reads entire list whenever 11 | # you ask for these! 12 | if not self.iterator_exhausted: 13 | if index < 0: 14 | self._read_all() 15 | if index >= len(self.list_so_far): 16 | self._read_many(index - len(self.list_so_far) + 1) 17 | 18 | return self.list_so_far[index] 19 | def _read_many(self, count): 20 | for x in range(count): 21 | self._read_iterator() 22 | if self.iterator_exhausted: 23 | break 24 | def _read_all(self): 25 | while not self.iterator_exhausted: 26 | self._read_iterator() 27 | def _read_iterator(self): 28 | try: 29 | self.list_so_far.append(self.iterator.next()) 30 | except StopIteration: 31 | self.iterator_exhausted = True 32 | 33 | def __getattr__(self, attr): 34 | if not self.use_partial_list: 35 | self._read_all() 36 | return getattr(self.list_so_far, attr) 37 | 38 | if __name__ == "__main__": 39 | def g(): 40 | for x in range(10): 41 | print 'yield', x 42 | yield x 43 | 44 | l = LazyList(g()) 45 | print l[4] 46 | print l[0] 47 | print l[2:5] 48 | print l[-1] 49 | print len(l) 50 | print repr(l) 51 | print len(l) 52 | -------------------------------------------------------------------------------- /waterworks/MANIFEST: -------------------------------------------------------------------------------- 1 | README 2 | README.html 3 | ChangeLog 4 | AIMA.py 5 | ExitCodes.py 6 | FigUtil.py 7 | Histogram.py 8 | IntRange.py 9 | IntShelve.py 10 | LazyList.py 11 | Selectron.py 12 | Tailer.py 13 | TeXTable.py 14 | ThreadedJobs.py 15 | TkGeomSavers.py 16 | Utility.py 17 | diffprint.py 18 | iterextras.py 19 | setup.py 20 | cookbook/Memoize.py 21 | cookbook/__init__.py 22 | cookbook/backwardsreader.py 23 | cookbook/baseconverter.py 24 | cookbook/cartesianproduct.py 25 | cookbook/frange.py 26 | cookbook/pcolor.py 27 | cookbook/print_exc_plus.py 28 | cookbook/progress.py 29 | waterworks/Dictionaries.py 30 | waterworks/Files.py 31 | waterworks/Processes.py 32 | waterworks/Sequences.py 33 | waterworks/Streams.py 34 | waterworks/Strings.py 35 | waterworks/Tools.py 36 | waterworks/__init__.py 37 | -------------------------------------------------------------------------------- /waterworks/PrecRec.py: -------------------------------------------------------------------------------- 1 | """PrecRec: standard calculation of precision, recall and f-score.""" 2 | from __future__ import division 3 | 4 | def precision_recall(n_matched, n_gold, n_proposed): 5 | """Calculates the classification precision and recall, given 6 | the number of true positives, the number of existing positives, 7 | and the number of proposed positives.""" 8 | 9 | if n_gold > 0: 10 | recall = n_matched / n_gold 11 | else: 12 | recall = 0.0 13 | 14 | if n_proposed > 0: 15 | precision = n_matched / n_proposed 16 | else: 17 | precision = 0.0 18 | 19 | return precision, recall 20 | 21 | def fscore(precision, recall, beta=1.0): 22 | """Calculates the f-score (default is balanced f-score; beta > 1 23 | favors precision), the harmonic mean of precision and recall.""" 24 | 25 | num = (beta ** 2 + 1) * precision * recall 26 | denom = (beta ** 2) * precision + recall 27 | if denom == 0: 28 | return 0.0 29 | else: 30 | return num / denom 31 | 32 | def precision_recall_f(n_matched, n_gold, n_proposed, beta=1.0): 33 | """Calculates precision, recall and f-score.""" 34 | 35 | prec, rec = precision_recall(n_matched, n_gold, n_proposed) 36 | f = fscore(prec, rec, beta=beta) 37 | 38 | return prec, rec, f 39 | 40 | def fscore_from_components(n_matched, n_gold, n_proposed, beta=1.0): 41 | """Calculates f-score from the number of matched, gold, and proposed 42 | items instead of precision and recall. See fscore().""" 43 | return precision_recall_f(n_matched, n_gold, n_proposed, beta=beta)[2] 44 | 45 | if __name__ == "__main__": 46 | print precision_recall(1, 10, 1) 47 | print precision_recall(10, 10, 20) 48 | print precision_recall(10, 10, 10) 49 | 50 | print fscore(1, 1) 51 | print fscore(0, 0) 52 | print fscore(.5, .5) 53 | print fscore(.5, .7) 54 | print fscore(.7, .5) 55 | -------------------------------------------------------------------------------- /waterworks/QuickColorize.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """QuickColorize: provides a quick way to randomly colorize text. 4 | By calling colorize() on text, it will randomly assign a color for 5 | that text (which will be reused on subsequent calls to colorize(). 6 | Also includes a command-line mode.""" 7 | 8 | # TODO use bg colors 9 | import ansi, itertools 10 | all_colors = [name for name in dir(ansi) if name[0].isupper()] 11 | fg_colors = sorted([name for name in all_colors if not name.endswith('BG')]) 12 | bg_colors = sorted([name for name in all_colors if name.endswith('BG')]) 13 | 14 | for name in 'BLACK BOLD REVERSE RESET'.split(): 15 | fg_colors.remove(name) 16 | 17 | # put black first 18 | bg_colors.remove('BLACKBG') 19 | bg_colors.insert(0, 'BLACKBG') 20 | 21 | color_pairs = [] 22 | for bg_color in bg_colors: 23 | for fg_color in fg_colors: 24 | if bg_color.startswith(fg_color): 25 | continue 26 | if bg_color == 'BLACKBG' and fg_color == 'WHITE': 27 | continue 28 | 29 | color_pairs.append((fg_color, bg_color)) 30 | 31 | color_cycler = itertools.cycle(color_pairs) 32 | colors_defined = {} 33 | def colorize(text): 34 | """Returns a colorized version of text. The same text values will 35 | be consistently colorized.""" 36 | color = colors_defined.get(text) 37 | if not color: 38 | names = color_cycler.next() 39 | colors = [getattr(ansi, name) for name in names] 40 | colors_defined[text] = color 41 | return ''.join(colors) + text + ansi.RESET 42 | 43 | if __name__ == "__main__": 44 | # a quick command-line program for colorizing stdin or a list of files 45 | 46 | # Example: 47 | # % quickcolorize term1 term2 term3 < somefilename 48 | # Use -- to separate terms to highlight from filenames: 49 | # % quickcolorize term1 term2 term3 -- file1 file2 file3 50 | import sys 51 | 52 | filenames = [] 53 | stdin_mode = True 54 | 55 | mapping = {} 56 | for arg in sys.argv[1:]: 57 | if arg == '--': 58 | stdin_mode = False 59 | continue 60 | if stdin_mode: 61 | mapping[arg] = colorize(arg) 62 | else: 63 | filenames.append(arg) 64 | 65 | def file_iter(): 66 | if stdin_mode: 67 | yield sys.stdin 68 | else: 69 | for filename in filenames: 70 | f = file(filename) 71 | yield f 72 | del f 73 | 74 | for input_file in file_iter(): 75 | for line in input_file: 76 | for old, new in mapping.items(): 77 | line = line.replace(old, new) 78 | try: 79 | sys.stdout.write(line) 80 | except IOError: 81 | raise SystemExit 82 | -------------------------------------------------------------------------------- /waterworks/TeXTable.py: -------------------------------------------------------------------------------- 1 | """Convert a Python table into a LaTeX/TeX table.""" 2 | __all__ = ['texify', 'make_tex_bitmap'] 3 | 4 | def texify(table, compact=1, has_header=False, hlines=True, vlines=True): 5 | """compact is a value from 0 to 2 which controls how much whitespace 6 | we output. It does not change the display of the table.""" 7 | s = [] 8 | xdim = len(table[0]) 9 | 10 | if compact == 0: 11 | compact1 = compact2 = '\n' 12 | elif compact == 1: 13 | compact1 = '\n' 14 | compact2 = '' 15 | else: 16 | compact1 = ' ' 17 | compact2 = '' 18 | 19 | if hlines: 20 | hline_text = r'\hline' 21 | else: 22 | hline_text = '' 23 | if vlines: 24 | separator = '|' 25 | else: 26 | separator = '' 27 | 28 | s.append(r"\begin{tabular}{" + 'c'.join(([separator] * (xdim + 1))) + "}\n") 29 | if hlines: 30 | s.append(hline_text + "\n") 31 | for count, row in enumerate(table): 32 | s.append(' & '.join([str(x) for x in row]) + compact1) 33 | if has_header and count == 0: 34 | s.append(r"\\ %s%s%s%s" % (hline_text, hline_text, '\n', compact2)) 35 | else: 36 | s.append(r"\\ " + hline_text + "\n" + compact2) 37 | s.append(r"\end{tabular}") 38 | 39 | return ''.join(s) 40 | 41 | def greyify_cell(cell, white_is_1=False, value_formatter=None): 42 | if isinstance(cell, (float, int)) and 0 <= cell <= 1: 43 | if not white_is_1: 44 | cell = 1 - cell 45 | result = r'\cellcolor[gray]{%0.3f}' % cell 46 | if value_formatter: 47 | result += ' ' + str(value_formatter(cell)) 48 | return result 49 | else: 50 | return cell 51 | 52 | # XXX TODO more docs 53 | def make_tex_bitmap(table, has_header=False, white_is_1=True, 54 | value_formatter=None): 55 | """All floats will be converted to their grey values. You will need 56 | to include the LaTeX package colortbl: 57 | 58 | \usepackage{colortbl} 59 | 60 | has_header is passed to texify.""" 61 | rows = [[greyify_cell(cell, white_is_1=white_is_1, 62 | value_formatter=value_formatter) 63 | for cell in row] 64 | for row in table] 65 | return texify(rows, has_header=has_header) 66 | 67 | if __name__ == "__main__": 68 | print texify([[1, 2, 3], 69 | [4, 5, 6], 70 | [7, 8, 9]]) 71 | print 72 | print make_tex_bitmap([[0.1, 0.2, 0.3]]) 73 | -------------------------------------------------------------------------------- /waterworks/TerminalTitle.py: -------------------------------------------------------------------------------- 1 | """Lets you change the title of your terminal in *NIX.""" 2 | import sys 3 | 4 | template = "\033]0;%s\007" 5 | 6 | def change_title(s): 7 | """Change the title of your terminal.""" 8 | sys.stdout.write(template % s) 9 | -------------------------------------------------------------------------------- /waterworks/ThreadedJobs.py: -------------------------------------------------------------------------------- 1 | """Wraps twisted.python.threadpool in a way that makes more sense to me.""" 2 | import time, random 3 | from twisted.python import threadpool, threadable 4 | 5 | class ThreadedJobs: 6 | def __init__(self, func=None, arglistlist=None, workers=5, debug=True): 7 | """arglistlist is a list of a list of args""" 8 | self.jobsfinished = 0 9 | self.jobstodo = 0 10 | self.debug = debug 11 | self.tp = threadpool.ThreadPool(workers, workers) 12 | for arglist in arglistlist or []: 13 | self.addjob(func, *arglist) 14 | 15 | def start(self): 16 | self.tp.start() 17 | while self.jobsfinished < self.getjobstodo(): 18 | time.sleep(0.1) 19 | self.tp.stop() 20 | 21 | def _run(self, func, *args): 22 | if self.debug: 23 | print "Starting", args 24 | func(*args) 25 | self.finished(args) 26 | 27 | def finished(self, args): 28 | if self.debug: 29 | print "Finished with", args 30 | self.jobsfinished += 1 31 | 32 | def getjobstodo(self): 33 | return self.jobstodo 34 | 35 | def addjob(self, func, *arglist): 36 | self.jobstodo += 1 37 | self.tp.callInThread(self._run, func, *arglist) 38 | 39 | synchronized = ["finished", "getjobstodo", "addjob"] 40 | 41 | threadable.synchronize(ThreadedJobs) 42 | 43 | if __name__ == "__main__": 44 | def sleeper(amount, bogusarg): 45 | print "sleeping for", amount 46 | time.sleep(amount) 47 | print "done sleeping for", amount 48 | 49 | times = [(t, 2) for t in range(20)] 50 | random.shuffle(times) 51 | 52 | tj = ThreadedJobs(sleeper, times) 53 | tj.start() 54 | -------------------------------------------------------------------------------- /waterworks/TkGeomSavers.py: -------------------------------------------------------------------------------- 1 | """Common geometry behaviors for Tk/Tix""" 2 | import atexit, os, Tix as Tk 3 | 4 | class GeomSaverMixin: 5 | """Mixin for widgets that save/load their geometry.""" 6 | def __init__(self, title): 7 | self.title(title) 8 | atexit.register(self.save_geom) 9 | self.filename = '.tkgeom-%s' % title 10 | self.load_geom() 11 | 12 | self.bind("<Destroy>", self.save_geom) 13 | def get_full_filename(self): 14 | return os.path.join(os.environ['HOME'], self.filename) 15 | def load_geom(self, *args): 16 | try: 17 | f = file(self.get_full_filename(), 'r') 18 | geom = f.read() 19 | self.wm_geometry(geom) 20 | except: 21 | pass 22 | def save_geom(self, *args): 23 | try: 24 | geom = self.wm_geometry() 25 | f = file(self.get_full_filename(), 'w') 26 | f.write(geom) 27 | except Tk.TclError: # since we call this twice, we expect an error 28 | # the second time 29 | pass 30 | 31 | class ToplevelGeomSaver(Tk.Toplevel, GeomSaverMixin): 32 | """Just like a Toplevel, but title is necessary""" 33 | def __init__(self, master, title, *args, **kw): 34 | Tk.Toplevel.__init__(self, master, *args, **kw) 35 | GeomSaverMixin.__init__(self, title) 36 | 37 | class TkRootGeomSaver(Tk.Tk, GeomSaverMixin): 38 | """Just like a Tk (root object), but title is necessary""" 39 | def __init__(self, title, *args, **kw): 40 | Tk.Tk.__init__(self, *args, **kw) 41 | GeomSaverMixin.__init__(self, title) 42 | 43 | if __name__ == "__main__": 44 | root = TkRootGeomSaver('mr-root') 45 | t = ToplevelGeomSaver(root, 'hello') 46 | Tk.mainloop() 47 | -------------------------------------------------------------------------------- /waterworks/ansi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | ''' 3 | ansi.py 4 | 5 | ANSI Terminal Interface 6 | 7 | (C)opyright 2000 Jason Petrone <jp_py@demonseed.net> 8 | All Rights Reserved 9 | 10 | Color Usage: 11 | print RED + 'this is red' + RESET 12 | print BOLD + GREEN + WHITEBG + 'this is bold green on white' + RESET 13 | 14 | Commands: 15 | def move(new_x, new_y): 'Move cursor to new_x, new_y' 16 | def moveUp(lines): 'Move cursor up # of lines' 17 | def moveDown(lines): 'Move cursor down # of lines' 18 | def moveForward(chars): 'Move cursor forward # of chars' 19 | def moveBack(chars): 'Move cursor backward # of chars' 20 | def save(): 'Saves cursor position' 21 | def restore(): 'Restores cursor position' 22 | def clear(): 'Clears screen and homes cursor' 23 | def clrtoeol(): 'Clears screen to end of line' 24 | ''' 25 | 26 | ################################ 27 | # C O L O R C O N S T A N T S # 28 | ################################ 29 | BLACK = '\033[30m' 30 | RED = '\033[31m' 31 | GREEN = '\033[32m' 32 | YELLOW = '\033[33m' 33 | BLUE = '\033[34m' 34 | MAGENTA = '\033[35m' 35 | CYAN = '\033[36m' 36 | WHITE = '\033[37m' 37 | 38 | RESET = '\033[0;0m' 39 | BOLD = '\033[1m' 40 | REVERSE = '\033[2m' 41 | 42 | BLACKBG = '\033[40m' 43 | REDBG = '\033[41m' 44 | GREENBG = '\033[42m' 45 | YELLOWBG = '\033[43m' 46 | BLUEBG = '\033[44m' 47 | MAGENTABG = '\033[45m' 48 | CYANBG = '\033[46m' 49 | WHITEBG = '\033[47m' 50 | 51 | def move(new_x, new_y): 52 | 'Move cursor to new_x, new_y' 53 | print '\033[' + str(new_x) + ';' + str(new_y) + 'H' 54 | 55 | def moveUp(lines): 56 | 'Move cursor up # of lines' 57 | print '\033[' + str(lines) + 'A' 58 | 59 | def moveDown(lines): 60 | 'Move cursor down # of lines' 61 | print '\033[' + str(lines) + 'B' 62 | 63 | def moveForward(chars): 64 | 'Move cursor forward # of chars' 65 | print '\033[' + str(lines) + 'C' 66 | 67 | def moveBack(chars): 68 | 'Move cursor backward # of chars' 69 | print '\033[' + str(lines) + 'D' 70 | 71 | def save(): 72 | 'Saves cursor position' 73 | print '\033[s' 74 | 75 | def restore(): 76 | 'Restores cursor position' 77 | print '\033[u' 78 | 79 | def clear(): 80 | 'Clears screen and homes cursor' 81 | print '\033[2J' 82 | 83 | def clrtoeol(): 84 | 'Clears screen to end of line' 85 | print '\033[K' 86 | -------------------------------------------------------------------------------- /waterworks/cookbook/Memoize.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52201 3 | Title: Memoizing (cacheing) function return values 4 | Submitter: Paul Moore 5 | Last Updated: 2001/10/16 6 | Version no: 1.1 7 | 8 | Description: 9 | For functions which are called often, particulary recursive functions 10 | or functions which are intensive to calculate, memoizing (cacheing) 11 | the return values can dramatically improve performance. 12 | """ 13 | 14 | class Memoize: 15 | """Memoize(fn) - an instance which acts like fn but memoizes its arguments 16 | Will only work on functions with non-mutable arguments 17 | """ 18 | def __init__(self, fn): 19 | self.fn = fn 20 | self.memo = {} 21 | def __call__(self, *args): 22 | if not self.memo.has_key(args): 23 | self.memo[args] = self.fn(*args) 24 | return self.memo[args] 25 | def clear(self): 26 | """Forget all memoized results.""" 27 | self.memo.clear() 28 | 29 | # this came from a different recipe 30 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/325205 31 | # TODO: incorporate more of their code (ImmutableDict, e.g.) 32 | import types 33 | def cachedmethod(function): 34 | return types.MethodType(Memoize(function), None) 35 | -------------------------------------------------------------------------------- /waterworks/cookbook/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/waterworks/cookbook/__init__.py -------------------------------------------------------------------------------- /waterworks/cookbook/backwardsreader.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*-mode: python; coding: iso-8859-1 -*- 3 | # 4 | # Copyright (c) Peter Astrand <astrand@cendio.se> 5 | 6 | """ 7 | URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439045 8 | Title: Read a text file backwards (yet another implementation) 9 | Submitter: Peter Astrand 10 | Last Updated: 2005/08/11 11 | Version no: 1.0 12 | Category: Files 13 | 14 | Description: 15 | Yet another way to read a file line by line, starting at the end. 16 | 17 | Modified by dmcc: put __init__ at top, testing code in __main__ and 18 | add __iter__ method. 19 | """ 20 | 21 | import os 22 | import string 23 | 24 | class BackwardsReader: 25 | """Read a file line by line, backwards""" 26 | BLKSIZE = 4096 27 | def __init__(self, file): 28 | self.file = file 29 | self.buf = "" 30 | self.file.seek(0, 2) 31 | def readline(self): 32 | while 1: 33 | newline_pos = string.rfind(self.buf, "\n") 34 | if newline_pos != -1: 35 | # Found a newline 36 | line = self.buf[newline_pos+1:] 37 | self.buf = self.buf[:newline_pos] 38 | return line + "\n" 39 | else: 40 | pos = self.file.tell() 41 | if pos == 0: 42 | # Start-of-file 43 | return "" 44 | else: 45 | # Need to fill buffer 46 | backseek = min(self.BLKSIZE, pos) 47 | self.file.seek(-backseek, 1) 48 | self.buf = self.file.read(self.BLKSIZE) + self.buf 49 | self.file.seek(-backseek, 1) 50 | if pos - backseek == 0: 51 | self.buf = "\n" + self.buf 52 | def __iter__(self): 53 | while 1: 54 | line = self.readline() 55 | if line is "": 56 | break 57 | else: 58 | yield line 59 | 60 | if __name__ == "__main__": 61 | # Example usage 62 | br = BackwardsReader(open('bar')) 63 | 64 | while 1: 65 | line = br.readline() 66 | if not line: 67 | break 68 | print repr(line) 69 | -------------------------------------------------------------------------------- /waterworks/cookbook/baseconverter.py: -------------------------------------------------------------------------------- 1 | """ 2 | Title: Numeric base converter that accepts arbitrary digits 3 | Submitter: Drew Perttula 4 | Last Updated: 2002/01/31 5 | Version no: 1.0 6 | Category: Algorithms 7 | 8 | Description: 9 | 10 | This is a traditional base converter with the twist that it 11 | accepts any strings as the digits for the input and output bases. 12 | 13 | Besides all the normal base-converts, you can now create compact 14 | versions of huge numbers by converting them to a base that uses 15 | all the letters and numbers for its digits, for example. 16 | 17 | URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/111286 18 | """ 19 | 20 | BASE2 = "01" 21 | BASE10 = "0123456789" 22 | BASE16 = "0123456789ABCDEF" 23 | BASE62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz" 24 | 25 | def baseconvert(number,fromdigits,todigits): 26 | """ converts a "number" between two bases of arbitrary digits 27 | 28 | The input number is assumed to be a string of digits from the 29 | fromdigits string (which is in order of smallest to largest 30 | digit). The return value is a string of elements from todigits 31 | (ordered in the same way). The input and output bases are 32 | determined from the lengths of the digit strings. Negative 33 | signs are passed through. 34 | 35 | decimal to binary 36 | >>> baseconvert(555,BASE10,BASE2) 37 | '1000101011' 38 | 39 | binary to decimal 40 | >>> baseconvert('1000101011',BASE2,BASE10) 41 | '555' 42 | 43 | integer interpreted as binary and converted to decimal (!) 44 | >>> baseconvert(1000101011,BASE2,BASE10) 45 | '555' 46 | 47 | base10 to base4 48 | >>> baseconvert(99,BASE10,"0123") 49 | '1203' 50 | 51 | base4 to base5 (with alphabetic digits) 52 | >>> baseconvert(1203,"0123","abcde") 53 | 'dee' 54 | 55 | base5, alpha digits back to base 10 56 | >>> baseconvert('dee',"abcde",BASE10) 57 | '99' 58 | 59 | decimal to a base that uses A-Z0-9a-z for its digits 60 | >>> baseconvert(257938572394L,BASE10,BASE62) 61 | 'E78Lxik' 62 | 63 | ..convert back 64 | >>> baseconvert('E78Lxik',BASE62,BASE10) 65 | '257938572394' 66 | 67 | binary to a base with words for digits (the function cannot convert this back) 68 | >>> baseconvert('1101',BASE2,('Zero','One')) 69 | 'OneOneZeroOne' 70 | 71 | """ 72 | 73 | if str(number)[0]=='-': 74 | number = str(number)[1:] 75 | neg=1 76 | else: 77 | neg=0 78 | 79 | # make an integer out of the number 80 | x=long(0) 81 | for digit in str(number): 82 | x = x*len(fromdigits) + fromdigits.index(digit) 83 | 84 | # create the result in base 'len(todigits)' 85 | res="" 86 | while x>0: 87 | digit = x % len(todigits) 88 | res = todigits[digit] + res 89 | x /= len(todigits) 90 | if neg: 91 | res = "-"+res 92 | 93 | return res 94 | -------------------------------------------------------------------------------- /waterworks/cookbook/defaultdict.py: -------------------------------------------------------------------------------- 1 | """ 2 | Title: emulate collections.defaultdict 3 | URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523034 4 | Submitter: Jason Kirtland 5 | Last Updated: 2007/07/09 6 | Version no: 1.0 7 | Category: Shortcuts 8 | 9 | Description: 10 | 11 | A pure-Python version of Python 2.5's defaultdict 12 | """ 13 | 14 | try: 15 | from collections import defaultdict 16 | except: 17 | class defaultdict(dict): 18 | def __init__(self, default_factory=None, *a, **kw): 19 | if (default_factory is not None and 20 | not hasattr(default_factory, '__call__')): 21 | raise TypeError('first argument must be callable') 22 | dict.__init__(self, *a, **kw) 23 | self.default_factory = default_factory 24 | def __getitem__(self, key): 25 | try: 26 | return dict.__getitem__(self, key) 27 | except KeyError: 28 | return self.__missing__(key) 29 | def __missing__(self, key): 30 | if self.default_factory is None: 31 | raise KeyError(key) 32 | self[key] = value = self.default_factory() 33 | return value 34 | def __reduce__(self): 35 | if self.default_factory is None: 36 | args = tuple() 37 | else: 38 | args = self.default_factory, 39 | return type(self), args, None, None, self.items() 40 | def copy(self): 41 | return self.__copy__() 42 | def __copy__(self): 43 | return type(self)(self.default_factory, self) 44 | def __deepcopy__(self, memo): 45 | import copy 46 | return type(self)(self.default_factory, 47 | copy.deepcopy(self.items())) 48 | def __repr__(self): 49 | return 'defaultdict(%s, %s)' % (self.default_factory, 50 | dict.__repr__(self)) 51 | -------------------------------------------------------------------------------- /waterworks/cookbook/frange.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66472 3 | Title: frange(), a range function with float increments 4 | Submitter: Dinu Gherman (other recipes) 5 | Last Updated: 2001/08/07 6 | Version no: 1.0 7 | Category: Shortcuts 8 | 9 | Description: 10 | 11 | Sadly missing in the Python standard library, this function allows to use 12 | ranges, just as the built-in function range(), but with float arguments. 13 | 14 | All thoretic restrictions apply, but in practice this is more useful 15 | than in theory.""" 16 | 17 | def frange(start, end=None, inc=None): 18 | "A range function, that does accept float increments..." 19 | 20 | if end == None: 21 | end = start + 0.0 22 | start = 0.0 23 | 24 | if inc == None: 25 | inc = 1.0 26 | 27 | L = [] 28 | while 1: 29 | next = start + len(L) * inc 30 | if inc > 0 and next >= end: 31 | break 32 | elif inc < 0 and next <= end: 33 | break 34 | L.append(next) 35 | 36 | return L 37 | 38 | def xfrange(start, end=None, inc=None): 39 | """A range function, that does accept float increments and is a 40 | generator (i.e. lazy)""" 41 | 42 | if end == None: 43 | end = start + 0.0 44 | start = 0.0 45 | 46 | if inc == None: 47 | inc = 1.0 48 | 49 | count = 0 50 | while 1: 51 | next = start + count * inc 52 | if inc > 0 and next >= end: 53 | break 54 | elif inc < 0 and next <= end: 55 | break 56 | 57 | yield next 58 | count += 1 59 | -------------------------------------------------------------------------------- /waterworks/cookbook/hungarian_method.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/waterworks/cookbook/hungarian_method.py -------------------------------------------------------------------------------- /waterworks/cookbook/pdbhelper.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65287 3 | Title: Automatically start the debugger on an exception 4 | Submitter: Thomas Heller 5 | Last Updated: 2001/07/13 6 | Version no: 1.4 7 | Category: Debugging 8 | 9 | Description: 10 | 11 | When Python runs a script and an uncatched exception is raised, 12 | a traceback is printed and the script is terminated. Python2.1 has 13 | introduced sys.excepthook, which can be used to override the handling of 14 | uncaught exceptions. This allows to automatically start the debugger on an 15 | unexpected exception, even if python is not running in interactive mode. 16 | 17 | Discussion: 18 | 19 | The above code should be included in 'sitecustomize.py', which is 20 | automatically imported by python. The debugger is only started when 21 | python is run in non-interactive mode. 22 | 23 | Note by dmcc: To install pdb as your exception hook, just import this module. 24 | It will save the original except hook, which you can restore with 25 | restore_pdb_excepthook(). You can reenable the pdb hook with 26 | install_pdb_excepthook()""" 27 | 28 | import sys 29 | 30 | def pdbexcepthook(type, value, tb): 31 | if hasattr(sys, 'ps1') or not sys.stderr.isatty(): 32 | # we are in interactive mode or we don't have a tty-like 33 | # device, so we call the default hook 34 | sys.__excepthook__(type, value, tb) 35 | else: 36 | import traceback, pdb 37 | # we are NOT in interactive mode, print the exception... 38 | traceback.print_exception(type, value, tb) 39 | print 40 | # ...then start the debugger in post-mortem mode. 41 | pdb.pm() 42 | 43 | def install_pdb_excepthook(): 44 | global _original_excepthook 45 | _original_excepthook = sys.excepthook 46 | sys.excepthook = pdbexcepthook 47 | def restore_pdb_excepthook(): 48 | global _original_excepthook 49 | sys.excepthook = _original_excepthook 50 | 51 | install_pdb_excepthook() 52 | -------------------------------------------------------------------------------- /waterworks/cookbook/print_exc_plus.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215 3 | Title: Get more information from tracebacks 4 | Submitter: Bryn Keller (other recipes) 5 | Last Updated: 2001/03/07 6 | Version no: 1.0 7 | 8 | Description: 9 | 10 | The standard Python traceback module provides very useful 11 | functions to produce useful information about where and why an error 12 | occurred. Traceback objects actually contain a great deal more information 13 | than the traceback module displays, however. That information can greatly 14 | assist in detecting the cause of your error. 15 | 16 | Here's an example of an extended traceback printer you might use, 17 | followed by a usage example.""" 18 | 19 | import sys, traceback 20 | 21 | def print_exc_plus(): 22 | """ 23 | Print the usual traceback information, followed by a listing of all the 24 | local variables in each frame. 25 | """ 26 | tb = sys.exc_info()[2] 27 | while 1: 28 | if not tb.tb_next: 29 | break 30 | tb = tb.tb_next 31 | stack = [] 32 | f = tb.tb_frame 33 | while f: 34 | stack.append(f) 35 | f = f.f_back 36 | stack.reverse() 37 | traceback.print_exc() 38 | print "Locals by frame, innermost last" 39 | for frame in stack: 40 | print 41 | print "Frame %s in %s at line %s" % (frame.f_code.co_name, 42 | frame.f_code.co_filename, 43 | frame.f_lineno) 44 | for key, value in frame.f_locals.items(): 45 | print "\t%20s = " % key, 46 | #We have to be careful not to cause a new error in our error 47 | #printer! Calling str() on an unknown object could cause an 48 | #error we don't want. 49 | try: 50 | print value 51 | except: 52 | print "<ERROR WHILE PRINTING VALUE>" 53 | 54 | 55 | if __name__ == '__main__': 56 | #A simplistic demonstration of the kind of problem this approach can help 57 | #with. Basically, we have a simple function which manipulates all the 58 | #strings in a list. The function doesn't do any error checking, so when 59 | #we pass a list which contains something other than strings, we get an 60 | #error. Figuring out what bad data caused the error is easier with our 61 | #new function. 62 | 63 | data = ["1", "2", 3, "4"] #Typo: We 'forget' the quotes on data[2] 64 | def pad4(seq): 65 | """ 66 | Pad each string in seq with zeros, to four places. Note there 67 | is no reason to actually write this function, Python already 68 | does this sort of thing much better. 69 | Just an example. 70 | """ 71 | return_value = [] 72 | for thing in seq: 73 | return_value.append("0" * (4 - len(thing)) + thing) 74 | return return_value 75 | 76 | #First, show the information we get from a normal traceback.print_exc(). 77 | try: 78 | pad4(data) 79 | except: 80 | traceback.print_exc() 81 | print 82 | print "----------------" 83 | print 84 | 85 | #Now with our new function. Note how easy it is to see the bad data that 86 | #caused the problem. The variable 'thing' has the value 3, so we know 87 | #that the TypeError we got was because of that. A quick look at the 88 | #value for 'data' shows us we simply forgot the quotes on that item. 89 | try: 90 | pad4(data) 91 | except: 92 | print_exc_plus() 93 | -------------------------------------------------------------------------------- /waterworks/cookbook/pushback_wrapper.py: -------------------------------------------------------------------------------- 1 | """ 2 | Title: iterator wrapper, allowing 'pushback' and 'nonzero' test 3 | Submitter: Manuel Garcia (other recipes) 4 | Last Updated: 2007/03/12 5 | Version no: 1.0 6 | Category: Algorithms 7 | 8 | Description: 9 | 10 | Needed the ability to 'pushback' values back onto an iterator, and also 11 | was able to add a 'nonzero' test to the iterator at the same time. 12 | 13 | Discussion: 14 | 15 | adapted from: 16 | http://mail.python.org/pipermail/python-dev/2003-October/038955.html 17 | [Python-Dev] generator comprehension syntax, 18 | was: accumulator display syntax 19 | Alex Martelli aleaxit at yahoo.com 20 | Sat Oct 18 05:20:45 EDT 2003 21 | 22 | the 'nonzero' test allows you to do quick boolean tests on whether or 23 | not the iterator is empty. 24 | """ 25 | class pushback_wrapper(object): 26 | """ 27 | 28 | >>> p = pushback_wrapper(iter([1, 2, 3, 37])) 29 | >>> L = [] 30 | >>> for x in p: 31 | ... L.append(x) 32 | ... if x == 2: 33 | ... p.pushback(17) 34 | ... p.pushback(19) 35 | >>> L 36 | [1, 2, 19, 17, 3, 37] 37 | >>> bool(p) 38 | False 39 | >>> p.pushback(17) 40 | >>> bool(p) 41 | True 42 | >>> p.next() 43 | 17 44 | >>> bool(p) 45 | False 46 | >>> p = pushback_wrapper(iter([1, 2])) 47 | >>> bool(p) 48 | True 49 | >>> p.next() 50 | 1 51 | >>> p.next() 52 | 2 53 | >>> bool(p) 54 | False 55 | >>> p.next() 56 | Traceback (most recent call last): 57 | ... 58 | StopIteration 59 | >>> bool(p) 60 | False 61 | >>> p.pushback(17) 62 | >>> bool(p) 63 | True 64 | 65 | """ 66 | 67 | def __init__(self, it): 68 | self.it = it 69 | self.pushed_back = [] 70 | 71 | def __iter__(self): 72 | return self 73 | 74 | def __nonzero__(self): 75 | 76 | if self.pushed_back: 77 | return True 78 | 79 | try: 80 | self.pushed_back.insert(0, self.it.next()) 81 | except StopIteration: 82 | return False 83 | else: 84 | return True 85 | 86 | def next(self): 87 | try: 88 | return self.pushed_back.pop() 89 | except IndexError: 90 | return self.it.next() 91 | 92 | def pushback(self, item): 93 | self.pushed_back.append(item) 94 | -------------------------------------------------------------------------------- /waterworks/cookbook/reload_all_mods.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/waterworks/cookbook/reload_all_mods.py -------------------------------------------------------------------------------- /waterworks/cookbook/terminal_size.py: -------------------------------------------------------------------------------- 1 | """Not actually from the Python Cookbook, but useful nonetheless. 2 | 3 | Lifted from http://pdos.csail.mit.edu/~cblake/cls/cls.py 4 | """ 5 | 6 | import os 7 | 8 | def ioctl_GWINSZ(fd): 9 | try: 10 | import fcntl, termios, struct 11 | cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) 12 | except: 13 | return None 14 | return cr 15 | 16 | def terminal_size(default=(25, 80)): 17 | """Will return *some* terminal size. If no terminal size can be 18 | determined, returns the defauult.""" 19 | cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) # try open fds 20 | if not cr: # ...then ctty 21 | try: 22 | fd = os.open(os.ctermid(), os.O_RDONLY) 23 | cr = ioctl_GWINSZ(fd) 24 | os.close(fd) 25 | except: 26 | pass 27 | if not cr: # env vars or finally defaults 28 | try: 29 | env = os.environ 30 | cr = (env['LINES'], env['COLUMNS']) 31 | except: 32 | cr = (25, 80) 33 | return int(cr[1]), int(cr[0]) # reverse rows, cols 34 | -------------------------------------------------------------------------------- /waterworks/cookbook/zip_exc.py: -------------------------------------------------------------------------------- 1 | """ 2 | URL: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/497006 3 | Title: zip_exc(), a lazy zip() that ensures that all iterables have the same length 4 | Submitter: Peter Otten 5 | Last Updated: 2006/08/31 6 | Version no: 1.0 7 | Category: Algorithms 8 | 9 | Description: 10 | 11 | Using zip(names, values) may inadvertently eat some of your data when 12 | there are, e. g., fewer values than names. This is easy to fix with 13 | assert len(names) == len(values) if the arguments' length is known, but 14 | not if they are arbitrary iterables. With zip_exc() no such glitches 15 | go unnoticed as list(zip_exc(names, values)) throws a LengthMismatch 16 | exception if the number of names and values differ. 17 | 18 | >>> list(zip_exc([])) 19 | [] 20 | 21 | >>> list(zip_exc((), (), ())) 22 | [] 23 | 24 | >>> list(zip_exc("abc", range(3))) 25 | [('a', 0), ('b', 1), ('c', 2)] 26 | 27 | >>> try: 28 | ... list(zip_exc("", range(3))) 29 | ... except LengthMismatch: 30 | ... print "mismatch" 31 | mismatch 32 | 33 | >>> try: 34 | ... list(zip_exc(range(3), ())) 35 | ... except LengthMismatch: 36 | ... print "mismatch" 37 | mismatch 38 | 39 | >>> try: 40 | ... list(zip_exc(range(3), range(2), range(4))) 41 | ... except LengthMismatch: 42 | ... print "mismatch" 43 | mismatch 44 | 45 | >>> items = zip_exc(range(3), range(2), range(4)) 46 | >>> items.next() 47 | (0, 0, 0) 48 | >>> items.next() 49 | (1, 1, 1) 50 | >>> try: items.next() 51 | ... except LengthMismatch: print "mismatch" 52 | mismatch 53 | 54 | Discussion: 55 | 56 | My implementation looks a bit different 57 | than the straightforward approach used in 58 | http://mail.python.org/pipermail/python-3000/2006-March/000160.html, 59 | for example. 60 | 61 | To keep the performance hit low, I've tried hard move as much of the 62 | work into code written in C (The chain() and izip() functions from the 63 | marvelous itertools module). I challenge you to come up with something 64 | faster in pure Python :-) 65 | """ 66 | 67 | from itertools import chain, izip 68 | 69 | class LengthMismatch(Exception): 70 | pass 71 | 72 | def _throw(): 73 | raise LengthMismatch 74 | yield None # unreachable 75 | 76 | def _check(rest): 77 | for i in rest: 78 | try: 79 | i.next() 80 | except LengthMismatch: 81 | pass 82 | else: 83 | raise LengthMismatch 84 | return 85 | yield None # unreachable 86 | 87 | def zip_exc(*iterables): 88 | """Like itertools.izip(), but throws a LengthMismatch exception if 89 | the iterables' lengths differ. 90 | """ 91 | rest = [chain(i, _throw()) for i in iterables[1:]] 92 | first = chain(iterables[0], _check(rest)) 93 | return izip(*[first] + rest) 94 | 95 | if __name__ == "__main__": 96 | import doctest 97 | doctest.testmod() 98 | -------------------------------------------------------------------------------- /waterworks/diffprint.py: -------------------------------------------------------------------------------- 1 | """Helps visualize diffs by giving you two parallel lists to print.""" 2 | import difflib 3 | 4 | def diffprint(a, b): 5 | sm = difflib.SequenceMatcher(None, a, b) 6 | opcodes = sm.get_grouped_opcodes().next() 7 | 8 | padded_a = [] 9 | padded_b = [] 10 | for op, i1, i2, j1, j2 in opcodes: 11 | if op in ('equal', 'replace'): 12 | padded_a.extend(a[i1:i2]) 13 | padded_b.extend(b[j1:j2]) 14 | elif op == 'insert': 15 | padded_a.extend([None] * (j2 - j1)) 16 | padded_b.extend(b[j1:j2]) 17 | elif op == 'delete': 18 | padded_a.extend(a[i1:i2]) 19 | padded_b.extend([None] * (i2 - i1)) 20 | return padded_a, padded_b 21 | 22 | if __name__ == "__main__": 23 | print diffprint('0123', '1234') 24 | print diffprint('0123', '123') 25 | print diffprint('123', '153') 26 | print diffprint('123', '153333') 27 | print diffprint('153333', '123') 28 | print diffprint('1112211121', '212111212222') 29 | -------------------------------------------------------------------------------- /waterworks/robust_apply.py: -------------------------------------------------------------------------------- 1 | """Simple front-end to louie.robustapply.robust_apply. This has been 2 | abstracted to make the implementation more portable and not tied to 3 | specific dispatcher packages.""" 4 | 5 | import louie.robustapply 6 | 7 | def robust_apply(func, *args, **kwargs): 8 | """Almost identical to louie.robustapply.robust_apply except that it 9 | forces func and signature to be identical.""" 10 | return louie.robustapply.robust_apply(func, func, *args, **kwargs) 11 | 12 | if __name__ == "__main__": 13 | def test(a, b, c): 14 | print 'test', (a, b, c) 15 | 16 | robust_apply(test, a=1, b=2, c=3, d=5) 17 | robust_apply(test, 1, b=2, c=3, d=5) 18 | -------------------------------------------------------------------------------- /waterworks/setup.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | from distutils.core import setup 3 | 4 | if len(sys.argv) < 2: 5 | sys.argv.append("build") 6 | 7 | setup(name = "waterworks", 8 | version = "0.2.5", 9 | maintainer = "David McClosky", 10 | maintainer_email = "dmcc+py (at) bigasterisk.com", 11 | description = "waterworks: Because everyone has their own utility library", 12 | packages = ['cookbook', 'waterworks'], 13 | py_modules = ['AIMA', 'ExitCodes', 'FigUtil', 'Histogram', 'IntRange', 14 | 'IntShelve', 'LazyList', 'Selectron', 'Tailer', 'TeXTable', 15 | 'ThreadedJobs', 'TkGeomSavers', 'diffprint', 16 | 'iterextras', 'ClusterMetrics', 'FunctionPickler', 17 | 'HeapQueue', 'PrecRec', 'Probably', 'robust_apply', 18 | 'TerminalTitle', 'vimdiff'], 19 | url='http://cs.brown.edu/~dmcc/software/', 20 | download_url='http://cs.brown.edu/~dmcc/software/waterworks/waterworks-0.2.5.tar.gz', 21 | ) 22 | -------------------------------------------------------------------------------- /waterworks/test/test_new_isinstance.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | class A: 4 | pass 5 | 6 | class B1(A): 7 | pass 8 | 9 | class B2(A): 10 | pass 11 | 12 | class C(B1, B2): 13 | pass 14 | 15 | class D(B1): 16 | pass 17 | 18 | class E(D, B2): 19 | pass 20 | 21 | class TestNewIsInstance(unittest.TestCase): 22 | def setUp(self): 23 | self.a = A() 24 | self.b1 = B1() 25 | self.b2 = B2() 26 | self.c = C() 27 | self.d = D() 28 | self.e = E() 29 | 30 | def testbasic(self): 31 | self.failUnless(new_isinstance(self.a, A)) 32 | self.failUnless(new_isinstance(self.a, [A])) 33 | self.failUnless(new_isinstance(self.a, [A, B1])) 34 | self.failUnless(new_isinstance(self.a, [B1, A])) 35 | self.failUnless(new_isinstance(self.a, [A, int])) 36 | self.failUnless(new_isinstance(self.a, [int, A])) 37 | self.failIf(new_isinstance(self.a, int)) 38 | self.failIf(new_isinstance(self.a, [int])) 39 | self.failIf(new_isinstance(self.a, [])) 40 | 41 | def testhier(self): 42 | self.failUnless(new_isinstance(self.b1, A)) 43 | self.failUnless(new_isinstance(self.b2, A)) 44 | self.failUnless(new_isinstance(self.c, A)) 45 | self.failUnless(new_isinstance(self.d, A)) 46 | self.failUnless(new_isinstance(self.e, A)) 47 | 48 | self.failUnless(new_isinstance(self.b1, B1)) 49 | self.failUnless(new_isinstance(self.c, B1)) 50 | self.failUnless(new_isinstance(self.d, B1)) 51 | self.failUnless(new_isinstance(self.e, B1)) 52 | self.failIf(new_isinstance(self.a, B1)) 53 | self.failIf(new_isinstance(self.b2, B1)) 54 | 55 | self.failUnless(new_isinstance(self.b2, B2)) 56 | self.failUnless(new_isinstance(self.c, B2)) 57 | self.failUnless(new_isinstance(self.e, B2)) 58 | self.failIf(new_isinstance(self.a, B2)) 59 | self.failIf(new_isinstance(self.b1, B2)) 60 | self.failIf(new_isinstance(self.d, B2)) 61 | 62 | self.failUnless(new_isinstance(self.c, C)) 63 | self.failIf(new_isinstance(self.a, C)) 64 | self.failIf(new_isinstance(self.b1, C)) 65 | self.failIf(new_isinstance(self.b2, C)) 66 | self.failIf(new_isinstance(self.d, C)) 67 | self.failIf(new_isinstance(self.e, C)) 68 | 69 | self.failUnless(new_isinstance(self.d, D)) 70 | self.failUnless(new_isinstance(self.e, D)) 71 | self.failIf(new_isinstance(self.a, D)) 72 | self.failIf(new_isinstance(self.b1, D)) 73 | self.failIf(new_isinstance(self.b2, D)) 74 | self.failIf(new_isinstance(self.c, D)) 75 | 76 | self.failUnless(new_isinstance(self.e, D)) 77 | self.failUnless(new_isinstance(self.d, D)) 78 | self.failIf(new_isinstance(self.a, D)) 79 | self.failIf(new_isinstance(self.b1, D)) 80 | self.failIf(new_isinstance(self.b2, D)) 81 | self.failIf(new_isinstance(self.c, D)) 82 | 83 | def testbuiltins(self): 84 | self.failIf(new_isinstance('hi', D)) 85 | self.failIf(new_isinstance([], D)) 86 | self.failIf(new_isinstance({}, D)) 87 | self.failIf(new_isinstance(37.2, D)) 88 | 89 | self.failUnless(new_isinstance('hi', str)) 90 | self.failUnless(new_isinstance([], list)) 91 | self.failUnless(new_isinstance({}, dict)) 92 | self.failUnless(new_isinstance(37.2, float)) 93 | 94 | if __name__ == "__main__": 95 | unittest.main() 96 | -------------------------------------------------------------------------------- /waterworks/vimdiff.py: -------------------------------------------------------------------------------- 1 | """Simple interface for visualizing differences between two strings using 2 | vimdiff (configurable to use other differs too)""" 3 | 4 | from waterworks.Files import keepable_tempfile 5 | def vimdiff(*strings, **options): 6 | """Given a list of strings, visualizes the differences between them 7 | using vimdiff (or similar program). 8 | The visualizer program is specified by the command_template keyword 9 | and the default is: 10 | 11 | vimdiff -gf %s 12 | 13 | where %s will be replaced with a list of filenames.""" 14 | import sys 15 | assert len(strings) >= 2, "Must have at least two strings." 16 | file_objs = [] 17 | for i, s in enumerate(strings): 18 | tempfile = keepable_tempfile(prefix='%d-' % i) 19 | tempfile.write(s) 20 | tempfile.flush() 21 | file_objs.append(tempfile) 22 | 23 | filenames = [tempfile.name for tempfile in file_objs] 24 | command_template = options.get('command_template', 'vimdiff -gf %s') 25 | command = command_template % ' '.join(filenames) 26 | import os 27 | print "Running:", command 28 | os.system(command) 29 | 30 | def pprint_and_vimdiff(*objs, **options): 31 | """Takes objects instead of strings, run them through pprint first.""" 32 | import pprint 33 | strings = [pprint.pformat(obj) for obj in objs] 34 | return vimdiff(*strings, **options) 35 | -------------------------------------------------------------------------------- /waterworks/waterworks/Numbers.py: -------------------------------------------------------------------------------- 1 | """Collection of function(s) for working with numbers.""" 2 | 3 | def deNaN(x, default=0): 4 | """If x is NaN, return default. Otherwise, return x.""" 5 | if x != x: # a test for NaN-ness 6 | return default 7 | else: 8 | return x 9 | 10 | -------------------------------------------------------------------------------- /waterworks/waterworks/Streams.py: -------------------------------------------------------------------------------- 1 | """Collection of functions and classes for working on file streams""" 2 | 3 | import sys 4 | from cStringIO import StringIO 5 | 6 | class stream_prefixer: 7 | def __init__(self, prefix, originalstream): 8 | self.prefix = prefix 9 | self.originalstream = originalstream 10 | 11 | self._should_print_prefix = True 12 | def write(self, text): 13 | if self._should_print_prefix: 14 | text = self.prefix + text 15 | self._should_print_prefix = False 16 | if text.endswith('\n'): 17 | self._should_print_prefix = True 18 | # not super elegant -- don't prefix last newline yet 19 | text = text[:-1].replace('\n', '\n' + self.prefix) + text[-1] 20 | self.originalstream.write(text) 21 | def __getattr__(self, attr): 22 | return getattr(self.originalstream, attr) 23 | 24 | def prefix_stdout(prefix): 25 | sys.stdout = stream_prefixer(prefix, sys.stdout) 26 | 27 | class Tee: 28 | def __init__(self, stream): 29 | self.stream = stream 30 | self.output = StringIO() 31 | def write(self, s): 32 | self.stream.write(s) 33 | self.seek(0, 2) 34 | self.output.write(s) 35 | def __str__(self): 36 | self.seek(0, 0) 37 | return self.output.read() 38 | def __getattr__(self, attr): 39 | return getattr(self.output, attr) 40 | 41 | def tee_stdout(): 42 | return Tee(sys.stdout) 43 | def tee_stderr(): 44 | return Tee(sys.stderr) 45 | 46 | if __name__ == "__main__": 47 | sys.stdout = tee_stdout() 48 | 49 | print "hi!" 50 | print 'str()ed:', repr(str(sys.stdout)) 51 | -------------------------------------------------------------------------------- /waterworks/waterworks/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/waterworks/waterworks/__init__.py -------------------------------------------------------------------------------- /waterworks/waterworks/new_isinstance.py: -------------------------------------------------------------------------------- 1 | """A version of isinstance that handles module reload()s better.""" 2 | import inspect, types 3 | 4 | def obj_signature(obj): 5 | try: 6 | filename = inspect.getsourcefile(obj) 7 | lines, lineno = inspect.findsource(obj) 8 | except TypeError: 9 | filename, lineno = '__builtin__', None 10 | return (obj.__name__, filename, lineno) 11 | 12 | def new_isinstance(obj, class_or_classes): 13 | # fall back to real isinstance for these 14 | if not hasattr(obj, '__class__'): 15 | return isinstance(obj, class_or_classes) 16 | 17 | if isinstance(class_or_classes, (tuple, list)): 18 | classes = class_or_classes 19 | else: 20 | classes = [class_or_classes] 21 | 22 | all_sigs = map(obj_signature, classes) 23 | 24 | for ancestor in inspect.getmro(obj.__class__): 25 | sig = obj_signature(ancestor) 26 | if sig in all_sigs: 27 | return True 28 | 29 | return False 30 | -------------------------------------------------------------------------------- /web/simple_flask_server.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask import render_template 3 | from walkdir import filtered_walk, file_paths 4 | 5 | app = Flask(__name__) 6 | 7 | 8 | def index_podcasts(): 9 | files = file_paths(filtered_walk('static/podcasts', included_files=['*.mp3'], )) 10 | return files 11 | 12 | 13 | def index_videos(): 14 | files = file_paths(filtered_walk('static/podcasts', included_files=['*.mp4'], )) 15 | return files 16 | 17 | 18 | @app.route('/directory', methods=['GET', 'POST']) 19 | def directory(): 20 | pass 21 | 22 | @app.route('/') 23 | def index(): 24 | filenames = index_podcasts() 25 | videos = index_videos() 26 | return render_template('index.html', filenames=filenames, videos=videos) 27 | 28 | 29 | @app.route('/video/<path:filename>') 30 | def video(filename=None): 31 | return render_template('video.html', filename=filename) 32 | 33 | 34 | @app.route('/audio/<path:filename>') 35 | def audio(filename=None): 36 | return render_template('audio.html', filename=filename) 37 | 38 | if __name__ == '__main__': 39 | app.run(debug=True) 40 | -------------------------------------------------------------------------------- /webkit/webkit_preso.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # [SNIPPET_NAME: Presenter] 4 | # [SNIPPET_CATEGORIES: Webkit] 5 | # [SNIPPET_DESCRIPTION: Fullscreen presenter program, for HTML presentations] 6 | # [SNIPPET_AUTHOR: Akkana Peck <akkana@shallowsky.com>] 7 | # [SNIPPET_DOCS: http://shallowsky.com/blog/programming/webkit-presenter.html] 8 | # [SNIPPET_LICENSE: GPL] 9 | 10 | import sys, os 11 | import gtk, gobject 12 | import webkit 13 | 14 | class WebBrowser(gtk.Window): 15 | def __init__(self, url): 16 | gtk.Window.__init__(self) 17 | 18 | # Either run fullscreen, or set an initial window size 19 | #self.set_default_size(1024,768) 20 | self.fullscreen() 21 | 22 | self._browser= webkit.WebView() 23 | self.add(self._browser) 24 | self.connect('destroy', gtk.main_quit) 25 | 26 | self._browser.open(url) # throw err if url isn't defined 27 | self.show_all() 28 | 29 | if __name__ == "__main__": 30 | if len(sys.argv) <= 1 : 31 | print "Usage:", sys.argv[0], "url" 32 | sys.exit(0) 33 | 34 | # Figure out if it's a filename or a url 35 | url = sys.argv[1] 36 | if url.find(':') < 0 : 37 | # If it's a local file, it needs to be converted to an absolute URL 38 | if url[0] == '/' : 39 | url = 'file://' + url 40 | else : 41 | url = 'file://' + os.getcwd() + '/' + url 42 | 43 | gobject.threads_init() 44 | webbrowser = WebBrowser(url) 45 | gtk.main() 46 | 47 | -------------------------------------------------------------------------------- /webkit/webkit_table-business.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: black; 3 | background-color: white; 4 | font-size: 80%; 5 | } 6 | table { 7 | border-collapse: collapse; 8 | border-bottom: 2px solid black; 9 | } 10 | thead { 11 | color: white; 12 | background-color: black; 13 | font-weight: bold; 14 | } 15 | thead th { 16 | padding: 0 1em; 17 | } 18 | tbody { 19 | font-size: 80%; 20 | } 21 | tbody tr:nth-child(even) { 22 | background: #eee; 23 | } 24 | td.left { 25 | text-align: left; 26 | } 27 | td.right { 28 | text-align: right; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /webkit/webkit_table-colourful.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: black; 3 | background-color: white; 4 | font-size: 80%; 5 | } 6 | h1 { 7 | color: #B90091; 8 | } 9 | table { 10 | border-collapse: collapse; 11 | } 12 | thead { 13 | color: white; 14 | background-color: #B90091; 15 | font-weight: bold; 16 | } 17 | thead th { 18 | padding: 0 1em; 19 | } 20 | tbody { 21 | font-size: 80%; 22 | } 23 | tbody tr:nth-child(odd) { 24 | background: #48DD00; 25 | } 26 | tbody tr:nth-child(even) { 27 | background: #FFF100; 28 | } 29 | td.left { 30 | text-align: left; 31 | } 32 | td.right { 33 | text-align: right; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /webkit/webkit_table-plain.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size: 80%; 3 | } 4 | td.left { 5 | text-align: left; 6 | } 7 | td.right { 8 | text-align: right; 9 | } 10 | 11 | -------------------------------------------------------------------------------- /webkit/webkit_table-rounded.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size: 80%; 3 | color: white; 4 | } 5 | h1 { 6 | background-color: #090; 7 | padding: 0.25em; 8 | border-radius: 0.5em; 9 | } 10 | div#content { 11 | background-color: #090; 12 | padding: 0.5em; 13 | border-radius: 1em; 14 | } 15 | table { 16 | border-collapse: collapse; 17 | } 18 | thead th { 19 | padding: 0 1em; 20 | font-weight: bold; 21 | } 22 | tbody { 23 | font-size: 80%; 24 | } 25 | tbody tr:nth-child(even) { 26 | background: #6c6; 27 | } 28 | td.left { 29 | text-align: left; 30 | } 31 | td.right { 32 | text-align: right; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /webkit/webkit_table.csv: -------------------------------------------------------------------------------- 1 | "100 grams","Pure chocolate","Milk chocolate","White chocolate" 2 | "Protein (g)",4.7,8.4,8 3 | "Fat (g)",29.2,30.3,30.9 4 | "Calories (kcal)",525,529,529 5 | "Carbohydrate (g)",64.8,59.4,58.3 6 | "Calcium (mg)",38,220,270 7 | "Magnesium (mg)",100,55,26 8 | "Iron (mg)",2.4,1.6,0.2 9 | "Zink (mg)",0.2,0.2,0.9 10 | "Vitamin A (µg)",40,40,75 11 | "Vitamin E (mg)",0.85,0.74,1.14 12 | "Vitamin B1 (mg)",0.07,0.1,0.08 13 | "Vitamin B2 (mg)",0.08,0.23,0.49 14 | "Vitamin B3 (mg)",0.4,0.2,0.2 15 | "Vitamin B6 (mg)",0.07,0.07,0.07 16 | "Vitamin B12 (µg)","-","trace","trace" 17 | "Folate (µg)",10,10,10 18 | -------------------------------------------------------------------------------- /wxBased/Data/file.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/brent-hoover/python-snippets/9666bc466f11bf6fd669e63ef6c1d237839c4747/wxBased/Data/file.db -------------------------------------------------------------------------------- /wxBased/wxframe.py: -------------------------------------------------------------------------------- 1 | #File: Frame_2.py 2 | #Author: Me 3 | #Description: 4 | import wx 5 | class MyFrame(wx.Frame): 6 | """docstring for MyFrame""" 7 | def __init__(self, parent, id): 8 | wx.Frame.__init__(self, parent, -1, "Frame with Menu and Status", 9 | pos = (125, 10), size = (300, 100)) 10 | panel = wx.Panel(self, -1) 11 | panel.SetBackgroundColour("White") 12 | statusBar = self.CreateStatusBar() 13 | toolBar = self.CreateToolBar() 14 | toolBar.Realize() 15 | menuBar = wx.MenuBar() 16 | menu1 = wx.Menu() 17 | menuBar.Append(menu1, "&File") 18 | menu2 = wx.Menu() 19 | menu2.Append(wx.NewId(), "&Copy", "Copy in status bar") 20 | menu2.Append(wx.NewId(), "C&ut", "") 21 | menu2.Append(wx.NewId(), "Paste", "") 22 | menu2.AppendSeparator() 23 | menu2.Append(wx.NewId(), "&Options...", "Display Options") 24 | menuBar.Append(menu2, "&Edit") 25 | self.SetMenuBar(menuBar) 26 | if __name__ == '__main__': 27 | app = wx.PySimpleApp() 28 | frame = MyFrame(parent = None, id = -1) 29 | frame.Show() 30 | app.MainLoop() -------------------------------------------------------------------------------- /xmpp/xmpp_client.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Twisted Matrix Laboratories. 2 | # See LICENSE for details. 3 | 4 | import sys 5 | from twisted.internet import reactor 6 | from twisted.names.srvconnect import SRVConnector 7 | from twisted.words.xish import domish 8 | from twisted.words.protocols.jabber import xmlstream, client, jid 9 | 10 | 11 | class XMPPClientConnector(SRVConnector): 12 | def __init__(self, reactor, domain, factory): 13 | SRVConnector.__init__(self, reactor, 'xmpp-client', domain, factory) 14 | 15 | 16 | def pickServer(self): 17 | host, port = SRVConnector.pickServer(self) 18 | 19 | if not self.servers and not self.orderedServers: 20 | # no SRV record, fall back.. 21 | port = 5222 22 | 23 | return host, port 24 | 25 | 26 | 27 | class Client(object): 28 | def __init__(self, client_jid, secret): 29 | f = client.XMPPClientFactory(client_jid, secret) 30 | f.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self.connected) 31 | f.addBootstrap(xmlstream.STREAM_END_EVENT, self.disconnected) 32 | f.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.authenticated) 33 | f.addBootstrap(xmlstream.INIT_FAILED_EVENT, self.init_failed) 34 | connector = XMPPClientConnector(reactor, client_jid.host, f) 35 | connector.connect() 36 | 37 | 38 | def rawDataIn(self, buf): 39 | print "RECV: %s" % unicode(buf, 'utf-8').encode('ascii', 'replace') 40 | 41 | 42 | def rawDataOut(self, buf): 43 | print "SEND: %s" % unicode(buf, 'utf-8').encode('ascii', 'replace') 44 | 45 | 46 | def connected(self, xs): 47 | print 'Connected.' 48 | 49 | self.xmlstream = xs 50 | 51 | # Log all traffic 52 | xs.rawDataInFn = self.rawDataIn 53 | xs.rawDataOutFn = self.rawDataOut 54 | 55 | 56 | def disconnected(self, xs): 57 | print 'Disconnected.' 58 | 59 | reactor.stop() 60 | 61 | 62 | def authenticated(self, xs): 63 | print "Authenticated." 64 | 65 | presence = domish.Element((None, 'presence')) 66 | xs.send(presence) 67 | 68 | reactor.callLater(5, xs.sendFooter) 69 | 70 | 71 | def init_failed(self, failure): 72 | print "Initialization failed." 73 | print failure 74 | 75 | self.xmlstream.sendFooter() 76 | 77 | 78 | client_jid = jid.JID(sys.argv[1]) 79 | secret = sys.argv[2] 80 | c = Client(client_jid, secret) 81 | 82 | reactor.run() 83 | --------------------------------------------------------------------------------