├── .flake8 ├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.rst ├── bin ├── sitemap_gen.py ├── strip.py └── update.sh ├── homebrew.sh ├── pavement.py ├── pearson ├── newphstyle.cls └── newphstyle.sty ├── requirements.txt ├── setup.cfg ├── sitemap_gen_config.xml ├── source ├── _exts │ ├── figureref.py │ ├── pearson │ │ └── writer.py │ ├── pydoc.py │ └── tableref.py ├── _static │ └── .placeholder ├── _themes │ └── pymotw │ │ ├── domainindex.html │ │ ├── genindex-single.html │ │ ├── genindex-split.html │ │ ├── genindex.html │ │ ├── layout.html │ │ ├── localtoc.html │ │ ├── page.html │ │ ├── search.html │ │ ├── searchbox.html │ │ ├── searchresults.html │ │ ├── sidebar_ads.html │ │ ├── sidebar_book.html │ │ ├── sidebar_examples.html │ │ ├── sidebar_lastupdated.html │ │ ├── sidebar_nav.html │ │ ├── sidebar_subscribe.html │ │ ├── sidebar_toc.html │ │ ├── static │ │ ├── book-cover-image-med.jpg │ │ ├── book-cover-image-small.jpg │ │ ├── cc-30-by-nc-sa.png │ │ ├── dh-green-hosting.gif │ │ ├── favicon.png │ │ ├── font-awesome │ │ │ ├── css │ │ │ │ ├── font-awesome-ie7.css │ │ │ │ ├── font-awesome-ie7.min.css │ │ │ │ ├── font-awesome.css │ │ │ │ └── font-awesome.min.css │ │ │ ├── font │ │ │ │ ├── FontAwesome.otf │ │ │ │ ├── fontawesome-webfont.eot │ │ │ │ ├── fontawesome-webfont.svg │ │ │ │ ├── fontawesome-webfont.ttf │ │ │ │ └── fontawesome-webfont.woff │ │ │ ├── fonts │ │ │ │ ├── FontAwesome.otf │ │ │ │ ├── fontawesome-webfont.eot │ │ │ │ ├── fontawesome-webfont.svg │ │ │ │ ├── fontawesome-webfont.ttf │ │ │ │ └── fontawesome-webfont.woff │ │ │ ├── less │ │ │ │ ├── bootstrap.less │ │ │ │ ├── bordered-pulled.less │ │ │ │ ├── core.less │ │ │ │ ├── extras.less │ │ │ │ ├── fixed-width.less │ │ │ │ ├── font-awesome-ie7.less │ │ │ │ ├── font-awesome.less │ │ │ │ ├── icons.less │ │ │ │ ├── larger.less │ │ │ │ ├── list.less │ │ │ │ ├── mixins.less │ │ │ │ ├── path.less │ │ │ │ ├── rotated-flipped.less │ │ │ │ ├── spinning.less │ │ │ │ ├── stacked.less │ │ │ │ └── variables.less │ │ │ └── scss │ │ │ │ ├── _bootstrap.scss │ │ │ │ ├── _bordered-pulled.scss │ │ │ │ ├── _core.scss │ │ │ │ ├── _extras.scss │ │ │ │ ├── _fixed-width.scss │ │ │ │ ├── _icons.scss │ │ │ │ ├── _larger.scss │ │ │ │ ├── _list.scss │ │ │ │ ├── _mixins.scss │ │ │ │ ├── _path.scss │ │ │ │ ├── _rotated-flipped.scss │ │ │ │ ├── _spinning.scss │ │ │ │ ├── _stacked.scss │ │ │ │ ├── _variables.scss │ │ │ │ ├── font-awesome-ie7.scss │ │ │ │ └── font-awesome.scss │ │ ├── logo-large.png │ │ ├── logo.png │ │ ├── pure-min.css │ │ ├── pymotw.less │ │ └── vars.less │ │ └── theme.conf ├── abc │ ├── abc_abc_base.py │ ├── abc_abstractproperty.py │ ├── abc_abstractproperty_rw.py │ ├── abc_base.py │ ├── abc_class_static.py │ ├── abc_concrete_method.py │ ├── abc_find_subclasses.py │ ├── abc_incomplete.py │ ├── abc_register.py │ ├── abc_subclass.py │ └── index.rst ├── about.rst ├── algorithm_tools.rst ├── argparse │ ├── argparse_FileType.py │ ├── argparse_action.py │ ├── argparse_arguments.py │ ├── argparse_choices.py │ ├── argparse_conflict_handler_resolve.py │ ├── argparse_conflict_handler_resolve2.py │ ├── argparse_custom_action.py │ ├── argparse_custom_help.py │ ├── argparse_default_grouping.py │ ├── argparse_fromfile_prefix_chars.py │ ├── argparse_fromfile_prefix_chars.txt │ ├── argparse_long.py │ ├── argparse_metavar_type_help_formatter.py │ ├── argparse_mutually_exclusive.py │ ├── argparse_nargs.py │ ├── argparse_parent_base.py │ ├── argparse_parent_with_group.py │ ├── argparse_prefix_chars.py │ ├── argparse_raw_description_help_formatter.py │ ├── argparse_raw_text_help_formatter.py │ ├── argparse_short.py │ ├── argparse_subparsers.py │ ├── argparse_type.py │ ├── argparse_uses_parent.py │ ├── argparse_uses_parent_with_group.py │ ├── argparse_with_help.py │ ├── argparse_with_shlex.ini │ ├── argparse_with_shlex.py │ ├── argparse_without_help.py │ ├── index.rst │ └── tmp_file.txt ├── array │ ├── array_byteswap.py │ ├── array_file.py │ ├── array_sequence.py │ ├── array_string.py │ ├── array_tobytes.py │ └── index.rst ├── asyncio │ ├── asyncio_as_completed.py │ ├── asyncio_call_at.py │ ├── asyncio_call_later.py │ ├── asyncio_call_soon.py │ ├── asyncio_cancel_task.py │ ├── asyncio_cancel_task2.py │ ├── asyncio_condition.py │ ├── asyncio_coroutine.py │ ├── asyncio_coroutine_chain.py │ ├── asyncio_coroutine_return.py │ ├── asyncio_create_task.py │ ├── asyncio_debug.py │ ├── asyncio_echo_client_coroutine.py │ ├── asyncio_echo_client_protocol.py │ ├── asyncio_echo_client_ssl.py │ ├── asyncio_echo_server_coroutine.py │ ├── asyncio_echo_server_generator.py │ ├── asyncio_echo_server_protocol.py │ ├── asyncio_echo_server_ssl.py │ ├── asyncio_ensure_future.py │ ├── asyncio_event.py │ ├── asyncio_executor_process.py │ ├── asyncio_executor_thread.py │ ├── asyncio_future_await.py │ ├── asyncio_future_callback.py │ ├── asyncio_future_event_loop.py │ ├── asyncio_gather.py │ ├── asyncio_generator.py │ ├── asyncio_getaddrinfo.py │ ├── asyncio_getnameinfo.py │ ├── asyncio_lock.py │ ├── asyncio_queue.py │ ├── asyncio_signal.py │ ├── asyncio_stop.py │ ├── asyncio_subprocess_coroutine.py │ ├── asyncio_subprocess_coroutine_write.py │ ├── asyncio_subprocess_protocol.py │ ├── asyncio_wait.py │ ├── asyncio_wait_timeout.py │ ├── concepts.rst │ ├── control.rst │ ├── coroutines.rst │ ├── debugging.rst │ ├── dns.rst │ ├── executors.rst │ ├── futures.rst │ ├── index.rst │ ├── io_coroutine.rst │ ├── io_protocol.rst │ ├── pymotw.crt │ ├── pymotw.key │ ├── scheduling.rst │ ├── ssl.rst │ ├── subprocesses.rst │ ├── synchronization.rst │ ├── tasks.rst │ └── unix_signals.rst ├── atexit │ ├── atexit_decorator.py │ ├── atexit_exception.py │ ├── atexit_keyboardinterrupt.py │ ├── atexit_multiple.py │ ├── atexit_os_exit.py │ ├── atexit_signal_child.py │ ├── atexit_signal_parent.py │ ├── atexit_simple.py │ ├── atexit_sys_exit.py │ ├── atexit_unregister.py │ ├── atexit_unregister_not_registered.py │ └── index.rst ├── base64 │ ├── base64_b64decode.py │ ├── base64_b64encode.py │ ├── base64_base16.py │ ├── base64_base32.py │ ├── base64_base85.py │ ├── base64_urlsafe.py │ └── index.rst ├── bisect │ ├── bisect_example.py │ ├── bisect_example2.py │ └── index.rst ├── book.rst ├── bz2 │ ├── bz2_file_compresslevel.py │ ├── bz2_file_read.py │ ├── bz2_file_seek.py │ ├── bz2_file_write.py │ ├── bz2_file_writelines.py │ ├── bz2_incremental.py │ ├── bz2_lengths.py │ ├── bz2_memory.py │ ├── bz2_mixed.py │ ├── bz2_server.py │ ├── bz2_unicode.py │ ├── index.rst │ └── lorem.txt ├── calendar │ ├── calendar_formatyear.py │ ├── calendar_htmlcalendar.py │ ├── calendar_locale.py │ ├── calendar_monthcalendar.py │ ├── calendar_secondthursday.py │ ├── calendar_textcalendar.py │ ├── calendar_yeardays2calendar.py │ └── index.rst ├── cgitb │ ├── cgitb_basic_traceback.py │ ├── cgitb_exception_properties.py │ ├── cgitb_extended_traceback.py │ ├── cgitb_html_output.py │ ├── cgitb_local_vars.py │ ├── cgitb_log_exception.py │ ├── cgitb_more_context.py │ ├── cgitb_with_classes.py │ └── index.rst ├── cmd │ ├── cmd_arg_completion.py │ ├── cmd_arguments.py │ ├── cmd_argv.py │ ├── cmd_attributes.py │ ├── cmd_do_help.py │ ├── cmd_do_shell.py │ ├── cmd_file.py │ ├── cmd_file.txt │ ├── cmd_illustrate_methods.py │ ├── cmd_simple.py │ └── index.rst ├── codecs │ ├── codecs_bom.py │ ├── codecs_bom_create_file.py │ ├── codecs_bom_detection.py │ ├── codecs_decode.py │ ├── codecs_decode_error.py │ ├── codecs_encode_error.py │ ├── codecs_encodedfile.py │ ├── codecs_encodings.py │ ├── codecs_incremental_bz2.py │ ├── codecs_invertcaps.py │ ├── codecs_invertcaps_charmap.py │ ├── codecs_invertcaps_error.py │ ├── codecs_invertcaps_register.py │ ├── codecs_open_read.py │ ├── codecs_open_write.py │ ├── codecs_register.py │ ├── codecs_rot13.py │ ├── codecs_socket.py │ ├── codecs_socket_fail.py │ ├── codecs_to_hex.py │ ├── codecs_zlib.py │ └── index.rst ├── collections │ ├── abc.rst │ ├── chainmap.rst │ ├── collections_chainmap_new_child.py │ ├── collections_chainmap_new_child_explicit.py │ ├── collections_chainmap_read.py │ ├── collections_chainmap_reorder.py │ ├── collections_chainmap_update_behind.py │ ├── collections_chainmap_update_directly.py │ ├── collections_counter_arithmetic.py │ ├── collections_counter_elements.py │ ├── collections_counter_get_values.py │ ├── collections_counter_init.py │ ├── collections_counter_most_common.py │ ├── collections_counter_update.py │ ├── collections_defaultdict.py │ ├── collections_deque.py │ ├── collections_deque_both_ends.py │ ├── collections_deque_consuming.py │ ├── collections_deque_maxlen.py │ ├── collections_deque_populating.py │ ├── collections_deque_rotate.py │ ├── collections_namedtuple_asdict.py │ ├── collections_namedtuple_bad_fields.py │ ├── collections_namedtuple_fields.py │ ├── collections_namedtuple_immutable.py │ ├── collections_namedtuple_person.py │ ├── collections_namedtuple_rename.py │ ├── collections_namedtuple_replace.py │ ├── collections_ordereddict_equality.py │ ├── collections_ordereddict_iter.py │ ├── collections_ordereddict_move_to_end.py │ ├── collections_tuple.py │ ├── counter.rst │ ├── defaultdict.rst │ ├── deque.rst │ ├── index.rst │ ├── namedtuple.rst │ └── ordereddict.rst ├── compileall │ ├── compileall_compile_dir.py │ ├── compileall_compile_file.py │ ├── compileall_exclude_dirs.py │ ├── compileall_path.py │ ├── compileall_recursion_depth.py │ ├── examples │ │ ├── README │ │ ├── a.py │ │ └── subdir │ │ │ └── b.py │ └── index.rst ├── compression.rst ├── concurrency.rst ├── concurrent.futures │ ├── futures_as_completed.py │ ├── futures_context_manager.py │ ├── futures_future_callback.py │ ├── futures_future_callback_cancel.py │ ├── futures_future_exception.py │ ├── futures_process_pool_broken.py │ ├── futures_process_pool_map.py │ ├── futures_thread_pool_map.py │ ├── futures_thread_pool_submit.py │ └── index.rst ├── conf.py ├── configparser │ ├── allow_no_value.ini │ ├── configparser_allow_no_value.py │ ├── configparser_custom_types.py │ ├── configparser_defaults.py │ ├── configparser_escape.py │ ├── configparser_example.py │ ├── configparser_extendedinterpolation.py │ ├── configparser_has_option.py │ ├── configparser_has_section.py │ ├── configparser_interpolation.py │ ├── configparser_interpolation_defaults.py │ ├── configparser_interpolation_error.py │ ├── configparser_interpolation_recursion.py │ ├── configparser_multiline.py │ ├── configparser_nointerpolation.py │ ├── configparser_populate.py │ ├── configparser_read.py │ ├── configparser_read_many.py │ ├── configparser_remove.py │ ├── configparser_structure.py │ ├── configparser_structure_dict.py │ ├── configparser_unicode.py │ ├── configparser_value_types.py │ ├── configparser_write.py │ ├── custom_types.ini │ ├── escape.ini │ ├── extended_interpolation.ini │ ├── index.rst │ ├── interpolation.ini │ ├── interpolation_defaults.ini │ ├── multiline.ini │ ├── multisection.ini │ ├── simple.ini │ ├── types.ini │ ├── unicode.ini │ └── with-defaults.ini ├── contextlib │ ├── contextlib_api.py │ ├── contextlib_api_error.py │ ├── contextlib_api_other_object.py │ ├── contextlib_closing.py │ ├── contextlib_context_managers.py │ ├── contextlib_contextmanager.py │ ├── contextlib_contextmanager_decorator.py │ ├── contextlib_decorator.py │ ├── contextlib_exitstack_callbacks.py │ ├── contextlib_exitstack_callbacks_decorator.py │ ├── contextlib_exitstack_callbacks_error.py │ ├── contextlib_exitstack_enter_context.py │ ├── contextlib_exitstack_enter_context_errors.py │ ├── contextlib_exitstack_pop_all.py │ ├── contextlib_file.py │ ├── contextlib_ignore_error.py │ ├── contextlib_redirect.py │ ├── contextlib_suppress.py │ └── index.rst ├── copy │ ├── copy_deep.py │ ├── copy_hooks.py │ ├── copy_recursion.py │ ├── copy_shallow.py │ └── index.rst ├── cryptographic.rst ├── csv │ ├── csv_dialect.py │ ├── csv_dialect_sniffer.py │ ├── csv_dialect_variations.py │ ├── csv_dictreader.py │ ├── csv_dictwriter.py │ ├── csv_list_dialects.py │ ├── csv_reader.py │ ├── csv_writer.py │ ├── csv_writer_quoted.py │ ├── index.rst │ ├── testdata.csv │ ├── testdata.pipes │ └── testlinebreak.csv ├── data_structures.rst ├── dates.rst ├── datetime │ ├── datetime_comparing.py │ ├── datetime_date.py │ ├── datetime_date_fromordinal.py │ ├── datetime_date_fromothervalue.py │ ├── datetime_date_isocalendar.py │ ├── datetime_date_math.py │ ├── datetime_date_minmax.py │ ├── datetime_date_replace.py │ ├── datetime_datetime.py │ ├── datetime_datetime_combine.py │ ├── datetime_datetime_math.py │ ├── datetime_datetime_minmax.py │ ├── datetime_datetime_strptime.py │ ├── datetime_format.py │ ├── datetime_time.py │ ├── datetime_time_minmax.py │ ├── datetime_time_resolution.py │ ├── datetime_timedelta.py │ ├── datetime_timedelta_math.py │ ├── datetime_timedelta_total_seconds.py │ ├── datetime_timezone.py │ └── index.rst ├── dbm │ ├── dbm_existing.py │ ├── dbm_intkeys.py │ ├── dbm_intvalue.py │ ├── dbm_new.py │ ├── dbm_whichdb.py │ └── index.rst ├── decimal │ ├── decimal_context_manager.py │ ├── decimal_create.py │ ├── decimal_format.py │ ├── decimal_getcontext.py │ ├── decimal_instance_context.py │ ├── decimal_log.py │ ├── decimal_operators.py │ ├── decimal_precision.py │ ├── decimal_rounding.py │ ├── decimal_special.py │ ├── decimal_thread_context.py │ ├── decimal_tuple.py │ └── index.rst ├── dev_tools.rst ├── difflib │ ├── difflib_context.py │ ├── difflib_data.py │ ├── difflib_differ.py │ ├── difflib_html.py │ ├── difflib_junk.py │ ├── difflib_ndiff.py │ ├── difflib_seq.py │ ├── difflib_unified.py │ ├── index.rst │ └── output.diff ├── dis │ ├── dis_class.py │ ├── dis_constant_folding.py │ ├── dis_eliminate_loop.py │ ├── dis_faster_loop.py │ ├── dis_fastest_loop.py │ ├── dis_function.py │ ├── dis_loops.py │ ├── dis_show_code.py │ ├── dis_simple.py │ ├── dis_slow_loop.py │ ├── dis_string.py │ ├── dis_test_loop.py │ ├── dis_traceback.py │ └── index.rst ├── doctest │ ├── doctest_blankline.py │ ├── doctest_blankline_fail.py │ ├── doctest_docstrings.py │ ├── doctest_ellipsis.py │ ├── doctest_extra_space.py │ ├── doctest_hashed_values.py │ ├── doctest_hashed_values_tests.py │ ├── doctest_in_help.py │ ├── doctest_in_help.txt │ ├── doctest_mutable_globals.py │ ├── doctest_ndiff.py │ ├── doctest_normalize_whitespace.py │ ├── doctest_private_tests.py │ ├── doctest_private_tests_external.py │ ├── doctest_simple.py │ ├── doctest_simple_with_docs.py │ ├── doctest_test_globals.py │ ├── doctest_testfile.py │ ├── doctest_testmod.py │ ├── doctest_testmod_other_module.py │ ├── doctest_tracebacks.py │ ├── doctest_tracebacks_no_body.py │ ├── doctest_unittest.py │ ├── doctest_unpredictable.py │ └── index.rst ├── email.rst ├── ensurepip │ └── index.rst ├── enum │ ├── enum_aliases.py │ ├── enum_comparison.py │ ├── enum_complex_values.py │ ├── enum_create.py │ ├── enum_intenum.py │ ├── enum_iterate.py │ ├── enum_programmatic_create.py │ ├── enum_programmatic_mapping.py │ ├── enum_tuple_values.py │ ├── enum_unique_enforce.py │ └── index.rst ├── file_access.rst ├── filecmp │ ├── filecmp_cmp.py │ ├── filecmp_cmpfiles.py │ ├── filecmp_dircmp_common.py │ ├── filecmp_dircmp_diff.py │ ├── filecmp_dircmp_list.py │ ├── filecmp_dircmp_list_filter.py │ ├── filecmp_dircmp_membership.py │ ├── filecmp_dircmp_report.py │ ├── filecmp_dircmp_report_full_closure.py │ ├── filecmp_dircmp_subdirs.py │ ├── filecmp_mkexamples.py │ └── index.rst ├── fileinput │ ├── etc_hosts │ ├── etc_hosts.txt │ ├── fileinput_change_subnet.py │ ├── fileinput_change_subnet_noisy.py │ ├── fileinput_example.py │ ├── fileinput_grep.py │ ├── index.rst │ ├── m3utorss.html │ └── sample_data.m3u ├── fnmatch │ ├── fnmatch_filter.py │ ├── fnmatch_fnmatch.py │ ├── fnmatch_fnmatchcase.py │ ├── fnmatch_translate.py │ └── index.rst ├── fractions │ ├── fractions_arithmetic.py │ ├── fractions_create_integers.py │ ├── fractions_create_strings.py │ ├── fractions_create_strings_floats.py │ ├── fractions_from_decimal.py │ ├── fractions_from_float.py │ ├── fractions_limit_denominator.py │ └── index.rst ├── frameworks.rst ├── functools │ ├── functools_callable.py │ ├── functools_cmp_to_key.py │ ├── functools_lru_cache.py │ ├── functools_lru_cache_arguments.py │ ├── functools_lru_cache_expire.py │ ├── functools_partial.py │ ├── functools_partialmethod.py │ ├── functools_reduce.py │ ├── functools_reduce_initializer.py │ ├── functools_reduce_short_sequences.py │ ├── functools_singledispatch.py │ ├── functools_singledispatch_mro.py │ ├── functools_total_ordering.py │ ├── functools_update_wrapper.py │ ├── functools_wraps.py │ └── index.rst ├── gc │ ├── gc_collect.py │ ├── gc_debug_leak.py │ ├── gc_debug_saveall.py │ ├── gc_debug_stats.py │ ├── gc_get_objects.py │ ├── gc_get_referents.py │ ├── gc_get_referents_cycles.py │ ├── gc_get_referrers.py │ ├── gc_get_threshold.py │ ├── gc_threshold.py │ └── index.rst ├── getopt │ ├── getopt_example.py │ ├── getopt_gnu.py │ ├── getopt_long.py │ ├── getopt_short.py │ └── index.rst ├── getpass │ ├── getpass_defaults.py │ ├── getpass_noterminal.py │ ├── getpass_prompt.py │ ├── getpass_stream.py │ └── index.rst ├── gettext │ ├── example.pot │ ├── gettext_app_builtin.py │ ├── gettext_example.py │ ├── gettext_example_corrected.py │ ├── gettext_find.py │ ├── gettext_module_global.py │ ├── gettext_plural.py │ ├── index.rst │ ├── locale │ │ ├── en_CA │ │ │ └── LC_MESSAGES │ │ │ │ ├── example.mo │ │ │ │ └── example.po │ │ └── en_US │ │ │ └── LC_MESSAGES │ │ │ ├── example.mo │ │ │ ├── example.po │ │ │ ├── plural.mo │ │ │ └── plural.po │ └── plural.pot ├── glob │ ├── glob_asterisk.py │ ├── glob_charrange.py │ ├── glob_escape.py │ ├── glob_extension.py │ ├── glob_maketestdata.py │ ├── glob_question.py │ ├── glob_subdir.py │ └── index.rst ├── grp │ ├── grp_getgrall.py │ ├── grp_getgrgid_fileowner.py │ ├── grp_getgrgid_process.py │ ├── grp_getgrnam.py │ ├── grp_groups_for_user.py │ └── index.rst ├── gzip │ ├── gzip_BytesIO.py │ ├── gzip_compresslevel.py │ ├── gzip_read.py │ ├── gzip_seek.py │ ├── gzip_write.py │ ├── gzip_writelines.py │ ├── index.rst │ └── lorem.txt ├── hashlib │ ├── hashlib_algorithms.py │ ├── hashlib_data.py │ ├── hashlib_md5.py │ ├── hashlib_new.py │ ├── hashlib_sha1.py │ ├── hashlib_update.py │ └── index.rst ├── heapq │ ├── heapq_extremes.py │ ├── heapq_heapdata.py │ ├── heapq_heapify.py │ ├── heapq_heappop.py │ ├── heapq_heappush.py │ ├── heapq_heapreplace.py │ ├── heapq_merge.py │ ├── heapq_showtree.py │ └── index.rst ├── hmac │ ├── hmac_base64.py │ ├── hmac_pickle.py │ ├── hmac_sha.py │ ├── hmac_simple.py │ ├── index.rst │ └── lorem.txt ├── http.cookies │ ├── http_cookies_Morsel.py │ ├── http_cookies_coded_value.py │ ├── http_cookies_js_output.py │ ├── http_cookies_parse.py │ ├── http_cookies_setheaders.py │ └── index.rst ├── http.server │ ├── http_server_GET.py │ ├── http_server_POST.py │ ├── http_server_errors.py │ ├── http_server_send_header.py │ ├── http_server_threads.py │ └── index.rst ├── i18n.rst ├── images │ └── replacement-character.png ├── imaplib │ ├── imaplib_append.py │ ├── imaplib_archive_read.py │ ├── imaplib_connect.py │ ├── imaplib_connect_fail.py │ ├── imaplib_delete_messages.py │ ├── imaplib_fetch_body.py │ ├── imaplib_fetch_raw.py │ ├── imaplib_fetch_rfc822.py │ ├── imaplib_fetch_separately.py │ ├── imaplib_list.py │ ├── imaplib_list_parse.py │ ├── imaplib_list_pattern.py │ ├── imaplib_list_subfolders.py │ ├── imaplib_search_all.py │ ├── imaplib_search_from.py │ ├── imaplib_search_subject.py │ ├── imaplib_select.py │ ├── imaplib_select_invalid.py │ ├── imaplib_status.py │ └── index.rst ├── importing.rst ├── importlib │ ├── example │ │ ├── __init__.py │ │ └── submodule.py │ ├── importlib_find_loader.py │ ├── importlib_import_module.py │ ├── importlib_import_module_error.py │ ├── importlib_reload.py │ ├── importlib_submodule.py │ ├── importlib_suffixes.py │ └── index.rst ├── index.rst ├── inspect │ ├── example.py │ ├── index.rst │ ├── inspect_currentframe.py │ ├── inspect_formatargspec.py │ ├── inspect_getclasstree.py │ ├── inspect_getclasstree_unique.py │ ├── inspect_getcomments_method.py │ ├── inspect_getcomments_module.py │ ├── inspect_getdoc.py │ ├── inspect_getmembers_class.py │ ├── inspect_getmembers_class_methods.py │ ├── inspect_getmembers_class_methods_b.py │ ├── inspect_getmembers_instance.py │ ├── inspect_getmembers_module.py │ ├── inspect_getmembers_module_class.py │ ├── inspect_getmro.py │ ├── inspect_getsource_class.py │ ├── inspect_getsource_method.py │ ├── inspect_getsourcelines_method.py │ ├── inspect_signature_bind.py │ ├── inspect_signature_bind_partial.py │ ├── inspect_signature_function.py │ └── inspect_stack.py ├── internet_protocols.rst ├── io │ ├── index.rst │ ├── io_bytesio.py │ ├── io_stringio.py │ └── io_textiowrapper.py ├── ipaddress │ ├── index.rst │ ├── ipaddress_addresses.py │ ├── ipaddress_interfaces.py │ ├── ipaddress_network_iterate.py │ ├── ipaddress_network_iterate_hosts.py │ ├── ipaddress_network_membership.py │ └── ipaddress_networks.py ├── itertools │ ├── index.rst │ ├── itertools_accumulate.py │ ├── itertools_accumulate_custom.py │ ├── itertools_chain.py │ ├── itertools_chain_from_iterable.py │ ├── itertools_combinations.py │ ├── itertools_combinations_with_replacement.py │ ├── itertools_compress.py │ ├── itertools_count.py │ ├── itertools_count_step.py │ ├── itertools_cycle.py │ ├── itertools_dropwhile.py │ ├── itertools_filter.py │ ├── itertools_filterfalse.py │ ├── itertools_groupby.py │ ├── itertools_groupby_seq.py │ ├── itertools_islice.py │ ├── itertools_map.py │ ├── itertools_permutations.py │ ├── itertools_product.py │ ├── itertools_product_ordering.py │ ├── itertools_product_repeat.py │ ├── itertools_repeat.py │ ├── itertools_repeat_map.py │ ├── itertools_repeat_zip.py │ ├── itertools_starmap.py │ ├── itertools_takewhile.py │ ├── itertools_tee.py │ ├── itertools_tee_error.py │ ├── itertools_zip.py │ └── itertools_zip_longest.py ├── json │ ├── example.json │ ├── index.rst │ ├── json_compact_encoding.py │ ├── json_decoder_object_hook.py │ ├── json_dump_default.py │ ├── json_dump_file.py │ ├── json_encoder_default.py │ ├── json_encoder_iterable.py │ ├── json_indent.py │ ├── json_load_file.py │ ├── json_load_object_hook.py │ ├── json_mixed_data.py │ ├── json_myobj.py │ ├── json_simple_types.py │ ├── json_simple_types_decode.py │ ├── json_skipkeys.py │ └── json_sort_keys.py ├── language.rst ├── linecache │ ├── index.rst │ ├── linecache_data.py │ ├── linecache_empty_line.py │ ├── linecache_example.py │ ├── linecache_getline.py │ ├── linecache_missing_file.py │ ├── linecache_out_of_range.py │ └── linecache_path_search.py ├── locale │ ├── index.rst │ ├── locale_atof.py │ ├── locale_currency.py │ ├── locale_date.py │ ├── locale_delocalize.py │ ├── locale_env.py │ ├── locale_grouping.py │ └── locale_list.py ├── logging │ ├── index.rst │ ├── logger_tree.dot │ ├── logger_tree_handler.dot │ ├── logger_tree_multihandler.dot │ ├── logging_capture_warnings.py │ ├── logging_file_example.py │ ├── logging_level_example.py │ ├── logging_modules_example.py │ └── logging_rotatingfile_example.py ├── mailbox │ ├── index.rst │ ├── mailbox_maildir_add_flag.py │ ├── mailbox_maildir_create.py │ ├── mailbox_maildir_folders.py │ ├── mailbox_maildir_read.py │ ├── mailbox_maildir_remove.py │ ├── mailbox_maildir_set_flags.py │ ├── mailbox_maildir_set_subdir.py │ ├── mailbox_mbox_create.py │ ├── mailbox_mbox_read.py │ └── mailbox_mbox_remove.py ├── math │ ├── index.rst │ ├── math_constants.py │ ├── math_copysign.py │ ├── math_degrees.py │ ├── math_distance_2_points.py │ ├── math_erf.py │ ├── math_erfc.py │ ├── math_exp.py │ ├── math_expm1.py │ ├── math_fabs.py │ ├── math_factorial.py │ ├── math_fmod.py │ ├── math_frexp.py │ ├── math_fsum.py │ ├── math_gamma.py │ ├── math_gcd.py │ ├── math_hyperbolic.py │ ├── math_hypot.py │ ├── math_integers.py │ ├── math_inverse_trig.py │ ├── math_isclose.py │ ├── math_isclose_abs_tol.py │ ├── math_isclose_inf.py │ ├── math_isfinite.py │ ├── math_isinf.py │ ├── math_isnan.py │ ├── math_ldexp.py │ ├── math_lgamma.py │ ├── math_log.py │ ├── math_log10.py │ ├── math_log1p.py │ ├── math_log2.py │ ├── math_modf.py │ ├── math_overflow.py │ ├── math_pow.py │ ├── math_radians.py │ ├── math_sqrt.py │ └── math_trig.py ├── mmap │ ├── index.rst │ ├── lorem.txt │ ├── mmap_read.py │ ├── mmap_regex.py │ ├── mmap_write_copy.py │ └── mmap_write_slice.py ├── multiprocessing │ ├── basics.rst │ ├── communication.rst │ ├── index.rst │ ├── mapreduce.rst │ ├── multiprocessing_condition.py │ ├── multiprocessing_daemon.py │ ├── multiprocessing_daemon_join.py │ ├── multiprocessing_daemon_join_timeout.py │ ├── multiprocessing_event.py │ ├── multiprocessing_exitcode.py │ ├── multiprocessing_get_logger.py │ ├── multiprocessing_import_main.py │ ├── multiprocessing_import_worker.py │ ├── multiprocessing_lock.py │ ├── multiprocessing_log_to_stderr.py │ ├── multiprocessing_manager_dict.py │ ├── multiprocessing_mapreduce.py │ ├── multiprocessing_names.py │ ├── multiprocessing_namespaces.py │ ├── multiprocessing_namespaces_mutable.py │ ├── multiprocessing_pool.py │ ├── multiprocessing_pool_maxtasksperchild.py │ ├── multiprocessing_producer_consumer.py │ ├── multiprocessing_queue.py │ ├── multiprocessing_semaphore.py │ ├── multiprocessing_simple.py │ ├── multiprocessing_simpleargs.py │ ├── multiprocessing_subclass.py │ ├── multiprocessing_terminate.py │ └── multiprocessing_wordcount.py ├── networking.rst ├── numeric.rst ├── operator │ ├── index.rst │ ├── operator_attrgetter.py │ ├── operator_boolean.py │ ├── operator_classes.py │ ├── operator_comparisons.py │ ├── operator_inplace.py │ ├── operator_itemgetter.py │ ├── operator_math.py │ └── operator_sequences.py ├── os.path │ ├── index.rst │ ├── ospath_abspath.py │ ├── ospath_basename.py │ ├── ospath_commonpath.py │ ├── ospath_commonprefix.py │ ├── ospath_dirname.py │ ├── ospath_expanduser.py │ ├── ospath_expandvars.py │ ├── ospath_join.py │ ├── ospath_normpath.py │ ├── ospath_properties.py │ ├── ospath_split.py │ ├── ospath_splitext.py │ └── ospath_tests.py ├── os │ ├── index.rst │ ├── os_access.py │ ├── os_cwd_example.py │ ├── os_directories.py │ ├── os_environ_example.py │ ├── os_exec_example.py │ ├── os_fork_example.py │ ├── os_kill_example.py │ ├── os_listdir.py │ ├── os_process_id_example.py │ ├── os_process_user_example.py │ ├── os_rename_replace.py │ ├── os_scandir.py │ ├── os_spawn_example.py │ ├── os_stat.py │ ├── os_stat_chmod.py │ ├── os_strerror.py │ ├── os_symlinks.py │ ├── os_system_background.py │ ├── os_system_example.py │ ├── os_system_shell.py │ ├── os_wait_example.py │ ├── os_waitpid_example.py │ └── os_walk.py ├── pathlib │ ├── index.rst │ ├── pathlib_chmod.py │ ├── pathlib_convenience.py │ ├── pathlib_from_existing.py │ ├── pathlib_glob.py │ ├── pathlib_iterdir.py │ ├── pathlib_joinpath.py │ ├── pathlib_mkdir.py │ ├── pathlib_name.py │ ├── pathlib_operator.py │ ├── pathlib_ownership.py │ ├── pathlib_parents.py │ ├── pathlib_parts.py │ ├── pathlib_read_write.py │ ├── pathlib_resolve.py │ ├── pathlib_rglob.py │ ├── pathlib_rmdir.py │ ├── pathlib_stat.py │ ├── pathlib_symlink_to.py │ ├── pathlib_touch.py │ ├── pathlib_types.py │ └── pathlib_unlink.py ├── pdb │ ├── index.rst │ ├── lorem.txt │ ├── pdb_break.py │ ├── pdb_break_remote.py │ ├── pdb_function_arguments.py │ ├── pdb_interact.py │ ├── pdb_jump.py │ ├── pdb_next.py │ ├── pdb_no_jump.py │ ├── pdb_post_mortem.py │ ├── pdb_pp.py │ ├── pdb_run.py │ ├── pdb_script.py │ ├── pdb_set_trace.py │ └── pdb_step.py ├── persistence.rst ├── pickle │ ├── index.rst │ ├── pickle_cycle.py │ ├── pickle_dump_to_file_1.py │ ├── pickle_load_from_file_1.py │ ├── pickle_load_from_file_2.py │ ├── pickle_state.py │ ├── pickle_stream.py │ ├── pickle_string.py │ └── pickle_unpickle.py ├── pkgutil │ ├── __init__.py │ ├── demopkg1 │ │ ├── __init__.py │ │ └── shared.py │ ├── demopkg2 │ │ ├── __init__.py │ │ └── overloaded.py │ ├── develop │ │ ├── demopkg2 │ │ │ ├── __init__.py │ │ │ └── overloaded.py │ │ └── nested │ │ │ ├── __init__.py │ │ │ ├── second │ │ │ ├── __init__.py │ │ │ └── deep.py │ │ │ └── shallow.py │ ├── extension │ │ ├── __init__.py │ │ └── demopkg1 │ │ │ ├── __init__.py │ │ │ └── not_shared.py │ ├── index.rst │ ├── nested │ │ ├── __init__.py │ │ ├── second │ │ │ ├── __init__.py │ │ │ └── deep.py │ │ └── shallow.py │ ├── os_one │ │ ├── demopkg1.pkg │ │ └── demopkg1 │ │ │ ├── __init__.py │ │ │ └── not_shared.py │ ├── os_two │ │ ├── demopkg1.pkg │ │ └── demopkg1 │ │ │ ├── __init__.py │ │ │ └── not_shared.py │ ├── pkgutil_devel.py │ ├── pkgutil_extend_path.py │ ├── pkgutil_extend_path.sh │ ├── pkgutil_get_data.py │ ├── pkgutil_get_data_zip.py │ ├── pkgutil_nested.py │ ├── pkgutil_os_specific.py │ ├── pkgwithdata │ │ ├── __init__.py │ │ └── templates │ │ │ └── base.html │ ├── run_dev.sh │ └── with_os.sh ├── platform │ ├── index.rst │ ├── platform_architecture.py │ ├── platform_os_info.py │ ├── platform_platform.py │ └── platform_python.py ├── porting_notes.rst ├── pprint │ ├── index.rst │ ├── pprint_arbitrary_object.py │ ├── pprint_compact.py │ ├── pprint_data.py │ ├── pprint_depth.py │ ├── pprint_pformat.py │ ├── pprint_pprint.py │ ├── pprint_recursion.py │ └── pprint_width.py ├── profile │ ├── index.rst │ ├── profile_fibonacci_memoized.py │ ├── profile_fibonacci_raw.py │ ├── profile_runctx.py │ ├── profile_stats.py │ ├── profile_stats_0.stats │ ├── profile_stats_1.stats │ ├── profile_stats_2.stats │ ├── profile_stats_3.stats │ ├── profile_stats_4.stats │ ├── profile_stats_callers.py │ └── profile_stats_restricted.py ├── pwd │ ├── index.rst │ ├── pwd_getpwall.py │ ├── pwd_getpwnam.py │ ├── pwd_getpwuid_fileowner.py │ └── pwd_getpwuid_process.py ├── pyclbr │ ├── index.rst │ ├── pyclbr_example.py │ ├── pyclbr_readmodule.py │ └── pyclbr_readmodule_ex.py ├── pydoc │ └── index.rst ├── queue │ ├── fetch_podcasts.py │ ├── index.rst │ ├── queue_fifo.py │ ├── queue_lifo.py │ └── queue_priority.py ├── random │ ├── index.rst │ ├── random_choice.py │ ├── random_randint.py │ ├── random_random.py │ ├── random_random_class.py │ ├── random_randrange.py │ ├── random_sample.py │ ├── random_seed.py │ ├── random_shuffle.py │ ├── random_state.py │ ├── random_system_random.py │ └── random_uniform.py ├── re │ ├── index.rst │ ├── re_anchoring.py │ ├── re_charset.py │ ├── re_charset_dot.py │ ├── re_charset_exclude.py │ ├── re_charset_ranges.py │ ├── re_email_compact.py │ ├── re_email_verbose.py │ ├── re_email_with_name.py │ ├── re_escape_codes.py │ ├── re_escape_escapes.py │ ├── re_findall.py │ ├── re_finditer.py │ ├── re_flags_ascii.py │ ├── re_flags_dotall.py │ ├── re_flags_embedded.py │ ├── re_flags_ignorecase.py │ ├── re_flags_multiline.py │ ├── re_fullmatch.py │ ├── re_groups.py │ ├── re_groups_alternative.py │ ├── re_groups_individual.py │ ├── re_groups_match.py │ ├── re_groups_named.py │ ├── re_groups_nested.py │ ├── re_groups_noncapturing.py │ ├── re_id.py │ ├── re_look_ahead.py │ ├── re_look_behind.py │ ├── re_match.py │ ├── re_negative_look_ahead.py │ ├── re_negative_look_behind.py │ ├── re_paragraphs_findall.py │ ├── re_refer_to_group.py │ ├── re_refer_to_named_group.py │ ├── re_repetition.py │ ├── re_repetition_non_greedy.py │ ├── re_search_substring.py │ ├── re_simple.py │ ├── re_simple_compiled.py │ ├── re_simple_match.py │ ├── re_split.py │ ├── re_split_groups.py │ ├── re_sub.py │ ├── re_sub_count.py │ ├── re_sub_named_groups.py │ ├── re_subn.py │ ├── re_test_patterns.py │ └── re_test_patterns_groups.py ├── readline │ ├── index.rst │ ├── myreadline.rc │ ├── readline_buffer.py │ ├── readline_completer.py │ ├── readline_history.py │ ├── readline_hooks.py │ ├── readline_parse_and_bind.py │ └── readline_read_init_file.py ├── resource │ ├── index.rst │ ├── resource_getrlimit.py │ ├── resource_getrusage.py │ ├── resource_setrlimit_cpu.py │ └── resource_setrlimit_nofile.py ├── runtime_services.rst ├── sched │ ├── index.rst │ ├── sched_basic.py │ ├── sched_cancel.py │ ├── sched_overlap.py │ └── sched_priority.py ├── select │ ├── index.rst │ ├── select_echo_multiclient.py │ ├── select_echo_server.py │ ├── select_echo_server_timeout.py │ ├── select_echo_slow_client.py │ └── select_poll_echo_server.py ├── selectors │ ├── index.rst │ ├── selectors_echo_client.py │ └── selectors_echo_server.py ├── shelve │ ├── index.rst │ ├── shelve_create.py │ ├── shelve_existing.py │ ├── shelve_readonly.py │ ├── shelve_withoutwriteback.py │ └── shelve_writeback.py ├── shlex │ ├── apostrophe.txt │ ├── comments.txt │ ├── index.rst │ ├── quotes.txt │ ├── shlex_errors.py │ ├── shlex_example.py │ ├── shlex_posix.py │ ├── shlex_quote.py │ ├── shlex_source.py │ ├── shlex_split.py │ ├── shlex_table.py │ └── shlex_whitespace.py ├── shutil │ ├── index.rst │ ├── shutil_copy.py │ ├── shutil_copy2.py │ ├── shutil_copyfile.py │ ├── shutil_copyfileobj.py │ ├── shutil_copymode.py │ ├── shutil_copystat.py │ ├── shutil_copytree.py │ ├── shutil_copytree_verbose.py │ ├── shutil_disk_usage.py │ ├── shutil_get_archive_formats.py │ ├── shutil_get_unpack_formats.py │ ├── shutil_make_archive.py │ ├── shutil_move.py │ ├── shutil_rmtree.py │ ├── shutil_unpack_archive.py │ ├── shutil_which.py │ └── shutil_which_regular_file.py ├── signal │ ├── index.rst │ ├── signal_alarm.py │ ├── signal_getsignal.py │ ├── signal_ignore.py │ ├── signal_signal.py │ ├── signal_threads.py │ └── signal_threads_alarm.py ├── site │ ├── index.rst │ ├── multiple_pth │ │ ├── a.pth │ │ ├── b.pth │ │ ├── from_a │ │ │ └── mymodule.py │ │ └── from_b │ │ │ └── mymodule.py │ ├── site_addsitedir.py │ ├── site_enable_user_site.py │ ├── site_import_path.py │ ├── site_user_base.py │ ├── with_modules │ │ └── mymodule.py │ ├── with_pth │ │ ├── pymotw.pth │ │ └── subdir │ │ │ └── mymodule.py │ ├── with_sitecustomize │ │ ├── site_sitecustomize.py │ │ └── sitecustomize.py │ └── with_usercustomize │ │ ├── site_usercustomize.py │ │ └── usercustomize.py ├── smtpd │ ├── index.rst │ ├── smtpd_custom.py │ ├── smtpd_debug.py │ ├── smtpd_proxy.py │ └── smtpd_senddata.py ├── smtplib │ ├── index.rst │ ├── smtplib_authenticated.py │ ├── smtplib_sendmail.py │ └── smtplib_verify.py ├── socket │ ├── addressing.rst │ ├── binary.rst │ ├── index.rst │ ├── multicast.rst │ ├── nonblocking.rst │ ├── socket_address_packing.py │ ├── socket_binary_client.py │ ├── socket_binary_server.py │ ├── socket_echo_client.py │ ├── socket_echo_client_dgram.py │ ├── socket_echo_client_easy.py │ ├── socket_echo_client_explicit.py │ ├── socket_echo_client_uds.py │ ├── socket_echo_server.py │ ├── socket_echo_server_any.py │ ├── socket_echo_server_dgram.py │ ├── socket_echo_server_explicit.py │ ├── socket_echo_server_uds.py │ ├── socket_getaddrinfo.py │ ├── socket_getaddrinfo_extra_args.py │ ├── socket_getfqdn.py │ ├── socket_gethostbyaddr.py │ ├── socket_gethostbyname.py │ ├── socket_gethostbyname_ex.py │ ├── socket_gethostname.py │ ├── socket_getprotobyname.py │ ├── socket_getservbyname.py │ ├── socket_getservbyport.py │ ├── socket_ipv6_address_packing.py │ ├── socket_multicast_receiver.py │ ├── socket_multicast_sender.py │ ├── socket_socketpair.py │ ├── tcp.rst │ ├── udp.rst │ └── uds.rst ├── socketserver │ ├── index.rst │ ├── socketserver_echo.py │ ├── socketserver_echo_simple.py │ ├── socketserver_forking.py │ └── socketserver_threaded.py ├── spelling_wordlist.txt ├── sqlite3 │ ├── index.rst │ ├── sqlite3_argument_named.py │ ├── sqlite3_argument_positional.py │ ├── sqlite3_argument_update.py │ ├── sqlite3_autocommit.py │ ├── sqlite3_create_aggregate.py │ ├── sqlite3_create_function.py │ ├── sqlite3_create_schema.py │ ├── sqlite3_createdb.py │ ├── sqlite3_cursor_description.py │ ├── sqlite3_custom_type.py │ ├── sqlite3_custom_type_column.py │ ├── sqlite3_date_types.py │ ├── sqlite3_isolation_levels.py │ ├── sqlite3_iterdump.py │ ├── sqlite3_load_csv.py │ ├── sqlite3_load_extension_fts3.py │ ├── sqlite3_memory.py │ ├── sqlite3_regex.py │ ├── sqlite3_row_factory.py │ ├── sqlite3_select_tasks.py │ ├── sqlite3_select_variations.py │ ├── sqlite3_set_authorizer.py │ ├── sqlite3_threading.py │ ├── sqlite3_transaction_commit.py │ ├── sqlite3_transaction_rollback.py │ ├── tasks.csv │ └── todo_schema.sql ├── statistics │ ├── index.rst │ ├── statistics_mean.py │ ├── statistics_median.py │ ├── statistics_median_grouped.py │ ├── statistics_mode.py │ └── statistics_variance.py ├── string │ ├── index.rst │ ├── string_capwords.py │ ├── string_constants.py │ ├── string_template.py │ ├── string_template_advanced.py │ ├── string_template_defaultpattern.py │ ├── string_template_missing.py │ └── string_template_newsyntax.py ├── struct │ ├── index.rst │ ├── struct_buffers.py │ ├── struct_endianness.py │ ├── struct_pack.py │ └── struct_unpack.py ├── subprocess │ ├── index.rst │ ├── interaction.py │ ├── repeater.py │ ├── signal_child.py │ ├── signal_parent.py │ ├── subprocess_check_output_error_trap_output.py │ ├── subprocess_os_system.py │ ├── subprocess_pipes.py │ ├── subprocess_popen2.py │ ├── subprocess_popen3.py │ ├── subprocess_popen4.py │ ├── subprocess_popen_read.py │ ├── subprocess_popen_write.py │ ├── subprocess_run_check.py │ ├── subprocess_run_output.py │ ├── subprocess_run_output_error.py │ ├── subprocess_run_output_error_suppress.py │ ├── subprocess_run_output_error_trap.py │ ├── subprocess_shell_variables.py │ ├── subprocess_signal_parent_shell.py │ └── subprocess_signal_setpgrp.py ├── sys │ ├── exceptions.rst │ ├── imports.rst │ ├── index.rst │ ├── interpreter.rst │ ├── limits.rst │ ├── package_dir_a │ │ ├── __init__.py │ │ └── example.py │ ├── package_dir_b │ │ ├── __init__.py │ │ └── example.py │ ├── runtime.rst │ ├── sys_argv.py │ ├── sys_builtins.py │ ├── sys_byteorder.py │ ├── sys_current_frames.py │ ├── sys_displayhook.py │ ├── sys_exc_info.py │ ├── sys_excepthook.py │ ├── sys_exit.py │ ├── sys_flags.py │ ├── sys_float_info.py │ ├── sys_getrefcount.py │ ├── sys_getsizeof.py │ ├── sys_getsizeof_custom.py │ ├── sys_getsizeof_object.py │ ├── sys_implementation.py │ ├── sys_int_info.py │ ├── sys_locations.py │ ├── sys_maximums.py │ ├── sys_meta_path.py │ ├── sys_modules.py │ ├── sys_path_hooks_noisy.py │ ├── sys_path_importer_cache.py │ ├── sys_path_modify.py │ ├── sys_path_show.py │ ├── sys_platform.py │ ├── sys_ps1.py │ ├── sys_recursionlimit.py │ ├── sys_settrace_call.py │ ├── sys_settrace_exception.py │ ├── sys_settrace_line.py │ ├── sys_settrace_return.py │ ├── sys_shelve_importer.py │ ├── sys_shelve_importer_create.py │ ├── sys_shelve_importer_get_data.py │ ├── sys_shelve_importer_missing.py │ ├── sys_shelve_importer_module.py │ ├── sys_shelve_importer_package.py │ ├── sys_shelve_importer_reload.py │ ├── sys_stdio.py │ ├── sys_switchinterval.py │ ├── sys_unicode.py │ ├── sys_version_values.py │ ├── threads.rst │ └── tracing.rst ├── sysconfig │ ├── index.rst │ ├── sysconfig_get_config_var.py │ ├── sysconfig_get_config_vars.py │ ├── sysconfig_get_config_vars_by_name.py │ ├── sysconfig_get_path.py │ ├── sysconfig_get_path_names.py │ ├── sysconfig_get_paths.py │ ├── sysconfig_get_platform.py │ ├── sysconfig_get_python_version.py │ └── sysconfig_get_scheme_names.py ├── tabnanny │ ├── index.rst │ └── tabnanny_check.py ├── tarfile │ ├── README.txt │ ├── bad_example.tar │ ├── example.tar │ ├── index.rst │ ├── tarfile_add.py │ ├── tarfile_addfile.py │ ├── tarfile_addfile_string.py │ ├── tarfile_append.py │ ├── tarfile_compression.py │ ├── tarfile_extract.py │ ├── tarfile_extractall.py │ ├── tarfile_extractall_members.py │ ├── tarfile_extractfile.py │ ├── tarfile_getmember.py │ ├── tarfile_getmembers.py │ ├── tarfile_getnames.py │ └── tarfile_is_tarfile.py ├── tempfile │ ├── index.rst │ ├── tempfile_NamedTemporaryFile.py │ ├── tempfile_NamedTemporaryFile_args.py │ ├── tempfile_SpooledTemporaryFile.py │ ├── tempfile_SpooledTemporaryFile_explicit.py │ ├── tempfile_TemporaryDirectory.py │ ├── tempfile_TemporaryFile.py │ ├── tempfile_TemporaryFile_binary.py │ ├── tempfile_TemporaryFile_text.py │ ├── tempfile_settings.py │ └── tempfile_tempdir.py ├── text.rst ├── textwrap │ ├── index.rst │ ├── textwrap_dedent.py │ ├── textwrap_example.py │ ├── textwrap_fill.py │ ├── textwrap_fill_width.py │ ├── textwrap_hanging_indent.py │ ├── textwrap_indent.py │ ├── textwrap_indent_predicate.py │ └── textwrap_shorten.py ├── third_party.rst ├── threading │ ├── index.rst │ ├── threading_barrier.py │ ├── threading_barrier_abort.py │ ├── threading_condition.py │ ├── threading_daemon.py │ ├── threading_daemon_join.py │ ├── threading_daemon_join_timeout.py │ ├── threading_enumerate.py │ ├── threading_event.py │ ├── threading_local.py │ ├── threading_local_defaults.py │ ├── threading_lock.py │ ├── threading_lock_noblock.py │ ├── threading_lock_reacquire.py │ ├── threading_lock_with.py │ ├── threading_names.py │ ├── threading_names_log.py │ ├── threading_rlock.py │ ├── threading_semaphore.py │ ├── threading_simple.py │ ├── threading_simpleargs.py │ ├── threading_subclass.py │ ├── threading_subclass_args.py │ └── threading_timer.py ├── time │ ├── index.rst │ ├── time_clock_sleep.py │ ├── time_ctime.py │ ├── time_get_clock_info.py │ ├── time_monotonic.py │ ├── time_perf_counter.py │ ├── time_process_time.py │ ├── time_strptime.py │ ├── time_struct.py │ ├── time_time.py │ └── time_timezone.py ├── timeit │ ├── index.rst │ ├── timeit_dictionary.py │ ├── timeit_example.py │ └── timeit_setitem.py ├── trace │ ├── coverdir1 │ │ └── trace_example.recurse.cover │ ├── coverdir2 │ │ └── trace_example.recurse.cover │ ├── index.rst │ ├── trace_CoverageResults.py │ ├── trace_example │ │ ├── __init__.py │ │ ├── main.cover │ │ ├── main.py │ │ ├── recurse.cover │ │ └── recurse.py │ ├── trace_report.py │ ├── trace_run.py │ └── trace_runfunc.py ├── traceback │ ├── index.rst │ ├── traceback_example.py │ ├── traceback_extract_stack.py │ ├── traceback_extract_tb.py │ ├── traceback_format_exc.py │ ├── traceback_format_exception.py │ ├── traceback_format_stack.py │ ├── traceback_framesummary.py │ ├── traceback_print_exc.py │ ├── traceback_print_exception.py │ ├── traceback_print_stack.py │ ├── traceback_print_tb.py │ ├── traceback_stacksummary.py │ └── traceback_tracebackexception.py ├── unittest │ ├── index.rst │ ├── unittest_addcleanup.py │ ├── unittest_almostequal.py │ ├── unittest_equality.py │ ├── unittest_equality_container.py │ ├── unittest_exception.py │ ├── unittest_expectedfailure.py │ ├── unittest_failwithmessage.py │ ├── unittest_fixtures.py │ ├── unittest_in.py │ ├── unittest_outcomes.py │ ├── unittest_simple.py │ ├── unittest_skip.py │ ├── unittest_subtest.py │ └── unittest_truth.py ├── unix.rst ├── urllib.parse │ ├── index.rst │ ├── urllib_parse_geturl.py │ ├── urllib_parse_parse_qs.py │ ├── urllib_parse_quote.py │ ├── urllib_parse_unquote.py │ ├── urllib_parse_urldefrag.py │ ├── urllib_parse_urlencode.py │ ├── urllib_parse_urlencode_doseq.py │ ├── urllib_parse_urljoin.py │ ├── urllib_parse_urljoin_with_path.py │ ├── urllib_parse_urlparse.py │ ├── urllib_parse_urlparseattrs.py │ ├── urllib_parse_urlsplit.py │ ├── urllib_parse_urlunparse.py │ └── urllib_parse_urlunparseextra.py ├── urllib.request │ ├── index.rst │ ├── urllib_request_http_get_args.py │ ├── urllib_request_nfs_handler.py │ ├── urllib_request_request_header.py │ ├── urllib_request_request_post.py │ ├── urllib_request_upload_files.py │ ├── urllib_request_urlopen.py │ ├── urllib_request_urlopen_iterator.py │ └── urllib_request_urlopen_post.py ├── urllib.robotparser │ ├── index.rst │ ├── robots.txt │ ├── urllib_robotparser_longlived.py │ └── urllib_robotparser_simple.py ├── uuid │ ├── index.rst │ ├── uuid_getnode.py │ ├── uuid_uuid1.py │ ├── uuid_uuid1_othermac.py │ ├── uuid_uuid1_repeat.py │ ├── uuid_uuid3_repeat.py │ ├── uuid_uuid3_uuid5.py │ ├── uuid_uuid4.py │ └── uuid_uuid_objects.py ├── venv │ └── index.rst ├── warnings │ ├── index.rst │ ├── warnings_filter.py │ ├── warnings_filterwarnings_lineno.py │ ├── warnings_filterwarnings_message.py │ ├── warnings_filterwarnings_module.py │ ├── warnings_formatwarning.py │ ├── warnings_once.py │ ├── warnings_repeated.py │ ├── warnings_showwarning.py │ ├── warnings_warn.py │ ├── warnings_warn_raise.py │ └── warnings_warn_stacklevel.py ├── weakref │ ├── index.rst │ ├── weakref_finalize.py │ ├── weakref_finalize_atexit.py │ ├── weakref_finalize_reference.py │ ├── weakref_finalize_reference_method.py │ ├── weakref_proxy.py │ ├── weakref_ref.py │ ├── weakref_ref_callback.py │ └── weakref_valuedict.py ├── webbrowser │ ├── index.rst │ ├── webbrowser_get.py │ ├── webbrowser_open.py │ ├── webbrowser_open_new.py │ └── webbrowser_open_new_tab.py ├── xml.etree.ElementTree │ ├── ElementTree_XML.py │ ├── ElementTree_XMLID.py │ ├── ElementTree_create.py │ ├── ElementTree_create_pretty.py │ ├── ElementTree_csv_to_xml.py │ ├── ElementTree_dump_opml.py │ ├── ElementTree_entity_references.py │ ├── ElementTree_extend.py │ ├── ElementTree_extend_node.py │ ├── ElementTree_extend_node_copy.py │ ├── ElementTree_find_feeds_by_structure.py │ ├── ElementTree_find_feeds_by_tag.py │ ├── ElementTree_node_attributes.py │ ├── ElementTree_node_text.py │ ├── ElementTree_parse_opml.py │ ├── ElementTree_podcast_csv_treebuilder.py │ ├── ElementTree_pretty.py │ ├── ElementTree_show_all_events.py │ ├── ElementTree_show_feed_urls.py │ ├── ElementTree_write.py │ ├── ElementTree_write_method.py │ ├── ElementTree_write_podcast_csv.py │ ├── create.rst │ ├── data.xml │ ├── index.rst │ ├── parse.rst │ ├── podcasts.csv │ └── podcasts.opml ├── xmlrpc.client │ ├── index.rst │ ├── xmlrpc_Binary.py │ ├── xmlrpc_Binary_pickle.py │ ├── xmlrpc_MultiCall.py │ ├── xmlrpc_MultiCall_exception.py │ ├── xmlrpc_ServerProxy.py │ ├── xmlrpc_ServerProxy_allow_none.py │ ├── xmlrpc_ServerProxy_encoding.py │ ├── xmlrpc_ServerProxy_use_datetime.py │ ├── xmlrpc_ServerProxy_verbose.py │ ├── xmlrpc_exception.py │ ├── xmlrpc_server.py │ ├── xmlrpc_types.py │ ├── xmlrpc_types_nested.py │ └── xmlrpc_types_object.py ├── xmlrpc.server │ ├── index.rst │ ├── xmlrpc_alternate_name.py │ ├── xmlrpc_alternate_name_client.py │ ├── xmlrpc_arbitrary_name.py │ ├── xmlrpc_arbitrary_name_client.py │ ├── xmlrpc_dotted_name.py │ ├── xmlrpc_dotted_name_client.py │ ├── xmlrpc_function.py │ ├── xmlrpc_function_client.py │ ├── xmlrpc_instance.py │ ├── xmlrpc_instance_client.py │ ├── xmlrpc_instance_dotted_names.py │ ├── xmlrpc_instance_dotted_names_client.py │ ├── xmlrpc_instance_with_prefix.py │ ├── xmlrpc_instance_with_prefix_client.py │ ├── xmlrpc_introspection.py │ └── xmlrpc_introspection_client.py ├── zipfile │ ├── README.txt │ ├── bad_example.zip │ ├── example.zip │ ├── index.rst │ ├── zipfile_append.py │ ├── zipfile_getinfo.py │ ├── zipfile_infolist.py │ ├── zipfile_is_zipfile.py │ ├── zipfile_namelist.py │ ├── zipfile_printdir.py │ ├── zipfile_pyzipfile.py │ ├── zipfile_read.py │ ├── zipfile_write.py │ ├── zipfile_write_arcname.py │ ├── zipfile_write_compression.py │ ├── zipfile_writestr.py │ └── zipfile_writestr_zipinfo.py ├── zipimport │ ├── __init__.py │ ├── example_package │ │ ├── README.txt │ │ └── __init__.py │ ├── index.rst │ ├── zipimport_find_module.py │ ├── zipimport_get_code.py │ ├── zipimport_get_data.py │ ├── zipimport_get_data_nozip.py │ ├── zipimport_get_data_zip.py │ ├── zipimport_get_source.py │ ├── zipimport_is_package.py │ ├── zipimport_load_module.py │ └── zipimport_make_example.py └── zlib │ ├── MANIFEST.in │ ├── index.rst │ ├── lorem.txt │ ├── zlib_checksum_tests.py │ ├── zlib_checksums.py │ ├── zlib_compresslevel.py │ ├── zlib_incremental.py │ ├── zlib_lengths.py │ ├── zlib_memory.py │ ├── zlib_mixed.py │ └── zlib_server.py └── tox.ini /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length = 65 3 | ignore = E265,E302,E741,F401,E402,F403,F405 4 | show-source = True 5 | exclude = source/_exts/,source/cgitb/,source/pdb/ -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include source *.py *.txt *.pth *.ini 2 | recursive-include source/csv *.csv *.pipes 3 | include source/difflib/output.diff 4 | prune source/_exts -------------------------------------------------------------------------------- /bin/strip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import fileinput 4 | import re 5 | 6 | straight_pat = re.compile(':(class|data|const|command):`(.*?)`') 7 | func_pat = re.compile(':(func|meth):`(.*?)(\(\))?`') 8 | 9 | for line in fileinput.input(inplace=True): 10 | line = line.rstrip('\n') 11 | line = straight_pat.sub(r'``\2``', line) 12 | line = func_pat.sub(r'``\2()``', line) 13 | print(line) 14 | -------------------------------------------------------------------------------- /bin/update.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source ~/Envs/pymotw34/bin/activate 4 | 5 | paver update $@ 6 | -------------------------------------------------------------------------------- /homebrew.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Instructions for installing external dependencies with homebrew. 4 | # 5 | 6 | PKGS=" 7 | gettext 8 | graphviz 9 | lynx 10 | readline 11 | " 12 | 13 | for pkg in $PKGS; do 14 | brew install $pkg 15 | done 16 | -------------------------------------------------------------------------------- /pearson/newphstyle.sty: -------------------------------------------------------------------------------- 1 | \NeedsTeXFormat{LaTeX2e} 2 | \@obsoletefile{newphstyle.cls}{newphstyle.sty} 3 | \LoadClass{newphstyle} \endinput 4 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/setup.cfg -------------------------------------------------------------------------------- /source/_static/.placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_static/.placeholder -------------------------------------------------------------------------------- /source/_themes/pymotw/localtoc.html: -------------------------------------------------------------------------------- 1 | {{ toc }} 2 | -------------------------------------------------------------------------------- /source/_themes/pymotw/page.html: -------------------------------------------------------------------------------- 1 | {%- extends "layout.html" %} 2 | {% block body %} 3 | {{ body }} 4 | {% endblock %} 5 | -------------------------------------------------------------------------------- /source/_themes/pymotw/sidebar_book.html: -------------------------------------------------------------------------------- 1 |
2 | -------------------------------------------------------------------------------- /source/_themes/pymotw/sidebar_examples.html: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /source/_themes/pymotw/sidebar_lastupdated.html: -------------------------------------------------------------------------------- 1 | {%- if last_updated -%} 2 | 5 | {% else %} 6 | 9 | {%- endif -%} 10 | -------------------------------------------------------------------------------- /source/_themes/pymotw/sidebar_toc.html: -------------------------------------------------------------------------------- 1 | {% if toc_menu %} 2 | 10 | {% endif %} 11 | -------------------------------------------------------------------------------- /source/_themes/pymotw/static/book-cover-image-med.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/book-cover-image-med.jpg -------------------------------------------------------------------------------- /source/_themes/pymotw/static/book-cover-image-small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/book-cover-image-small.jpg -------------------------------------------------------------------------------- /source/_themes/pymotw/static/cc-30-by-nc-sa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/cc-30-by-nc-sa.png -------------------------------------------------------------------------------- /source/_themes/pymotw/static/dh-green-hosting.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/dh-green-hosting.gif -------------------------------------------------------------------------------- /source/_themes/pymotw/static/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/favicon.png -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/font/FontAwesome.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/font-awesome/font/FontAwesome.otf -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/font/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/font-awesome/font/fontawesome-webfont.eot -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/font/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/font-awesome/font/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/font/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/font-awesome/font/fontawesome-webfont.woff -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/fonts/FontAwesome.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/font-awesome/fonts/FontAwesome.otf -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/fonts/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/font-awesome/fonts/fontawesome-webfont.eot -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/fonts/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/font-awesome/fonts/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/fonts/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/font-awesome/fonts/fontawesome-webfont.woff -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/less/core.less: -------------------------------------------------------------------------------- 1 | // Base Class Definition 2 | // ------------------------- 3 | 4 | .@{fa-css-prefix} { 5 | display: inline-block; 6 | font-family: FontAwesome; 7 | font-style: normal; 8 | font-weight: normal; 9 | line-height: 1; 10 | -webkit-font-smoothing: antialiased; 11 | -moz-osx-font-smoothing: grayscale; 12 | } 13 | -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/less/fixed-width.less: -------------------------------------------------------------------------------- 1 | // Fixed Width Icons 2 | // ------------------------- 3 | .@{fa-css-prefix}-fw { 4 | width: (18em / 14); 5 | text-align: center; 6 | } 7 | -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/scss/_core.scss: -------------------------------------------------------------------------------- 1 | // Base Class Definition 2 | // ------------------------- 3 | 4 | .#{$fa-css-prefix} { 5 | display: inline-block; 6 | font-family: FontAwesome; 7 | font-style: normal; 8 | font-weight: normal; 9 | line-height: 1; 10 | -webkit-font-smoothing: antialiased; 11 | -moz-osx-font-smoothing: grayscale; 12 | } 13 | -------------------------------------------------------------------------------- /source/_themes/pymotw/static/font-awesome/scss/_fixed-width.scss: -------------------------------------------------------------------------------- 1 | // Fixed Width Icons 2 | // ------------------------- 3 | .#{$fa-css-prefix}-fw { 4 | width: (18em / 14); 5 | text-align: center; 6 | } 7 | -------------------------------------------------------------------------------- /source/_themes/pymotw/static/logo-large.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/logo-large.png -------------------------------------------------------------------------------- /source/_themes/pymotw/static/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/_themes/pymotw/static/logo.png -------------------------------------------------------------------------------- /source/_themes/pymotw/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = none 3 | stylesheet = pymotw.css 4 | pygments_style = sphinx 5 | -------------------------------------------------------------------------------- /source/abc/abc_find_subclasses.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import abc 11 | from abc_base import PluginBase 12 | import abc_subclass 13 | import abc_register 14 | 15 | for sc in PluginBase.__subclasses__(): 16 | print(sc.__name__) 17 | -------------------------------------------------------------------------------- /source/argparse/argparse_choices.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import argparse 11 | 12 | parser = argparse.ArgumentParser() 13 | 14 | parser.add_argument( 15 | '--mode', 16 | choices=('read-only', 'read-write'), 17 | ) 18 | 19 | print(parser.parse_args()) 20 | -------------------------------------------------------------------------------- /source/argparse/argparse_fromfile_prefix_chars.txt: -------------------------------------------------------------------------------- 1 | -a 2 | -b 3 | 2 4 | -------------------------------------------------------------------------------- /source/argparse/argparse_parent_base.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Parent with shared options 7 | """ 8 | 9 | #end_pymotw_header 10 | import argparse 11 | 12 | parser = argparse.ArgumentParser(add_help=False) 13 | 14 | parser.add_argument('--user', action="store") 15 | parser.add_argument('--password', action="store") 16 | -------------------------------------------------------------------------------- /source/argparse/argparse_with_shlex.ini: -------------------------------------------------------------------------------- 1 | [cli] 2 | options = -a -b 2 -------------------------------------------------------------------------------- /source/argparse/tmp_file.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/argparse/tmp_file.txt -------------------------------------------------------------------------------- /source/array/array_sequence.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import array 11 | import pprint 12 | 13 | a = array.array('i', range(3)) 14 | print('Initial :', a) 15 | 16 | a.extend(range(3)) 17 | print('Extended:', a) 18 | 19 | print('Slice :', a[2:5]) 20 | 21 | print('Iterator:') 22 | print(list(enumerate(a))) 23 | -------------------------------------------------------------------------------- /source/array/array_string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import array 11 | import binascii 12 | 13 | s = b'This is the array.' 14 | a = array.array('b', s) 15 | 16 | print('As byte string:', s) 17 | print('As array :', a) 18 | print('As hex :', binascii.hexlify(a)) 19 | -------------------------------------------------------------------------------- /source/atexit/atexit_decorator.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using atexit.register as a decorator. 3 | """ 4 | # end_pymotw_header 5 | import atexit 6 | 7 | 8 | @atexit.register 9 | def all_done(): 10 | print('all_done()') 11 | 12 | 13 | print('starting main program') 14 | -------------------------------------------------------------------------------- /source/atexit/atexit_exception.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """If an exception is raised in an atexit callback, it is 3 | printed to the console. 4 | 5 | """ 6 | # end_pymotw_header 7 | import atexit 8 | 9 | 10 | def exit_with_exception(message): 11 | raise RuntimeError(message) 12 | 13 | 14 | atexit.register(exit_with_exception, 'Registered first') 15 | atexit.register(exit_with_exception, 'Registered second') 16 | -------------------------------------------------------------------------------- /source/atexit/atexit_multiple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Register repeatedly with different arguments. 3 | """ 4 | 5 | # end_pymotw_header 6 | import atexit 7 | 8 | 9 | def my_cleanup(name): 10 | print('my_cleanup({})'.format(name)) 11 | 12 | 13 | atexit.register(my_cleanup, 'first') 14 | atexit.register(my_cleanup, 'second') 15 | atexit.register(my_cleanup, 'third') 16 | -------------------------------------------------------------------------------- /source/atexit/atexit_os_exit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """atexit callbacks are not invoked if we bypass normal exit and 3 | cleanup. 4 | 5 | """ 6 | 7 | # end_pymotw_header 8 | import atexit 9 | import os 10 | 11 | 12 | def not_called(): 13 | print('This should not be called') 14 | 15 | 16 | print('Registering') 17 | atexit.register(not_called) 18 | print('Registered') 19 | 20 | print('Exiting...') 21 | os._exit(0) 22 | -------------------------------------------------------------------------------- /source/atexit/atexit_simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Simple atexit example. 3 | """ 4 | 5 | # end_pymotw_header 6 | import atexit 7 | 8 | 9 | def all_done(): 10 | print('all_done()') 11 | 12 | 13 | print('Registering') 14 | atexit.register(all_done) 15 | print('Registered') 16 | -------------------------------------------------------------------------------- /source/atexit/atexit_sys_exit.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """ 3 | """ 4 | 5 | # end_pymotw_header 6 | import atexit 7 | import sys 8 | 9 | 10 | def all_done(): 11 | print('all_done()') 12 | 13 | 14 | print('Registering') 15 | atexit.register(all_done) 16 | print('Registered') 17 | 18 | print('Exiting...') 19 | sys.exit() 20 | -------------------------------------------------------------------------------- /source/atexit/atexit_unregister.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Cancel registered callback 3 | """ 4 | # end_pymotw_header 5 | import atexit 6 | 7 | 8 | def my_cleanup(name): 9 | print('my_cleanup({})'.format(name)) 10 | 11 | 12 | atexit.register(my_cleanup, 'first') 13 | atexit.register(my_cleanup, 'second') 14 | atexit.register(my_cleanup, 'third') 15 | 16 | atexit.unregister(my_cleanup) 17 | -------------------------------------------------------------------------------- /source/atexit/atexit_unregister_not_registered.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Cancel registered callback 3 | """ 4 | # end_pymotw_header 5 | import atexit 6 | 7 | 8 | def my_cleanup(name): 9 | print('my_cleanup({})'.format(name)) 10 | 11 | 12 | if False: 13 | atexit.register(my_cleanup, 'never registered') 14 | 15 | atexit.unregister(my_cleanup) 16 | -------------------------------------------------------------------------------- /source/base64/base64_b64decode.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import base64 11 | 12 | encoded_data = b'VGhpcyBpcyB0aGUgZGF0YSwgaW4gdGhlIGNsZWFyLg==' 13 | decoded_data = base64.b64decode(encoded_data) 14 | print('Encoded :', encoded_data) 15 | print('Decoded :', decoded_data) 16 | -------------------------------------------------------------------------------- /source/bz2/bz2_file_read.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import bz2 11 | import io 12 | 13 | with bz2.BZ2File('example.bz2', 'rb') as input: 14 | with io.TextIOWrapper(input, encoding='utf-8') as dec: 15 | print(dec.read()) 16 | -------------------------------------------------------------------------------- /source/bz2/lorem.txt: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec 2 | egestas, enim et consectetuer ullamcorper, lectus ligula rutrum leo, 3 | a elementum elit tortor eu quam. Duis tincidunt nisi ut ante. Nulla 4 | facilisi. 5 | -------------------------------------------------------------------------------- /source/calendar/calendar_formatyear.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Print entire year calendar. 3 | """ 4 | #end_pymotw_header 5 | import calendar 6 | 7 | cal = calendar.TextCalendar(calendar.SUNDAY) 8 | print(cal.formatyear(2017, 2, 1, 1, 3)) 9 | -------------------------------------------------------------------------------- /source/calendar/calendar_htmlcalendar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Sample of TextCalendar output. 3 | """ 4 | #end_pymotw_header 5 | import calendar 6 | 7 | c = calendar.HTMLCalendar(calendar.SUNDAY) 8 | print(c.formatmonth(2017, 7)) 9 | -------------------------------------------------------------------------------- /source/calendar/calendar_locale.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Sample of TextCalendar output. 3 | """ 4 | #end_pymotw_header 5 | import calendar 6 | 7 | c = calendar.LocaleTextCalendar(locale='en_US') 8 | c.prmonth(2017, 7) 9 | 10 | print() 11 | 12 | c = calendar.LocaleTextCalendar(locale='fr_FR') 13 | c.prmonth(2017, 7) 14 | -------------------------------------------------------------------------------- /source/calendar/calendar_monthcalendar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Show the dates in each week of a month. 3 | """ 4 | #end_pymotw_header 5 | import calendar 6 | import pprint 7 | 8 | pprint.pprint(calendar.monthcalendar(2017, 7)) 9 | -------------------------------------------------------------------------------- /source/calendar/calendar_textcalendar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Sample of TextCalendar output. 3 | """ 4 | #end_pymotw_header 5 | import calendar 6 | 7 | c = calendar.TextCalendar(calendar.SUNDAY) 8 | c.prmonth(2017, 7) 9 | -------------------------------------------------------------------------------- /source/cgitb/cgitb_basic_traceback.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann. All rights reserved. 5 | # 6 | """Generate a traceback 7 | """ 8 | 9 | #end_pymotw_header 10 | def func2(a, divisor): 11 | return a / divisor 12 | 13 | 14 | def func1(a, b): 15 | c = b - 5 16 | return func2(a, c) 17 | 18 | func1(1, 5) 19 | -------------------------------------------------------------------------------- /source/cgitb/cgitb_local_vars.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann. All rights reserved. 5 | # 6 | """Local variables lead to an answer 7 | """ 8 | 9 | #end_pymotw_header 10 | import cgitb 11 | cgitb.enable(format='text') 12 | 13 | 14 | def func2(a, divisor): 15 | return a / divisor 16 | 17 | 18 | def func1(a, b): 19 | c = b - 5 20 | return func2(a, c) 21 | 22 | func1(1, 5) 23 | -------------------------------------------------------------------------------- /source/cmd/cmd_file.txt: -------------------------------------------------------------------------------- 1 | greet 2 | greet Alice and Bob 3 | -------------------------------------------------------------------------------- /source/collections/collections_counter_elements.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Produce the elements of the counter. 7 | """ 8 | 9 | #end_pymotw_header 10 | import collections 11 | 12 | c = collections.Counter('extremely') 13 | c['z'] = 0 14 | print(c) 15 | print(list(c.elements())) 16 | -------------------------------------------------------------------------------- /source/collections/collections_counter_get_values.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Updating counts. 7 | """ 8 | 9 | #end_pymotw_header 10 | import collections 11 | 12 | c = collections.Counter('abcdaab') 13 | 14 | for letter in 'abcde': 15 | print('{} : {}'.format(letter, c[letter])) 16 | -------------------------------------------------------------------------------- /source/collections/collections_counter_init.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Initializing a Counter 7 | """ 8 | 9 | #end_pymotw_header 10 | import collections 11 | 12 | print(collections.Counter(['a', 'b', 'c', 'a', 'b', 'b'])) 13 | print(collections.Counter({'a': 2, 'b': 3, 'c': 1})) 14 | print(collections.Counter(a=2, b=3, c=1)) 15 | -------------------------------------------------------------------------------- /source/collections/collections_counter_update.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Updating counts. 7 | """ 8 | 9 | #end_pymotw_header 10 | import collections 11 | 12 | c = collections.Counter() 13 | print('Initial :', c) 14 | 15 | c.update('abcdaab') 16 | print('Sequence:', c) 17 | 18 | c.update({'a': 1, 'd': 5}) 19 | print('Dict :', c) 20 | -------------------------------------------------------------------------------- /source/collections/collections_namedtuple_asdict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import collections 11 | 12 | Person = collections.namedtuple('Person', 'name age') 13 | 14 | bob = Person(name='Bob', age=30) 15 | print('Representation:', bob) 16 | print('As Dictionary:', bob._asdict()) 17 | -------------------------------------------------------------------------------- /source/collections/collections_namedtuple_fields.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import collections 11 | 12 | Person = collections.namedtuple('Person', 'name age') 13 | 14 | bob = Person(name='Bob', age=30) 15 | print('Representation:', bob) 16 | print('Fields:', bob._fields) 17 | -------------------------------------------------------------------------------- /source/collections/collections_namedtuple_immutable.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import collections 11 | 12 | Person = collections.namedtuple('Person', 'name age') 13 | 14 | pat = Person(name='Pat', age=12) 15 | print('\nRepresentation:', pat) 16 | 17 | pat.age = 21 18 | -------------------------------------------------------------------------------- /source/compileall/compileall_exclude_dirs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import compileall 11 | import re 12 | 13 | compileall.compile_dir( 14 | 'examples', 15 | rx=re.compile(r'/subdir'), 16 | ) 17 | -------------------------------------------------------------------------------- /source/compileall/compileall_path.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import compileall 11 | import sys 12 | 13 | sys.path[:] = ['examples', 'notthere'] 14 | print('sys.path =', sys.path) 15 | compileall.compile_path() 16 | -------------------------------------------------------------------------------- /source/compileall/compileall_recursion_depth.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import compileall 11 | import re 12 | 13 | compileall.compile_dir( 14 | 'examples', 15 | maxlevels=0, 16 | ) 17 | -------------------------------------------------------------------------------- /source/compileall/examples/README: -------------------------------------------------------------------------------- 1 | This directory contains meta-data files created from a temporary svn 2 | repository to illustrate what happens when a working copy is compiled 3 | without taking care to avoid recursing too deeply. 4 | -------------------------------------------------------------------------------- /source/compileall/examples/a.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | print('running {}'.format(__file__)) 11 | -------------------------------------------------------------------------------- /source/compileall/examples/subdir/b.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | print('running {}'.format(__file__)) 11 | -------------------------------------------------------------------------------- /source/configparser/allow_no_value.ini: -------------------------------------------------------------------------------- 1 | [flags] 2 | turn_feature_on 3 | -------------------------------------------------------------------------------- /source/configparser/configparser_escape.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | """Example use of ConfigParser module. 5 | """ 6 | 7 | #end_pymotw_header 8 | from configparser import ConfigParser 9 | import os 10 | 11 | filename = 'escape.ini' 12 | config = ConfigParser() 13 | config.read([filename]) 14 | 15 | value = config.get('escape', 'value') 16 | 17 | print(value) 18 | -------------------------------------------------------------------------------- /source/configparser/configparser_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | """Example use of ConfigParser module. 5 | """ 6 | 7 | #end_pymotw_header 8 | from configparser import ConfigParser 9 | import os 10 | 11 | filename = 'approach.ini' 12 | config = ConfigParser() 13 | config.read([filename]) 14 | 15 | url = config.get('portal', 'url') 16 | 17 | print(url) 18 | -------------------------------------------------------------------------------- /source/configparser/configparser_interpolation_defaults.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Reading a configuration file. 7 | """ 8 | 9 | #end_pymotw_header 10 | from configparser import ConfigParser 11 | 12 | parser = ConfigParser() 13 | parser.read('interpolation_defaults.ini') 14 | 15 | print('URL:', parser.get('bug_tracker', 'url')) 16 | -------------------------------------------------------------------------------- /source/configparser/configparser_multiline.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2015 Doug Hellmann. 4 | """Example use of ConfigParser module. 5 | """ 6 | 7 | #end_pymotw_header 8 | from configparser import ConfigParser 9 | import os 10 | 11 | filename = 'multiline.ini' 12 | config = ConfigParser() 13 | config.read([filename]) 14 | 15 | message = config['example']['message'] 16 | 17 | print(message) 18 | -------------------------------------------------------------------------------- /source/configparser/configparser_nointerpolation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Disabling interpolation 7 | """ 8 | 9 | #end_pymotw_header 10 | from configparser import ConfigParser 11 | 12 | parser = ConfigParser(interpolation=None) 13 | parser.read('interpolation.ini') 14 | 15 | print('Without interpolation:', parser.get('bug_tracker', 'url')) 16 | -------------------------------------------------------------------------------- /source/configparser/configparser_read.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Reading a configuration file. 7 | """ 8 | 9 | #end_pymotw_header 10 | from configparser import ConfigParser 11 | 12 | parser = ConfigParser() 13 | parser.read('simple.ini') 14 | 15 | print(parser.get('bug_tracker', 'url')) 16 | -------------------------------------------------------------------------------- /source/configparser/custom_types.ini: -------------------------------------------------------------------------------- 1 | [datetimes] 2 | due_date = 2015-11-08T11:30:05.905898 -------------------------------------------------------------------------------- /source/configparser/escape.ini: -------------------------------------------------------------------------------- 1 | [escape] 2 | value = a literal %% must be escaped -------------------------------------------------------------------------------- /source/configparser/extended_interpolation.ini: -------------------------------------------------------------------------------- 1 | [intranet] 2 | server = localhost 3 | port = 8080 4 | 5 | [bug_tracker] 6 | url = http://${intranet:server}:${intranet:port}/bugs/ 7 | username = dhellmann 8 | password = SECRET 9 | -------------------------------------------------------------------------------- /source/configparser/interpolation.ini: -------------------------------------------------------------------------------- 1 | [bug_tracker] 2 | protocol = http 3 | server = localhost 4 | port = 8080 5 | url = %(protocol)s://%(server)s:%(port)s/bugs/ 6 | username = dhellmann 7 | password = SECRET 8 | -------------------------------------------------------------------------------- /source/configparser/interpolation_defaults.ini: -------------------------------------------------------------------------------- 1 | [DEFAULT] 2 | url = %(protocol)s://%(server)s:%(port)s/bugs/ 3 | protocol = http 4 | server = bugs.example.com 5 | port = 80 6 | 7 | [bug_tracker] 8 | server = localhost 9 | port = 8080 10 | username = dhellmann 11 | password = SECRET 12 | -------------------------------------------------------------------------------- /source/configparser/multiline.ini: -------------------------------------------------------------------------------- 1 | [example] 2 | message = This is a multi-line string. 3 | With two paragraphs. 4 | 5 | They are separated by a completely empty line. 6 | -------------------------------------------------------------------------------- /source/configparser/multisection.ini: -------------------------------------------------------------------------------- 1 | [bug_tracker] 2 | url = http://localhost:8080/bugs/ 3 | username = dhellmann 4 | password = SECRET 5 | 6 | [wiki] 7 | url = http://localhost:8080/wiki/ 8 | username = dhellmann 9 | password = SECRET 10 | -------------------------------------------------------------------------------- /source/configparser/simple.ini: -------------------------------------------------------------------------------- 1 | # This is a simple example with comments. 2 | [bug_tracker] 3 | url = http://localhost:8080/bugs/ 4 | username = dhellmann 5 | ; You should not store passwords in plain text 6 | ; configuration files. 7 | password = SECRET 8 | -------------------------------------------------------------------------------- /source/configparser/types.ini: -------------------------------------------------------------------------------- 1 | [ints] 2 | positive = 1 3 | negative = -5 4 | 5 | [floats] 6 | positive = 0.2 7 | negative = -3.14 8 | 9 | [booleans] 10 | number_true = 1 11 | number_false = 0 12 | yn_true = yes 13 | yn_false = no 14 | tf_true = true 15 | tf_false = false 16 | onoff_true = on 17 | onoff_false = false 18 | -------------------------------------------------------------------------------- /source/configparser/unicode.ini: -------------------------------------------------------------------------------- 1 | [bug_tracker] 2 | url = http://localhost:8080/bugs/ 3 | username = dhellmann 4 | password = ßéç®é† 5 | -------------------------------------------------------------------------------- /source/configparser/with-defaults.ini: -------------------------------------------------------------------------------- 1 | [DEFAULT] 2 | file-only = value from DEFAULT section 3 | init-and-file = value from DEFAULT section 4 | from-section = value from DEFAULT section 5 | from-vars = value from DEFAULT section 6 | 7 | [sect] 8 | section-only = value from section in file 9 | from-section = value from section in file 10 | from-vars = value from section in file 11 | -------------------------------------------------------------------------------- /source/contextlib/contextlib_file.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Files as context managers. 7 | """ 8 | 9 | #end_pymotw_header 10 | with open('/tmp/pymotw.txt', 'wt') as f: 11 | f.write('contents go here') 12 | # file is automatically closed 13 | -------------------------------------------------------------------------------- /source/csv/csv_dialect.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | #end_pymotw_header 9 | import csv 10 | 11 | csv.register_dialect('pipes', delimiter='|') 12 | 13 | with open('testdata.pipes', 'r') as f: 14 | reader = csv.reader(f, dialect='pipes') 15 | for row in reader: 16 | print(row) 17 | -------------------------------------------------------------------------------- /source/csv/csv_dictreader.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """Dictionary reader. 4 | """ 5 | #end_pymotw_header 6 | import csv 7 | import sys 8 | 9 | with open(sys.argv[1], 'rt') as f: 10 | reader = csv.DictReader(f) 11 | for row in reader: 12 | print(row) 13 | -------------------------------------------------------------------------------- /source/csv/csv_list_dialects.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """List the registered dialects. 7 | """ 8 | #end_pymotw_header 9 | import csv 10 | 11 | print(csv.list_dialects()) 12 | -------------------------------------------------------------------------------- /source/csv/csv_reader.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Example of reading a comma separated value file. 6 | """ 7 | #end_pymotw_header 8 | import csv 9 | import sys 10 | 11 | with open(sys.argv[1], 'rt') as f: 12 | reader = csv.reader(f) 13 | for row in reader: 14 | print(row) 15 | -------------------------------------------------------------------------------- /source/csv/testdata.csv: -------------------------------------------------------------------------------- 1 | "Title 1","Title 2","Title 3","Title 4" 2 | 1,"a",08/18/07,"å" 3 | 2,"b",08/19/07,"∫" 4 | 3,"c",08/20/07,"ç" 5 | -------------------------------------------------------------------------------- /source/csv/testdata.pipes: -------------------------------------------------------------------------------- 1 | "Title 1"|"Title 2"|"Title 3" 2 | 1|"first line 3 | second line"|08/18/07 4 | -------------------------------------------------------------------------------- /source/csv/testlinebreak.csv: -------------------------------------------------------------------------------- 1 | "Title 1","Title 2","Title 3" 2 | 1,"first line 3 | second line",08/18/07 4 | -------------------------------------------------------------------------------- /source/datetime/datetime_date_minmax.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """Minimum and maximum values for dates. 7 | """ 8 | 9 | #end_pymotw_header 10 | import datetime 11 | 12 | print('Earliest :', datetime.date.min) 13 | print('Latest :', datetime.date.max) 14 | print('Resolution:', datetime.date.resolution) 15 | -------------------------------------------------------------------------------- /source/datetime/datetime_date_replace.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """Creating dates from existing objects. 7 | """ 8 | 9 | #end_pymotw_header 10 | import datetime 11 | 12 | d1 = datetime.date(2008, 3, 29) 13 | print('d1:', d1.ctime()) 14 | 15 | d2 = d1.replace(year=2009) 16 | print('d2:', d2.ctime()) 17 | -------------------------------------------------------------------------------- /source/datetime/datetime_datetime_combine.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import datetime 11 | 12 | t = datetime.time(1, 2, 3) 13 | print('t :', t) 14 | 15 | d = datetime.date.today() 16 | print('d :', d) 17 | 18 | dt = datetime.datetime.combine(d, t) 19 | print('dt:', dt) 20 | -------------------------------------------------------------------------------- /source/datetime/datetime_datetime_minmax.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """The range of valid datetime values. 7 | """ 8 | 9 | #end_pymotw_header 10 | import datetime 11 | 12 | print('Earliest :', datetime.datetime.min) 13 | print('Latest :', datetime.datetime.max) 14 | print('Resolution:', datetime.datetime.resolution) 15 | -------------------------------------------------------------------------------- /source/datetime/datetime_format.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import datetime 11 | 12 | today = datetime.datetime.today() 13 | print('ISO :', today) 14 | print('format(): {:%a %b %d %H:%M:%S %Y}'.format(today)) 15 | -------------------------------------------------------------------------------- /source/datetime/datetime_time_minmax.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """Valid range of time values. 7 | """ 8 | 9 | #end_pymotw_header 10 | import datetime 11 | 12 | print('Earliest :', datetime.time.min) 13 | print('Latest :', datetime.time.max) 14 | print('Resolution:', datetime.time.resolution) 15 | -------------------------------------------------------------------------------- /source/dbm/dbm_existing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import dbm 11 | 12 | with dbm.open('/tmp/example.db', 'r') as db: 13 | print('keys():', db.keys()) 14 | for k in db.keys(): 15 | print('iterating:', k, db[k]) 16 | print('db["author"] =', db['author']) 17 | -------------------------------------------------------------------------------- /source/dbm/dbm_intkeys.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import dbm 11 | 12 | with dbm.open('/tmp/example.db', 'w') as db: 13 | try: 14 | db[1] = 'one' 15 | except TypeError as err: 16 | print(err) 17 | -------------------------------------------------------------------------------- /source/dbm/dbm_intvalue.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import dbm 11 | 12 | with dbm.open('/tmp/example.db', 'w') as db: 13 | try: 14 | db['one'] = 1 15 | except TypeError as err: 16 | print(err) 17 | -------------------------------------------------------------------------------- /source/dbm/dbm_new.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import dbm 11 | 12 | with dbm.open('/tmp/example.db', 'n') as db: 13 | db['key'] = 'value' 14 | db['today'] = 'Sunday' 15 | db['author'] = 'Doug' 16 | -------------------------------------------------------------------------------- /source/dbm/dbm_whichdb.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import dbm 11 | 12 | print(dbm.whichdb('/tmp/example.db')) 13 | -------------------------------------------------------------------------------- /source/decimal/decimal_log.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import decimal 11 | 12 | d = decimal.Decimal(100) 13 | print('d :', d) 14 | print('log10 :', d.log10()) 15 | print('ln :', d.ln()) 16 | -------------------------------------------------------------------------------- /source/decimal/decimal_precision.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import decimal 11 | 12 | d = decimal.Decimal('0.123456') 13 | 14 | for i in range(1, 5): 15 | decimal.getcontext().prec = i 16 | print(i, ':', d, d * 1) 17 | -------------------------------------------------------------------------------- /source/decimal/decimal_tuple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import decimal 11 | 12 | # Tuple 13 | t = (1, (1, 1), -2) 14 | print('Input :', t) 15 | print('Decimal:', decimal.Decimal(t)) 16 | -------------------------------------------------------------------------------- /source/difflib/difflib_context.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Context diff example 6 | """ 7 | 8 | #end_pymotw_header 9 | import difflib 10 | from difflib_data import * 11 | 12 | diff = difflib.context_diff( 13 | text1_lines, 14 | text2_lines, 15 | lineterm='', 16 | ) 17 | print('\n'.join(diff)) 18 | -------------------------------------------------------------------------------- /source/difflib/difflib_differ.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Differ example 6 | """ 7 | 8 | 9 | #end_pymotw_header 10 | import difflib 11 | from difflib_data import * 12 | 13 | d = difflib.Differ() 14 | diff = d.compare(text1_lines, text2_lines) 15 | print('\n'.join(diff)) 16 | -------------------------------------------------------------------------------- /source/difflib/difflib_html.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """HtmlDiff example 6 | """ 7 | 8 | 9 | #end_pymotw_header 10 | import difflib 11 | from difflib_data import * 12 | 13 | d = difflib.HtmlDiff() 14 | print(d.make_table(text1_lines, text2_lines)) 15 | -------------------------------------------------------------------------------- /source/difflib/difflib_ndiff.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """ndiff example 6 | """ 7 | 8 | #end_pymotw_header 9 | import difflib 10 | from difflib_data import * 11 | 12 | diff = difflib.ndiff(text1_lines, text2_lines) 13 | print('\n'.join(diff)) 14 | -------------------------------------------------------------------------------- /source/difflib/difflib_unified.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Unified diff example 6 | """ 7 | 8 | 9 | #end_pymotw_header 10 | import difflib 11 | from difflib_data import * 12 | 13 | diff = difflib.unified_diff( 14 | text1_lines, 15 | text2_lines, 16 | lineterm='', 17 | ) 18 | print('\n'.join(diff)) 19 | -------------------------------------------------------------------------------- /source/dis/dis_class.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | 4 | import dis 5 | 6 | 7 | class MyObject: 8 | """Example for dis.""" 9 | 10 | CLASS_ATTRIBUTE = 'some value' 11 | 12 | def __str__(self): 13 | return 'MyObject({})'.format(self.name) 14 | 15 | def __init__(self, name): 16 | self.name = name 17 | 18 | 19 | dis.dis(MyObject) 20 | -------------------------------------------------------------------------------- /source/dis/dis_constant_folding.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | 4 | # Folded 5 | i = 1 + 2 6 | f = 3.4 * 5.6 7 | s = 'Hello,' + ' World!' 8 | 9 | # Not folded 10 | I = i * 3 * 4 11 | F = f / 2 / 3 12 | S = s + '\n' + 'Fantastic!' 13 | -------------------------------------------------------------------------------- /source/dis/dis_fastest_loop.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | 4 | import collections 5 | 6 | 7 | class Dictionary: 8 | 9 | def __init__(self, words): 10 | self.by_letter = collections.defaultdict(list) 11 | self.load_data(words) 12 | 13 | def load_data(self, words): 14 | by_letter = self.by_letter 15 | for word in words: 16 | by_letter[word[0]].append(word) 17 | -------------------------------------------------------------------------------- /source/dis/dis_function.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | 4 | 5 | def f(*args): 6 | nargs = len(args) 7 | print(nargs, args) 8 | 9 | 10 | if __name__ == '__main__': 11 | import dis 12 | dis.dis(f) 13 | -------------------------------------------------------------------------------- /source/dis/dis_show_code.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | 4 | 5 | def f(*args): 6 | nargs = len(args) 7 | print(nargs, args) 8 | 9 | 10 | if __name__ == '__main__': 11 | import dis 12 | dis.show_code(f) 13 | -------------------------------------------------------------------------------- /source/dis/dis_simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | 4 | my_dict = {'a': 1} 5 | -------------------------------------------------------------------------------- /source/dis/dis_slow_loop.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | 4 | 5 | class Dictionary: 6 | 7 | def __init__(self, words): 8 | self.by_letter = {} 9 | self.load_data(words) 10 | 11 | def load_data(self, words): 12 | for word in words: 13 | try: 14 | self.by_letter[word[0]].append(word) 15 | except KeyError: 16 | self.by_letter[word[0]] = [word] 17 | -------------------------------------------------------------------------------- /source/dis/dis_string.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2016 Doug Hellmann. All rights reserved. 5 | # Written for https://pymotw.com 6 | # 7 | 8 | #end_pymotw_header 9 | import dis 10 | 11 | code = """ 12 | my_dict = {'a': 1} 13 | """ 14 | 15 | print('Disassembly:\n') 16 | dis.dis(code) 17 | 18 | print('\nCode details:\n') 19 | dis.show_code(code) 20 | -------------------------------------------------------------------------------- /source/dis/dis_traceback.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | 4 | i = 1 5 | j = 0 6 | k = 3 7 | 8 | try: 9 | result = k * (i / j) + (i / k) 10 | except Exception: 11 | import dis 12 | import sys 13 | exc_type, exc_value, exc_tb = sys.exc_info() 14 | dis.distb(exc_tb) 15 | -------------------------------------------------------------------------------- /source/doctest/doctest_hashed_values.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Demonstrate how insertion order affects dictonaries and sets. 7 | """ 8 | 9 | #end_pymotw_header 10 | keys = ['a', 'aa', 'aaa'] 11 | 12 | print('dict:', {k: len(k) for k in keys}) 13 | print('set :', set(keys)) 14 | -------------------------------------------------------------------------------- /source/doctest/doctest_in_help.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Tests for this module are in the external help text. 7 | """ 8 | #end_pymotw_header 9 | def my_function(a, b): 10 | """Returns a*b 11 | """ 12 | return a * b 13 | -------------------------------------------------------------------------------- /source/doctest/doctest_private_tests_external.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | #end_pymotw_header 7 | """External tests associated with doctest_private_tests.py. 8 | 9 | >>> my_function(['A', 'B', 'C'], 2) 10 | ['A', 'B', 'C', 'A', 'B', 'C'] 11 | """ 12 | -------------------------------------------------------------------------------- /source/doctest/doctest_simple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Simple example using doctest 7 | """ 8 | 9 | #end_pymotw_header 10 | def my_function(a, b): 11 | """ 12 | >>> my_function(2, 3) 13 | 6 14 | >>> my_function('a', 3) 15 | 'aaa' 16 | """ 17 | return a * b 18 | -------------------------------------------------------------------------------- /source/doctest/doctest_testfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Run the tests from an external file. 7 | """ 8 | 9 | #end_pymotw_header 10 | import doctest 11 | 12 | if __name__ == '__main__': 13 | doctest.testfile('doctest_in_help.txt') 14 | -------------------------------------------------------------------------------- /source/doctest/doctest_testmod_other_module.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Run tests for another module we import. 7 | """ 8 | 9 | #end_pymotw_header 10 | import doctest_simple 11 | 12 | if __name__ == '__main__': 13 | import doctest 14 | doctest.testmod(doctest_simple) 15 | -------------------------------------------------------------------------------- /source/enum/enum_intenum.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """ 4 | """ 5 | 6 | #end_pymotw_header 7 | import enum 8 | 9 | 10 | class BugStatus(enum.IntEnum): 11 | 12 | new = 7 13 | incomplete = 6 14 | invalid = 5 15 | wont_fix = 4 16 | in_progress = 3 17 | fix_committed = 2 18 | fix_released = 1 19 | 20 | 21 | print('Ordered by value:') 22 | print('\n'.join(' ' + s.name for s in sorted(BugStatus))) 23 | -------------------------------------------------------------------------------- /source/enum/enum_iterate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """ 4 | """ 5 | 6 | #end_pymotw_header 7 | import enum 8 | 9 | 10 | class BugStatus(enum.Enum): 11 | 12 | new = 7 13 | incomplete = 6 14 | invalid = 5 15 | wont_fix = 4 16 | in_progress = 3 17 | fix_committed = 2 18 | fix_released = 1 19 | 20 | 21 | for status in BugStatus: 22 | print('{:15} = {}'.format(status.name, status.value)) 23 | -------------------------------------------------------------------------------- /source/filecmp/filecmp_dircmp_diff.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import filecmp 11 | 12 | dc = filecmp.dircmp('example/dir1', 'example/dir2') 13 | print('Same :', dc.same_files) 14 | print('Different :', dc.diff_files) 15 | print('Funny :', dc.funny_files) 16 | -------------------------------------------------------------------------------- /source/filecmp/filecmp_dircmp_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import filecmp 11 | import pprint 12 | 13 | dc = filecmp.dircmp('example/dir1', 'example/dir2') 14 | print('Left:') 15 | pprint.pprint(dc.left_list) 16 | 17 | print('\nRight:') 18 | pprint.pprint(dc.right_list) 19 | -------------------------------------------------------------------------------- /source/filecmp/filecmp_dircmp_report.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import filecmp 11 | 12 | dc = filecmp.dircmp('example/dir1', 'example/dir2') 13 | dc.report() 14 | -------------------------------------------------------------------------------- /source/filecmp/filecmp_dircmp_report_full_closure.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import filecmp 11 | 12 | dc = filecmp.dircmp('example/dir1', 'example/dir2') 13 | dc.report_full_closure() 14 | -------------------------------------------------------------------------------- /source/filecmp/filecmp_dircmp_subdirs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import filecmp 11 | 12 | dc = filecmp.dircmp('example/dir1', 'example/dir2') 13 | print('Subdirectories:') 14 | print(dc.subdirs) 15 | -------------------------------------------------------------------------------- /source/fileinput/etc_hosts: -------------------------------------------------------------------------------- 1 | ## 2 | # Host Database 3 | # 4 | # localhost is used to configure the loopback interface 5 | # when the system is booting. Do not change this entry. 6 | ## 7 | 127.0.0.1 localhost 8 | 255.255.255.255 broadcasthost 9 | ::1 localhost 10 | fe80::1%lo0 localhost 11 | 10.16.177.128 hubert hubert.hellfly.net 12 | 10.16.177.132 cubert cubert.hellfly.net 13 | 10.16.177.136 zoidberg zoidberg.hellfly.net 14 | -------------------------------------------------------------------------------- /source/fileinput/etc_hosts.txt: -------------------------------------------------------------------------------- 1 | ## 2 | # Host Database 3 | # 4 | # localhost is used to configure the loopback interface 5 | # when the system is booting. Do not change this entry. 6 | ## 7 | 127.0.0.1 localhost 8 | 255.255.255.255 broadcasthost 9 | ::1 localhost 10 | fe80::1%lo0 localhost 11 | 10.17.177.128 hubert hubert.hellfly.net 12 | 10.17.177.132 cubert cubert.hellfly.net 13 | 10.17.177.136 zoidberg zoidberg.hellfly.net 14 | -------------------------------------------------------------------------------- /source/fileinput/sample_data.m3u: -------------------------------------------------------------------------------- 1 | # This is a sample m3u file 2 | episode-one.mp3 3 | episode-two.mp3 4 | -------------------------------------------------------------------------------- /source/fnmatch/fnmatch_translate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """Translate a glob-style pattern to a regular expression. 7 | """ 8 | 9 | #end_pymotw_header 10 | import fnmatch 11 | 12 | pattern = 'fnmatch_*.py' 13 | print('Pattern :', pattern) 14 | print('Regex :', fnmatch.translate(pattern)) 15 | -------------------------------------------------------------------------------- /source/fractions/fractions_create_integers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import fractions 11 | 12 | for n, d in [(1, 2), (2, 4), (3, 6)]: 13 | f = fractions.Fraction(n, d) 14 | print('{}/{} = {}'.format(n, d, f)) 15 | -------------------------------------------------------------------------------- /source/fractions/fractions_create_strings.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import fractions 11 | 12 | for s in ['1/2', '2/4', '3/6']: 13 | f = fractions.Fraction(s) 14 | print('{} = {}'.format(s, f)) 15 | -------------------------------------------------------------------------------- /source/fractions/fractions_create_strings_floats.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import fractions 11 | 12 | for s in ['0.5', '1.5', '2.0', '5e-1']: 13 | f = fractions.Fraction(s) 14 | print('{0:>4} = {1}'.format(s, f)) 15 | -------------------------------------------------------------------------------- /source/fractions/fractions_from_float.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import fractions 11 | 12 | for v in [0.1, 0.5, 1.5, 2.0]: 13 | print('{} = {}'.format(v, fractions.Fraction(v))) 14 | -------------------------------------------------------------------------------- /source/functools/functools_reduce.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """ 4 | """ 5 | 6 | #end_pymotw_header 7 | import functools 8 | 9 | 10 | def do_reduce(a, b): 11 | print('do_reduce({}, {})'.format(a, b)) 12 | return a + b 13 | 14 | 15 | data = range(1, 5) 16 | print(data) 17 | result = functools.reduce(do_reduce, data) 18 | print('result: {}'.format(result)) 19 | -------------------------------------------------------------------------------- /source/functools/functools_reduce_initializer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """ 4 | """ 5 | 6 | #end_pymotw_header 7 | import functools 8 | 9 | 10 | def do_reduce(a, b): 11 | print('do_reduce({}, {})'.format(a, b)) 12 | return a + b 13 | 14 | 15 | data = range(1, 5) 16 | print(data) 17 | result = functools.reduce(do_reduce, data, 99) 18 | print('result: {}'.format(result)) 19 | -------------------------------------------------------------------------------- /source/gc/gc_debug_stats.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Tuning the garbage collector threshold. 7 | """ 8 | 9 | #end_pymotw_header 10 | import gc 11 | 12 | gc.set_debug(gc.DEBUG_STATS) 13 | 14 | gc.collect() 15 | print('Exiting') 16 | -------------------------------------------------------------------------------- /source/gc/gc_get_objects.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Examine the objects being managed. 7 | """ 8 | 9 | #end_pymotw_header 10 | import gc 11 | 12 | print(len(gc.get_objects())) 13 | -------------------------------------------------------------------------------- /source/gc/gc_get_threshold.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Show the default thresholds. 7 | """ 8 | 9 | #end_pymotw_header 10 | import gc 11 | 12 | print(gc.get_threshold()) 13 | -------------------------------------------------------------------------------- /source/getopt/getopt_long.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using getopt with longer option names. 3 | """ 4 | 5 | #end_pymotw_header 6 | import getopt 7 | 8 | opts, args = getopt.getopt( 9 | ['--noarg', 10 | '--witharg', 'val', 11 | '--witharg2=another'], 12 | '', 13 | ['noarg', 'witharg=', 'witharg2='], 14 | ) 15 | for opt in opts: 16 | print(opt) 17 | -------------------------------------------------------------------------------- /source/getopt/getopt_short.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using optparse with single-letter options. 3 | """ 4 | 5 | #end_pymotw_header 6 | import getopt 7 | 8 | opts, args = getopt.getopt(['-a', '-bval', '-c', 'val'], 'ab:c:') 9 | 10 | for opt in opts: 11 | print(opt) 12 | -------------------------------------------------------------------------------- /source/getpass/getpass_defaults.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright 2007 Doug Hellmann. 5 | # 6 | """Default use of getpass. 7 | """ 8 | 9 | #end_pymotw_header 10 | import getpass 11 | 12 | try: 13 | p = getpass.getpass() 14 | except Exception as err: 15 | print('ERROR:', err) 16 | else: 17 | print('You entered:', p) 18 | -------------------------------------------------------------------------------- /source/getpass/getpass_prompt.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright 2007 Doug Hellmann. 5 | # 6 | """Passing a prompt to getpass. 7 | 8 | """ 9 | 10 | #end_pymotw_header 11 | import getpass 12 | 13 | p = getpass.getpass(prompt='What is your favorite color? ') 14 | if p.lower() == 'blue': 15 | print('Right. Off you go.') 16 | else: 17 | print('Auuuuugh!') 18 | -------------------------------------------------------------------------------- /source/getpass/getpass_stream.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright 2007 Doug Hellmann. 5 | # 6 | """Using sys.stderr for the prompt lets us redirect stdout. 7 | 8 | """ 9 | 10 | #end_pymotw_header 11 | import getpass 12 | import sys 13 | 14 | p = getpass.getpass(stream=sys.stderr) 15 | print('You entered:', p) 16 | -------------------------------------------------------------------------------- /source/gettext/gettext_app_builtin.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | # flake8: noqa 9 | 10 | #end_pymotw_header 11 | import gettext 12 | 13 | gettext.install( 14 | 'example', 15 | 'locale', 16 | names=['ngettext'], 17 | ) 18 | 19 | print(_('This message is in the script.')) 20 | -------------------------------------------------------------------------------- /source/gettext/gettext_example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import gettext 11 | 12 | # Set up message catalog access 13 | t = gettext.translation( 14 | 'example_domain', 'locale', 15 | fallback=True, 16 | ) 17 | _ = t.gettext 18 | 19 | print(_('This message is in the script.')) 20 | -------------------------------------------------------------------------------- /source/gettext/gettext_example_corrected.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import gettext 11 | 12 | # Set up message catalog access 13 | t = gettext.translation( 14 | 'example', 'locale', 15 | fallback=True, 16 | ) 17 | _ = t.gettext 18 | 19 | print(_('This message is in the script.')) 20 | -------------------------------------------------------------------------------- /source/gettext/gettext_find.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import gettext 11 | 12 | catalogs = gettext.find('example', 'locale', all=True) 13 | print('Catalogs:', catalogs) 14 | -------------------------------------------------------------------------------- /source/gettext/gettext_module_global.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import gettext 11 | 12 | t = gettext.translation( 13 | 'example', 14 | 'locale', 15 | fallback=False, 16 | ) 17 | _ = t.gettext 18 | ngettext = t.ngettext 19 | 20 | print(_('This message is in the script.')) 21 | -------------------------------------------------------------------------------- /source/gettext/locale/en_CA/LC_MESSAGES/example.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/gettext/locale/en_CA/LC_MESSAGES/example.mo -------------------------------------------------------------------------------- /source/gettext/locale/en_US/LC_MESSAGES/example.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/gettext/locale/en_US/LC_MESSAGES/example.mo -------------------------------------------------------------------------------- /source/gettext/locale/en_US/LC_MESSAGES/plural.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/gettext/locale/en_US/LC_MESSAGES/plural.mo -------------------------------------------------------------------------------- /source/gzip/gzip_read.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import gzip 11 | import io 12 | 13 | with gzip.open('example.txt.gz', 'rb') as input_file: 14 | with io.TextIOWrapper(input_file, encoding='utf-8') as dec: 15 | print(dec.read()) 16 | -------------------------------------------------------------------------------- /source/hashlib/hashlib_algorithms.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """ 4 | """ 5 | 6 | #end_pymotw_header 7 | import hashlib 8 | 9 | 10 | print('Guaranteed:\n{}\n'.format( 11 | ', '.join(sorted(hashlib.algorithms_guaranteed)))) 12 | print('Available:\n{}'.format( 13 | ', '.join(sorted(hashlib.algorithms_available)))) 14 | -------------------------------------------------------------------------------- /source/hashlib/hashlib_md5.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """Simple MD5 generation. 7 | """ 8 | 9 | #end_pymotw_header 10 | import hashlib 11 | 12 | from hashlib_data import lorem 13 | 14 | h = hashlib.md5() 15 | h.update(lorem.encode('utf-8')) 16 | print(h.hexdigest()) 17 | -------------------------------------------------------------------------------- /source/hashlib/hashlib_sha1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """Simple SHA1 generation. 7 | """ 8 | 9 | #end_pymotw_header 10 | import hashlib 11 | 12 | from hashlib_data import lorem 13 | 14 | h = hashlib.sha1() 15 | h.update(lorem.encode('utf-8')) 16 | print(h.hexdigest()) 17 | -------------------------------------------------------------------------------- /source/heapq/heapq_heapdata.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | # This data was generated with the random module. 11 | 12 | data = [19, 9, 4, 10, 11] 13 | -------------------------------------------------------------------------------- /source/heapq/heapq_heapify.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import heapq 11 | from heapq_showtree import show_tree 12 | from heapq_heapdata import data 13 | 14 | print('random :', data) 15 | heapq.heapify(data) 16 | print('heapified :') 17 | show_tree(data) 18 | -------------------------------------------------------------------------------- /source/hmac/hmac_base64.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Generate a base64 encoded HMAC signature. 3 | """ 4 | 5 | #end_pymotw_header 6 | import base64 7 | import hmac 8 | import hashlib 9 | 10 | with open('lorem.txt', 'rb') as f: 11 | body = f.read() 12 | 13 | hash = hmac.new( 14 | b'secret-shared-key-goes-here', 15 | body, 16 | hashlib.sha1, 17 | ) 18 | 19 | digest = hash.digest() 20 | print(base64.encodebytes(digest)) 21 | -------------------------------------------------------------------------------- /source/http.cookies/http_cookies_setheaders.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | 10 | #end_pymotw_header 11 | from http import cookies 12 | 13 | 14 | c = cookies.SimpleCookie() 15 | c['mycookie'] = 'cookie_value' 16 | print(c) 17 | -------------------------------------------------------------------------------- /source/images/replacement-character.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/images/replacement-character.png -------------------------------------------------------------------------------- /source/imaplib/imaplib_list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import imaplib 11 | from pprint import pprint 12 | from imaplib_connect import open_connection 13 | 14 | with open_connection() as c: 15 | typ, data = c.list() 16 | print('Response code:', typ) 17 | print('Response:') 18 | pprint(data) 19 | -------------------------------------------------------------------------------- /source/imaplib/imaplib_select_invalid.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import imaplib 11 | import imaplib_connect 12 | 13 | with imaplib_connect.open_connection() as c: 14 | typ, data = c.select('Does-Not-Exist') 15 | print(typ, data) 16 | -------------------------------------------------------------------------------- /source/importlib/example/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | print('Importing example package') 11 | -------------------------------------------------------------------------------- /source/importlib/example/submodule.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | print('Importing submodule') 11 | -------------------------------------------------------------------------------- /source/importlib/importlib_find_loader.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """ 4 | """ 5 | 6 | #end_pymotw_header 7 | import importlib 8 | 9 | spec = importlib.util.find_spec('example') 10 | print('Loader:', spec.loader) 11 | 12 | m = spec.loader.load_module() 13 | print('Module:', m) 14 | -------------------------------------------------------------------------------- /source/importlib/importlib_import_module_error.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2016 Doug Hellmann. All rights reserved. 5 | # Written for https://pymotw.com 6 | # 7 | """Importing a module from a package 8 | """ 9 | 10 | #end_pymotw_header 11 | import importlib 12 | 13 | 14 | try: 15 | importlib.import_module('example.nosuchmodule') 16 | except ImportError as err: 17 | print('Error:', err) 18 | -------------------------------------------------------------------------------- /source/importlib/importlib_reload.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2016 Doug Hellmann. All rights reserved. 5 | # Written for https://pymotw.com 6 | # 7 | """Importing a module from a package 8 | """ 9 | 10 | #end_pymotw_header 11 | import importlib 12 | 13 | 14 | m1 = importlib.import_module('example.submodule') 15 | print(m1) 16 | 17 | m2 = importlib.reload(m1) 18 | print(m1 is m2) 19 | -------------------------------------------------------------------------------- /source/importlib/importlib_submodule.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """ 4 | """ 5 | 6 | #end_pymotw_header 7 | import importlib 8 | 9 | spec = importlib.util.find_spec('.submodule', package='example') 10 | print('Loader:', spec.loader) 11 | 12 | m = spec.loader.load_module() 13 | print('Module:', m) 14 | -------------------------------------------------------------------------------- /source/inspect/inspect_formatargspec.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Print information about the arguments to a method. 3 | """ 4 | 5 | 6 | #end_pymotw_header 7 | import inspect 8 | import example 9 | 10 | spec = inspect.getargspec(example.module_level_function) 11 | print(spec) 12 | print(inspect.formatargspec(spec)) 13 | -------------------------------------------------------------------------------- /source/inspect/inspect_getclasstree_unique.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """ 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | import example 8 | from inspect_getclasstree import * 9 | 10 | print_class_tree(inspect.getclasstree( 11 | [example.A, example.B, C, D], 12 | unique=True, 13 | )) 14 | -------------------------------------------------------------------------------- /source/inspect/inspect_getcomments_method.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Show the comment before a method. 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | import example 8 | 9 | print(inspect.getcomments(example.B.do_something)) 10 | -------------------------------------------------------------------------------- /source/inspect/inspect_getcomments_module.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Show the comment before a method. 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | import example 8 | 9 | print(inspect.getcomments(example)) 10 | -------------------------------------------------------------------------------- /source/inspect/inspect_getdoc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Docstrings 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | import example 8 | 9 | print('B.__doc__:') 10 | print(example.B.__doc__) 11 | print() 12 | print('getdoc(B):') 13 | print(inspect.getdoc(example.B)) 14 | -------------------------------------------------------------------------------- /source/inspect/inspect_getmembers_class.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using getmembers() 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | from pprint import pprint 8 | 9 | import example 10 | 11 | pprint(inspect.getmembers(example.A), width=65) 12 | -------------------------------------------------------------------------------- /source/inspect/inspect_getmembers_class_methods.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using getmembers() 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | from pprint import pprint 8 | 9 | import example 10 | 11 | pprint(inspect.getmembers(example.A, inspect.isfunction)) 12 | -------------------------------------------------------------------------------- /source/inspect/inspect_getmembers_class_methods_b.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using getmembers() 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | from pprint import pprint 8 | 9 | import example 10 | 11 | pprint(inspect.getmembers(example.B, inspect.isfunction)) 12 | -------------------------------------------------------------------------------- /source/inspect/inspect_getmembers_instance.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using getmembers() 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | from pprint import pprint 8 | 9 | import example 10 | 11 | a = example.A(name='inspect_getmembers') 12 | pprint(inspect.getmembers(a, inspect.ismethod)) 13 | -------------------------------------------------------------------------------- /source/inspect/inspect_getmembers_module.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using getmembers() 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | 8 | import example 9 | 10 | for name, data in inspect.getmembers(example): 11 | if name.startswith('__'): 12 | continue 13 | print('{} : {!r}'.format(name, data)) 14 | -------------------------------------------------------------------------------- /source/inspect/inspect_getmembers_module_class.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using getmembers() 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | 8 | import example 9 | 10 | for name, data in inspect.getmembers(example, inspect.isclass): 11 | print('{} : {!r}'.format(name, data)) 12 | -------------------------------------------------------------------------------- /source/inspect/inspect_getsource_class.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """ 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | import example 8 | 9 | print(inspect.getsource(example.A)) 10 | -------------------------------------------------------------------------------- /source/inspect/inspect_getsource_method.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """ 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | import example 8 | 9 | print(inspect.getsource(example.A.get_name)) 10 | -------------------------------------------------------------------------------- /source/inspect/inspect_getsourcelines_method.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """ 3 | """ 4 | 5 | #end_pymotw_header 6 | import inspect 7 | import pprint 8 | import example 9 | 10 | pprint.pprint(inspect.getsourcelines(example.A.get_name)) 11 | -------------------------------------------------------------------------------- /source/itertools/itertools_accumulate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """combine values 4 | """ 5 | 6 | #end_pymotw_header 7 | from itertools import * 8 | 9 | print(list(accumulate(range(5)))) 10 | print(list(accumulate('abcde'))) 11 | -------------------------------------------------------------------------------- /source/itertools/itertools_accumulate_custom.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | """combine values 4 | """ 5 | 6 | #end_pymotw_header 7 | from itertools import * 8 | 9 | 10 | def f(a, b): 11 | print(a, b) 12 | return b + a + b 13 | 14 | 15 | print(list(accumulate('abcde', f))) 16 | -------------------------------------------------------------------------------- /source/itertools/itertools_chain.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using chain() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | for i in chain([1, 2, 3], ['a', 'b', 'c']): 9 | print(i, end=' ') 10 | print() 11 | -------------------------------------------------------------------------------- /source/itertools/itertools_chain_from_iterable.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using chain() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | 9 | def make_iterables_to_chain(): 10 | yield [1, 2, 3] 11 | yield ['a', 'b', 'c'] 12 | 13 | 14 | for i in chain.from_iterable(make_iterables_to_chain()): 15 | print(i, end=' ') 16 | print() 17 | -------------------------------------------------------------------------------- /source/itertools/itertools_compress.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using filter() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | every_third = cycle([False, False, True]) 9 | data = range(1, 10) 10 | 11 | for i in compress(data, every_third): 12 | print(i, end=' ') 13 | print() 14 | -------------------------------------------------------------------------------- /source/itertools/itertools_count.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using count() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | for i in zip(count(1), ['a', 'b', 'c']): 9 | print(i) 10 | -------------------------------------------------------------------------------- /source/itertools/itertools_count_step.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using count() 3 | """ 4 | 5 | #end_pymotw_header 6 | import fractions 7 | from itertools import * 8 | 9 | start = fractions.Fraction(1, 3) 10 | step = fractions.Fraction(1, 3) 11 | 12 | for i in zip(count(start, step), ['a', 'b', 'c']): 13 | print('{}: {}'.format(*i)) 14 | -------------------------------------------------------------------------------- /source/itertools/itertools_cycle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using cycle(). 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | for i in zip(range(7), cycle(['a', 'b', 'c'])): 9 | print(i) 10 | -------------------------------------------------------------------------------- /source/itertools/itertools_dropwhile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using dropwhile() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | 9 | def should_drop(x): 10 | print('Testing:', x) 11 | return x < 1 12 | 13 | 14 | for i in dropwhile(should_drop, [-1, 0, 1, 2, -2]): 15 | print('Yielding:', i) 16 | -------------------------------------------------------------------------------- /source/itertools/itertools_filter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using filter() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | 9 | def check_item(x): 10 | print('Testing:', x) 11 | return x < 1 12 | 13 | 14 | for i in filter(check_item, [-1, 0, 1, 2, -2]): 15 | print('Yielding:', i) 16 | -------------------------------------------------------------------------------- /source/itertools/itertools_filterfalse.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using filterfalse() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | 9 | def check_item(x): 10 | print('Testing:', x) 11 | return x < 1 12 | 13 | 14 | for i in filterfalse(check_item, [-1, 0, 1, 2, -2]): 15 | print('Yielding:', i) 16 | -------------------------------------------------------------------------------- /source/itertools/itertools_groupby.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using groupby() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | from operator import itemgetter 8 | 9 | d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) 10 | di = sorted(d.iteritems(), key=itemgetter(1)) 11 | for k, g in groupby(di, key=itemgetter(1)): 12 | print(k, map(itemgetter(0), g)) 13 | -------------------------------------------------------------------------------- /source/itertools/itertools_repeat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using repeat() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | for i in repeat('over-and-over', 5): 9 | print(i) 10 | -------------------------------------------------------------------------------- /source/itertools/itertools_repeat_map.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using repeat() and map() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | for i in map(lambda x, y: (x, y, x * y), repeat(2), range(5)): 9 | print('{:d} * {:d} = {:d}'.format(*i)) 10 | -------------------------------------------------------------------------------- /source/itertools/itertools_repeat_zip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using repeat() with zip(). 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | for i, s in zip(count(), repeat('over-and-over', 5)): 9 | print(i, s) 10 | -------------------------------------------------------------------------------- /source/itertools/itertools_starmap.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using starmap() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | values = [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)] 9 | 10 | for i in starmap(lambda x, y: (x, y, x * y), values): 11 | print('{} * {} = {}'.format(*i)) 12 | -------------------------------------------------------------------------------- /source/itertools/itertools_takewhile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using takewhile() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | 9 | def should_take(x): 10 | print('Testing:', x) 11 | return x < 2 12 | 13 | 14 | for i in takewhile(should_take, [-1, 0, 1, 2, -2]): 15 | print('Yielding:', i) 16 | -------------------------------------------------------------------------------- /source/itertools/itertools_tee.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using tee() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | r = islice(count(), 5) 9 | i1, i2 = tee(r) 10 | 11 | print('i1:', list(i1)) 12 | print('i2:', list(i2)) 13 | -------------------------------------------------------------------------------- /source/itertools/itertools_tee_error.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using tee() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | r = islice(count(), 5) 9 | i1, i2 = tee(r) 10 | 11 | print('r:', end=' ') 12 | for i in r: 13 | print(i, end=' ') 14 | if i > 1: 15 | break 16 | print() 17 | 18 | print('i1:', list(i1)) 19 | print('i2:', list(i2)) 20 | -------------------------------------------------------------------------------- /source/itertools/itertools_zip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using zip() 3 | """ 4 | 5 | #end_pymotw_header 6 | for i in zip([1, 2, 3], ['a', 'b', 'c']): 7 | print(i) 8 | -------------------------------------------------------------------------------- /source/itertools/itertools_zip_longest.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | """Using zip() 3 | """ 4 | 5 | #end_pymotw_header 6 | from itertools import * 7 | 8 | r1 = range(3) 9 | r2 = range(2) 10 | 11 | print('zip stops early:') 12 | print(list(zip(r1, r2))) 13 | 14 | r1 = range(3) 15 | r2 = range(2) 16 | 17 | print('\nzip_longest processes all of the values:') 18 | print(list(zip_longest(r1, r2))) 19 | -------------------------------------------------------------------------------- /source/json/example.json: -------------------------------------------------------------------------------- 1 | [{"a": "A", "c": 3.0, "b": [2, 4]}] 2 | -------------------------------------------------------------------------------- /source/json/json_dump_file.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import io 11 | import json 12 | 13 | data = [{'a': 'A', 'b': (2, 4), 'c': 3.0}] 14 | 15 | f = io.StringIO() 16 | json.dump(data, f) 17 | 18 | print(f.getvalue()) 19 | -------------------------------------------------------------------------------- /source/json/json_encoder_iterable.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import json 11 | 12 | encoder = json.JSONEncoder() 13 | data = [{'a': 'A', 'b': (2, 4), 'c': 3.0}] 14 | 15 | for part in encoder.iterencode(data): 16 | print('PART:', part) 17 | -------------------------------------------------------------------------------- /source/json/json_indent.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import json 11 | 12 | data = [{'a': 'A', 'b': (2, 4), 'c': 3.0}] 13 | print('DATA:', repr(data)) 14 | 15 | print('NORMAL:', json.dumps(data, sort_keys=True)) 16 | print('INDENT:', json.dumps(data, sort_keys=True, indent=2)) 17 | -------------------------------------------------------------------------------- /source/json/json_load_file.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import io 11 | import json 12 | 13 | f = io.StringIO('[{"a": "A", "c": 3.0, "b": [2, 4]}]') 14 | print(json.load(f)) 15 | -------------------------------------------------------------------------------- /source/json/json_myobj.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | 11 | class MyObj: 12 | 13 | def __init__(self, s): 14 | self.s = s 15 | 16 | def __repr__(self): 17 | return 'This is a sample data file.
10 | 11 | 12 | -------------------------------------------------------------------------------- /source/pkgutil/run_dev.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #end_pymotw_header 3 | 4 | export PYTHONPATH=develop 5 | python3 pkgutil_devel.py 6 | -------------------------------------------------------------------------------- /source/pkgutil/with_os.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | export PYTHONPATH=os_${1} 4 | echo "PYTHONPATH=$PYTHONPATH" 5 | echo 6 | 7 | python3 pkgutil_os_specific.py 8 | -------------------------------------------------------------------------------- /source/platform/platform_architecture.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | 10 | #end_pymotw_header 11 | import platform 12 | 13 | print('interpreter:', platform.architecture()) 14 | print('/bin/ls :', platform.architecture('/bin/ls')) 15 | -------------------------------------------------------------------------------- /source/platform/platform_platform.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | 10 | #end_pymotw_header 11 | import platform 12 | 13 | print('Normal :', platform.platform()) 14 | print('Aliased:', platform.platform(aliased=True)) 15 | print('Terse :', platform.platform(terse=True)) 16 | -------------------------------------------------------------------------------- /source/pprint/pprint_compact.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Pretty print with pprint 6 | """ 7 | 8 | #end_pymotw_header 9 | from pprint import pprint 10 | 11 | from pprint_data import data 12 | 13 | print('DEFAULT:') 14 | pprint(data, compact=False) 15 | print('\nCOMPACT:') 16 | pprint(data, compact=True) 17 | -------------------------------------------------------------------------------- /source/pprint/pprint_depth.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Pretty print with pprint 6 | """ 7 | 8 | #end_pymotw_header 9 | from pprint import pprint 10 | 11 | from pprint_data import data 12 | 13 | pprint(data, depth=1) 14 | pprint(data, depth=2) 15 | -------------------------------------------------------------------------------- /source/pprint/pprint_pprint.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Pretty print with pprint 6 | """ 7 | 8 | #end_pymotw_header 9 | from pprint import pprint 10 | 11 | from pprint_data import data 12 | 13 | print('PRINT:') 14 | print(data) 15 | print() 16 | print('PPRINT:') 17 | pprint(data) 18 | -------------------------------------------------------------------------------- /source/pprint/pprint_recursion.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Handling recursive data structures. 6 | """ 7 | 8 | #end_pymotw_header 9 | from pprint import pprint 10 | 11 | local_data = ['a', 'b', 1, 2] 12 | local_data.append(local_data) 13 | 14 | print('id(local_data) =>', id(local_data)) 15 | pprint(local_data) 16 | -------------------------------------------------------------------------------- /source/pprint/pprint_width.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 3 | # Copyright 2007 Doug Hellmann. 4 | # 5 | """Pretty print with pprint 6 | """ 7 | 8 | #end_pymotw_header 9 | from pprint import pprint 10 | 11 | from pprint_data import data 12 | 13 | for width in [80, 5]: 14 | print('WIDTH =', width) 15 | pprint(data, width=width) 16 | print() 17 | -------------------------------------------------------------------------------- /source/profile/profile_runctx.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2008 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import profile 11 | from profile_fibonacci_memoized import fib, fib_seq 12 | 13 | if __name__ == '__main__': 14 | profile.runctx( 15 | 'print(fib_seq(n)); print()', 16 | globals(), 17 | {'n': 20}, 18 | ) 19 | -------------------------------------------------------------------------------- /source/profile/profile_stats_0.stats: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/profile/profile_stats_0.stats -------------------------------------------------------------------------------- /source/profile/profile_stats_1.stats: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/profile/profile_stats_1.stats -------------------------------------------------------------------------------- /source/profile/profile_stats_2.stats: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/profile/profile_stats_2.stats -------------------------------------------------------------------------------- /source/profile/profile_stats_3.stats: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/profile/profile_stats_3.stats -------------------------------------------------------------------------------- /source/profile/profile_stats_4.stats: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhellmann/pymotw-3/8c3750a62580da538b1056a5287fb806f0f51d0a/source/profile/profile_stats_4.stats -------------------------------------------------------------------------------- /source/pwd/pwd_getpwuid_process.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2009 Doug Hellmann All rights reserved. 5 | # 6 | """ 7 | """ 8 | 9 | #end_pymotw_header 10 | import pwd 11 | import os 12 | 13 | uid = os.getuid() 14 | user_info = pwd.getpwuid(uid) 15 | print('Currently running with UID={} username={}'.format( 16 | uid, user_info.pw_name)) 17 | -------------------------------------------------------------------------------- /source/queue/queue_fifo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """FIFO Queue 7 | """ 8 | 9 | #end_pymotw_header 10 | import queue 11 | 12 | q = queue.Queue() 13 | 14 | for i in range(5): 15 | q.put(i) 16 | 17 | while not q.empty(): 18 | print(q.get(), end=' ') 19 | print() 20 | -------------------------------------------------------------------------------- /source/queue/queue_lifo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """LIFO Queue 7 | """ 8 | 9 | #end_pymotw_header 10 | import queue 11 | 12 | q = queue.LifoQueue() 13 | 14 | for i in range(5): 15 | q.put(i) 16 | 17 | while not q.empty(): 18 | print(q.get(), end=' ') 19 | print() 20 | -------------------------------------------------------------------------------- /source/random/random_random.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Generate random numbers 7 | """ 8 | 9 | #end_pymotw_header 10 | import random 11 | 12 | for i in range(5): 13 | print('%04.3f' % random.random(), end=' ') 14 | print() 15 | -------------------------------------------------------------------------------- /source/random/random_randrange.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Random values from a range 7 | """ 8 | 9 | #end_pymotw_header 10 | import random 11 | 12 | for i in range(3): 13 | print(random.randrange(0, 101, 5), end=' ') 14 | print() 15 | -------------------------------------------------------------------------------- /source/random/random_sample.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Sampling from sequences 7 | """ 8 | 9 | #end_pymotw_header 10 | import random 11 | 12 | with open('/usr/share/dict/words', 'rt') as f: 13 | words = f.readlines() 14 | words = [w.rstrip() for w in words] 15 | 16 | for w in random.sample(words, 5): 17 | print(w) 18 | -------------------------------------------------------------------------------- /source/random/random_seed.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Generate random numbers 7 | """ 8 | 9 | #end_pymotw_header 10 | import random 11 | 12 | random.seed(1) 13 | 14 | for i in range(5): 15 | print('{:04.3f}'.format(random.random()), end=' ') 16 | print() 17 | -------------------------------------------------------------------------------- /source/random/random_uniform.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Generate random numbers 7 | """ 8 | 9 | #end_pymotw_header 10 | import random 11 | 12 | for i in range(5): 13 | print('{:04.3f}'.format(random.uniform(1, 100)), end=' ') 14 | print() 15 | -------------------------------------------------------------------------------- /source/re/re_charset_exclude.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Repetition of patterns 7 | """ 8 | 9 | #end_pymotw_header 10 | from re_test_patterns import test_patterns 11 | 12 | test_patterns( 13 | 'This is some text -- with punctuation.', 14 | [('[^-. ]+', 'sequences without -, ., or space')], 15 | ) 16 | -------------------------------------------------------------------------------- /source/re/re_escape_escapes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Escaping escape codes 7 | """ 8 | 9 | #end_pymotw_header 10 | from re_test_patterns import test_patterns 11 | 12 | test_patterns( 13 | r'\d+ \D+ \s+', 14 | [(r'\\.\+', 'escape code')], 15 | ) 16 | -------------------------------------------------------------------------------- /source/re/re_findall.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Repetition of patterns 7 | """ 8 | 9 | #end_pymotw_header 10 | import re 11 | 12 | text = 'abbaaabbbbaaaaa' 13 | 14 | pattern = 'ab' 15 | 16 | for match in re.findall(pattern, text): 17 | print('Found {!r}'.format(match)) 18 | -------------------------------------------------------------------------------- /source/re/re_groups_alternative.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Matching alternative groups 7 | """ 8 | 9 | #end_pymotw_header 10 | from re_test_patterns_groups import test_patterns 11 | 12 | test_patterns( 13 | 'abbaabbba', 14 | [(r'a((a+)|(b+))', 'a then seq. of a or seq. of b'), 15 | (r'a((a|b)+)', 'a then seq. of [ab]')], 16 | ) 17 | -------------------------------------------------------------------------------- /source/re/re_groups_nested.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Nested groups 7 | """ 8 | 9 | #end_pymotw_header 10 | from re_test_patterns_groups import test_patterns 11 | 12 | test_patterns( 13 | 'abbaabbba', 14 | [(r'a((a*)(b*))', 'a followed by 0-n a and 0-n b')], 15 | ) 16 | -------------------------------------------------------------------------------- /source/re/re_groups_noncapturing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Noncapturing groups 7 | """ 8 | 9 | #end_pymotw_header 10 | from re_test_patterns_groups import test_patterns 11 | 12 | test_patterns( 13 | 'abbaabbba', 14 | [(r'a((a+)|(b+))', 'capturing form'), 15 | (r'a((?:a+)|(?:b+))', 'noncapturing')], 16 | ) 17 | -------------------------------------------------------------------------------- /source/re/re_sub.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Substitute based on patterns. 7 | """ 8 | 9 | #end_pymotw_header 10 | import re 11 | 12 | bold = re.compile(r'\*{2}(.*?)\*{2}') 13 | 14 | text = 'Make this **bold**. This **too**.' 15 | 16 | print('Text:', text) 17 | print('Bold:', bold.sub(r'\1', text)) 18 | -------------------------------------------------------------------------------- /source/re/re_sub_count.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Substitute based on patterns. 7 | """ 8 | 9 | #end_pymotw_header 10 | import re 11 | 12 | bold = re.compile(r'\*{2}(.*?)\*{2}') 13 | 14 | text = 'Make this **bold**. This **too**.' 15 | 16 | print('Text:', text) 17 | print('Bold:', bold.sub(r'\1', text, count=1)) 18 | -------------------------------------------------------------------------------- /source/re/re_sub_named_groups.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # encoding: utf-8 3 | # 4 | # Copyright (c) 2010 Doug Hellmann. All rights reserved. 5 | # 6 | """Substitute based on patterns. 7 | """ 8 | 9 | #end_pymotw_header 10 | import re 11 | 12 | bold = re.compile(r'\*{2}(?P