CS 3723
Programming Languages
Code from
Python Cookbook
by Beazley and Jones
[Note: If there's no filename at the end of a URL below, "/example.py" is assumed to be at the end.]
Chapter 1
calculating_with_dictionaries
determine_the_top_n_items_occurring_in_a_list
extracting_a_subset_of_a_dictionary
filtering_list_elements
finding_out_what_two_dictionaries_have_in_common
finding_the_largest_or_smallest_n_items
grouping-records-together-based-on-a-field
implementing_a_priority_queue
keeping_the_last_n_items
mapping_names_to_sequence_elements
removing_duplicates_from_a_sequence_while_maintaining_order
sort_a_list_of_dictionaries_by_a_common_key
sort_objects_without_native_comparison_support
transforming_and_reducing_data_at_the_same_time
unpack_a_fixed_number_of_elements_from_iterables_of_arbitrary_length
working_with_multiple_mappings_as_a_single_mapping
Chapter 2
combining_and_concatenating_strings
matching_and_searching_for_text_patterns_using_regular_expressions
matching_strings_using_shell_wildcard_patterns
normalizing_unicode_text_to_a_standard_representation
reformatting_text_to_fixed_number_of_columns
sanitizing_and_cleaning_up_text
searching_and_replacing_text
specifying_a_regular_expression_for_the_shortest_match
splitting_strings_on_any_of_multiple_delimiters
tokenizing_text
variable_interpolation_in_strings
writing_a_regular_expression_for_multiline_patterns
writing_a_simple_recursive_descent_parser
Chapter 3
determining_last_fridays_date
finding_the_date_range_for_the_current_month
Chapter 4
creating_data_processing_pipelines
creating_new_iteration_patterns_with_generators
delegating-iteration
easy_implementation_of_the_iterator_protocol
generators_with_state
how_to_flatten_a_nested_sequence
iterate_over_the_index-value_pairs_of_a_list
iterating_in_reverse
iterating_in_sorted_order_over_merged_sorted_iterables
iterating_on_items_in_separate_containers
Chapter 5
adding_or_changing_the_encoding_of_an_already_open_file
getting_a_directory_listing
iterating_over_fixed-sized_records
reading_and_writing_text_data
wrapping_an_existing_file_descriptor_as_a_file_object
writing_bytes_to_a_text_file
Chapter 6
incremental_parsing_of_huge_xml_files
parsing_modifying_and_rewriting_xml
parsing_simple_xml_data
parsing_xml_documents_with_namespaces
reading_and_writing_binary_arrays_of_structures/readrecords.py
reading_and_writing_binary_arrays_of_structures/unpackrecords.py
reading_and_writing_binary_arrays_of_structures/writerecords.py
reading_and_writing_csv_data
reading_and_writing_json_data
reading_nested_and_variable_sized_binary_structures/example1.py
reading_nested_and_variable_sized_binary_structures/example2.py
reading_nested_and_variable_sized_binary_structures/example3.py
reading_nested_and_variable_sized_binary_structures/example4.py
reading_nested_and_variable_sized_binary_structures/writepolys.py
Chapter 7
accessing_variables_defined_inside_a_closure/example1.py
accessing_variables_defined_inside_a_closure/example2.py
accessing_variables_defined_inside_a_closure/example3.py
carrying_extra_state_with_callback_functions
functions_that_accept_any_number_of_arguments
functions_that_only_accept_keyword_arguments
functions_with_default_arguments
inlining_callback_functions
making_an_n-argument_callable_work_as_a_callable_with_fewer_arguments/example1.py
making_an_n-argument_callable_work_as_a_callable_with_fewer_arguments/example2.py
making_an_n-argument_callable_work_as_a_callable_with_fewer_arguments/example3.py
Chapter 8
calling_a_method_on_an_object_given_the_name_as_a_string
calling_a_method_on_a_parent_class/example1.py
calling_a_method_on_a_parent_class/example2.py
calling_a_method_on_a_parent_class/example3.py
calling_a_method_on_a_parent_class/example4.py
calling_a_method_on_a_parent_class/example5.py
changing_the_string_representation_of_instances
creating_a_new_kind_of_class_or_instance_attribute/example1.py
creating_a_new_kind_of_class_or_instance_attribute/example2.py
creating_an_instance_without_invoking_init
creating_cached_instances/example1.py
creating_cached_instances/example2.py
creating_cached_instances/example3.py
creating_managed_attributes
customized_formatting
delegation_and_proxies/example1.py
delegation_and_proxies/example2.py
delegation_and_proxies/example3.py
delegation_and_proxies/example4.py
extending_a_property_in_a_subclass
extending_classes_with_mixins/example1.py
extending_classes_with_mixins/example2.py
extending_classes_with_mixins/example3.py
how_to_define_an_interface_or_abstract_base_class
how_to_define_more_than_one_constructor_in_a_class
how_to_encapsulate_names_in_a_class
implementing_a_data_model_or_type_system
implementing_custom_containers/example1.py
implementing_custom_containers/example2.py
implementing_stateful_objects_or_state_machines/example1.py
implementing_stateful_objects_or_state_machines/example2.py
implementing_the_visitor_pattern
implementing_the_visitor_pattern_without_recursion/example1.py
implementing_the_visitor_pattern_without_recursion/example2.py
implementing_the_visitor_pattern_without_recursion/example3.py
implementing_the_visitor_pattern_without_recursion/node.py
lazily_computed_attributes/example1.py
lazily_computed_attributes/example2.py
making_classes_support_comparison_operations
making_objects_support_the_context_manager_protocol/example1.py
making_objects_support_the_context_manager_protocol/example2.py
managing_memory_in_cyclic_data_structures
simplified_initialization_of_data_structures/example1.py
simplified_initialization_of_data_structures/example2.py
simplified_initialization_of_data_structures/example3.py
Chapter 9
applying_decorators_to_class_and_static_methods
avoiding_repetitive_property_methods
capturing_class_attribute_definition_order/example1.py
capturing_class_attribute_definition_order/example2.py
defining_a_decorator_that_takes_an_optional_argument
defining_a_decorator_that_takes_arguments
defining_a_decorator_with_user_adjustable_attributes/example1.py
defining_a_decorator_with_user_adjustable_attributes/example2.py
defining_a_metaclass_that_takes_optional_arguments
defining_classes_programmatically/example1.py
defining_classes_programmatically/example2.py
defining_context_managers_the_easy_way/example1.py
defining_context_managers_the_easy_way/example2.py
defining_decorators_as_classes/example1)
defining_decorators_as_classes/example2)
defining_decorators_as_classes/example3)
defining_decorators_as_part_of_a_class/example1.py
defining_decorators_as_part_of_a_class/example2.py
disassembling_python_byte_code
enforcing_an_argument_signature/example1.py
enforcing_an_argument_signature/example2.py
enforcing_coding_conventions_in_classes/example1.py
enforcing_coding_conventions_in_classes/example2.py
enforcing_type_checking_on_a_function_using_a_decorator
executing_code_with_local_side_effects
initializing_class_members_at_definition_time
monkeypatching_class_definitions
multiple_dispatch_with_function_annotations/example1.py
multiple_dispatch_with_function_annotations/example1.py
parsing_and_analyzing_python_source/example1.py
parsing_and_analyzing_python_source/namelower.py
preserving_function_metadata_when_writing_decorators
unwrapping_a_decorator
using_metaclasses_to_control_instance_creation/example1.py
using_metaclasses_to_control_instance_creation/example2.py
using_metaclasses_to_control_instance_creation/example3.py
Chapter 10
loading_modules_from_a_remote_machine_using_import_hooks/explicit_load.py
loading_modules_from_a_remote_machine_using_import_hooks/metaexample.py
loading_modules_from_a_remote_machine_using_import_hooks/pathexample.py
loading_modules_from_a_remote_machine_using_import_hooks/urlimport.py
(plus test data)
making_separate_directories_import_under_a_common_namespace
monkeypatching_modules_on_import/example1.py
monkeypatching_modules_on_import/example2.py
monkeypatching_modules_on_import/postimport.py
splitting_a_module_into_multiple_files
Chapter 11
adding_ssl_to_network_servers/echoclient.py
adding_ssl_to_network_servers/echoserv.py
adding_ssl_to_network_servers/makecerts.sh
adding_ssl_to_network_servers/sslmixin.py
adding_ssl_to_network_servers/ssl_xmlrpc_client.py
adding_ssl_to_network_servers/ssl_xmlrpc_server.py
creating_a_simple_rest_based_interface/client1.py
creating_a_simple_rest_based_interface/example1.py
creating_a_simple_rest_based_interface/resty.py
creating_a_tcp_server/echoclient.py
creating_a_tcp_server/echoserv1.py
creating_a_tcp_server/echoserv2.py
creating_a_tcp_server/echoserv3.py
creating_a_tcp_server/echoserv4.py
creating_a_tcp_server/echoserv5.py
creating_a_tcp_server/echoserv.py
creating_a_tcp_server/threadedserv.py
creating_a_udp_server/client.py
creating_a_udp_server/timeserv1.py
creating_a_udp_server/timeserv2.py
event_driven_io_explained/eventhandler.py
event_driven_io_explained/tcpclient.py
event_driven_io_explained/tcpserver.py
event_driven_io_explained/threadpool.py
event_driven_io_explained/thrpoolclient.py
event_driven_io_explained/udpclient.py
event_driven_io_explained/udpserver.py
implementing_remote_procedure_call/jsonrpclient.py
implementing_remote_procedure_call/jsonrpcserver.py
implementing_remote_procedure_call/rpcclient.py
implementing_remote_procedure_call/rpcserver.py
interacting_with_http_services_as_a_client/example1.py
interacting_with_http_services_as_a_client/example2.py
interacting_with_http_services_as_a_client/example3.py
passing_a_socket_file_descriptor_between_processes/client1.py
passing_a_socket_file_descriptor_between_processes/server1.py
passing_a_socket_file_descriptor_between_processes/servermp.py
passing_a_socket_file_descriptor_between_processes/server.py
passing_a_socket_file_descriptor_between_processes/workermp.py
passing_a_socket_file_descriptor_between_processes/worker.py
simple_authentication_of_clients/auth.py
simple_authentication_of_clients/client.py
simple_authentication_of_clients/server.py
simple_communication_between_interpreters/echoclient.py
simple_communication_between_interpreters/echoserv.py
simple_remote_procedure_call_with_xmlrpcclient.py
simple_remote_procedure_call_with_xmlrpckeyserv.py
zero_copy_sending_and_receiving_of_large_arrays/client.py
zero_copy_sending_and_receiving_of_large_arrays/server.py
zero_copy_sending_and_receiving_of_large_arrays/zerocopy.py
Chapter 12
defining_an_actor_task/actor.py
defining_an_actor_task/tagged.py
defining_an_actor_task/worker.py
how_to_communicate_between_threads/example1.py
how_to_communicate_between_threads/example2.py
how_to_create_a_thread_pool/example1.py
how_to_create_a_thread_pool/example2.py
how_to_create_a_thread_pool/example3.py
how_to_determine_if_a_thread_has_started/example1.py
how_to_determine_if_a_thread_has_started/example2.py
how_to_determine_if_a_thread_has_started/example3.py
how_to_lock_critical_sections/example1.py
how_to_start_and_stop_threads
implementing_publish_subscribe_messaging/exchange1.py
implementing_publish_subscribe_messaging/exchange2.py
launching_a_daemon_process_on_unix/daemon.py
locking_with_deadlock_avoidance/deadlock.py
locking_with_deadlock_avoidance/example1.py
locking_with_deadlock_avoidance/example2.py
locking_with_deadlock_avoidance/example3.py
polling_multiple_thread_queues/pqueue.py
simple_parallel_programming/findrobots.py
simple_parallel_programming/findrobots_par.py
storing_thread_specific_state/example1.py
storing_thread_specific_state/example2.py
using_generators_as_an_alternative_to_threads/actorsched.py
using_generators_as_an_alternative_to_threads/netsched.py
using_generators_as_an_alternative_to_threads/simple.py
Chapter 13
adding_logging_to_libraries/somelib.py
executing_an_external_command_and_getting_its_output/example1.py
executing_an_external_command_and_getting_its_output/example2.py
finding_files/modified_within.py
generating_a_range_of_ip_addresses_from_a_cidr_address
getting_the_terminal_size
making_a_stopwatch/stopwatch.py
parsing_command_line_options/search.py
prompting_for_a_password_at_runtime
putting_limits_on_memory_and_cpu_usage
reading_configuration_files/example1.py
reading_configuration_files/config.ini)
simple_logging_for_scripts/example1.py
simple_logging_for_scripts/example2.py
simple_logging_for_scripts/logconfig.ini
Chapter 14
logging_test_output_to_a_file/test.py
make_your_programs_run_faster
profiling_and_timing_your_program/timethis.py
raising_an_exception_in_response_to_another_exception
skipping_or_anticipating_test_failures/test.py
testing_for_exceptional_conditions_in_unit_tests/test.py
testing_output_sent_to_stdout/mymodule.py
testing_output_sent_to_stdout/testmymodule.py
Chapter 15
accessing_c_code_using_ctypes
calling_python_from_c/embed.c)
calling_python_from_c/Makefile)
consuming_an_iterable_from_c
defining_and_exporting_c_apis_from_extension_modules
diagnosing_segmentation_faults
Makefile
managing_opaque_pointers_in_c_extension_modules
passing_null_terminated_strings_to_c_libraries
passing_unicode_strings_to_c_libraries
reading_file_like_objects_from_c
sample.c
sample.h
turning_a_function_pointer_into_a_callable
using_cython_to_write_high_performance_array_operations
working_with_c_strings_of_dubious_encoding
wrapping_c_code_with_swig
wrapping_existing_c_code_with_cython
writing_an_extension_function_that_operates_on_arrays
writing_a_simple_c_extension_module