├── .coveragerc ├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST.in ├── README.markdown ├── docs ├── Makefile ├── conf.py ├── index.txt ├── installation.txt ├── make.bat ├── misc │ ├── index.txt │ └── mailing-lists.txt ├── ref │ └── index.txt ├── theme │ ├── changes │ │ ├── frameset.html │ │ ├── rstsource.html │ │ └── versionchanges.html │ ├── defindex.html │ ├── genindex-single.html │ ├── genindex-split.html │ ├── genindex.html │ ├── layout.html │ ├── modindex.html │ ├── opensearch.xml │ ├── page.html │ ├── search.html │ ├── static │ │ ├── basic.css │ │ ├── default.css_t │ │ ├── doctools.js │ │ ├── file.png │ │ ├── jquery.js │ │ ├── minus.png │ │ ├── plus.png │ │ ├── searchtools.js │ │ └── sidebar.js │ └── theme.conf └── topics │ ├── abc-tools.txt │ ├── address-tools.txt │ ├── binary-search.txt │ ├── caching │ ├── cache.txt │ ├── cached_property.txt │ ├── cached_type.txt │ └── index.txt │ ├── change-tracker.txt │ ├── cheat-hashing.txt │ ├── color-tools.txt │ ├── combi.txt │ ├── comparison-tools.txt │ ├── context-management.txt │ ├── copy-mode.txt │ ├── copy-tools.txt │ ├── cute-iter-tools.txt │ ├── cute-profile.txt │ ├── cute-testing.txt │ ├── decorator-tools.txt │ ├── dict-tools.txt │ ├── emitting.txt │ ├── exceptions.txt │ ├── file-tools.txt │ ├── freezing.txt │ ├── function-anchoring-type.txt │ ├── gc-tools.txt │ ├── human-names.txt │ ├── identities.txt │ ├── import-tools.txt │ ├── index.txt │ ├── introspection-tools.txt │ ├── locking.txt │ ├── logic-tools.txt │ ├── math-tools.txt │ ├── misc-tools.txt │ ├── monkeypatching-tools.txt │ ├── nifty-collections.txt │ ├── os-tools.txt │ ├── path-tools.txt │ ├── pickle-tools.txt │ ├── process-priority.txt │ ├── queue-tools.txt │ ├── random-tools.txt │ ├── re-tools.txt │ ├── read-write-lock.txt │ ├── reasoned-bool.txt │ ├── rst-tools.txt │ ├── segment-tools.txt │ ├── sequence-tools.txt │ ├── sleek-reffing.txt │ ├── string-cataloging.txt │ ├── string-tools.txt │ ├── sys-tools.txt │ ├── temp-file-tools.txt │ ├── temp-value-setting.txt │ ├── third-party.txt │ ├── tracing-tools.txt │ ├── version-info.txt │ ├── wx-tools.txt │ └── zip-tools.txt ├── make_release.sh ├── misc ├── IDE files │ └── Wing │ │ └── python_toolbox.wpr └── __init__.py ├── python_toolbox ├── MIT_license.txt ├── __init__.py ├── abc_tools.py ├── address_tools │ ├── __init__.py │ ├── object_to_string.py │ ├── shared.py │ └── string_to_object.py ├── binary_search │ ├── __init__.py │ ├── binary_search_profile.py │ ├── functions.py │ └── roundings.py ├── caching │ ├── __init__.py │ ├── cached_property.py │ ├── cached_type.py │ └── decorators.py ├── change_tracker.py ├── cheat_hashing │ ├── __init__.py │ ├── cheat_hash.py │ └── cheat_hash_functions.py ├── color_tools.py ├── combi │ ├── __init__.py │ ├── chain_space.py │ ├── map_space.py │ ├── misc.py │ ├── perming │ │ ├── __init__.py │ │ ├── _fixed_map_managing_mixin.py │ │ ├── _variation_adding_mixin.py │ │ ├── _variation_removing_mixin.py │ │ ├── calculating_length.py │ │ ├── comb.py │ │ ├── comb_space.py │ │ ├── perm.py │ │ ├── perm_space.py │ │ └── variations.py │ ├── product_space.py │ └── selection_space.py ├── comparison_tools.py ├── context_management │ ├── __init__.py │ ├── abstract_context_manager.py │ ├── blank_context_manager.py │ ├── context_manager.py │ ├── context_manager_type.py │ ├── context_manager_type_type.py │ ├── delegating_context_manager.py │ ├── functions.py │ ├── mixins │ │ ├── __init__.py │ │ └── decorating_context_manager_mixin.py │ ├── modifiers.py │ └── self_hook.py ├── copy_mode.py ├── copy_tools.py ├── cute_enum.py ├── cute_iter_tools.py ├── cute_profile │ ├── __init__.py │ ├── base_profile.py │ ├── cute_profile.py │ ├── profile_handling.py │ └── pstats_troubleshooting.py ├── cute_testing.py ├── decorator_tools.py ├── dict_tools.py ├── emitting │ ├── __init__.py │ ├── emitter.py │ └── emitter_system │ │ ├── __init__.py │ │ ├── emitter.py │ │ └── emitter_system.py ├── exceptions.py ├── file_tools.py ├── freezing │ ├── __init__.py │ ├── delegatee_context_manager.py │ ├── freezer.py │ ├── freezer_property.py │ └── freezer_property_freezer.py ├── function_anchoring_type.py ├── future_tools.py ├── gc_tools.py ├── human_names │ ├── __init__.py │ └── _name_list.py ├── import_tools.py ├── introspection_tools.py ├── locking │ ├── __init__.py │ ├── original_read_write_lock.py │ └── read_write_lock.py ├── logic_tools.py ├── math_tools │ ├── __init__.py │ ├── factorials.py │ ├── misc.py │ ├── sequences.py │ ├── statistics.py │ └── types.py ├── misc_tools │ ├── __init__.py │ ├── misc_tools.py │ ├── name_mangling.py │ ├── overridable_property.py │ └── proxy_property.py ├── monkeypatch_copyreg.py ├── monkeypatch_envelopes.py ├── monkeypatching_tools.py ├── nifty_collections │ ├── __init__.py │ ├── abstract.py │ ├── bagging.py │ ├── emitting_weak_key_default_dict.py │ ├── frozen_bag_bag.py │ ├── lazy_tuple.py │ ├── ordered_dict.py │ ├── various_frozen_dicts.py │ ├── various_ordered_sets.py │ ├── weak_key_default_dict.py │ └── weak_key_identity_dict.py ├── number_encoding.py ├── os_tools.py ├── path_tools.py ├── pickle_tools.py ├── process_priority.py ├── queue_tools.py ├── random_tools.py ├── re_tools.py ├── reasoned_bool.py ├── rst_tools.py ├── segment_tools.py ├── sequence_tools │ ├── __init__.py │ ├── canonical_slice.py │ ├── cute_range.py │ └── misc.py ├── sleek_reffing │ ├── __init__.py │ ├── cute_sleek_value_dict.py │ ├── exceptions.py │ ├── sleek_call_args.py │ └── sleek_ref.py ├── string_cataloging.py ├── string_tools │ ├── __init__.py │ ├── case_conversions.py │ └── string_tools.py ├── sys_tools.py ├── temp_file_tools.py ├── temp_value_setting │ ├── __init__.py │ ├── temp_import_hook_setter.py │ ├── temp_recursion_limit_setter.py │ ├── temp_value_setter.py │ └── temp_working_directory_setter.py ├── third_party │ ├── __init__.py │ ├── decorator.py │ ├── envelopes │ │ ├── __init__.py │ │ ├── compat.py │ │ ├── conn.py │ │ ├── connstack.py │ │ ├── envelope.py │ │ └── local.py │ └── sortedcontainers │ │ ├── __init__.py │ │ ├── sorteddict.py │ │ ├── sortedlist.py │ │ └── sortedset.py ├── tracing_tools │ ├── __init__.py │ ├── count_calls.py │ └── temp_function_call_counter.py ├── version_info.py ├── wx_tools │ ├── __init__.py │ ├── bitmap_tools.py │ ├── colors.py │ ├── cursors │ │ ├── __init__.py │ │ ├── collection │ │ │ ├── __init__.py │ │ │ ├── collection.py │ │ │ └── images │ │ │ │ ├── __init__.py │ │ │ │ ├── closed_grab.png │ │ │ │ └── open_grab.png │ │ └── cursor_changer.py │ ├── drawing_tools │ │ ├── __init__.py │ │ └── pens.py │ ├── event_tools.py │ ├── generic_bitmaps.py │ ├── keyboard │ │ ├── __init__.py │ │ ├── key.py │ │ └── keys │ │ │ ├── __init__.py │ │ │ ├── global_keys.py │ │ │ ├── gtk_keys.py │ │ │ ├── mac_keys.py │ │ │ └── win_keys.py │ ├── timing │ │ ├── __init__.py │ │ ├── cute_base_timer.py │ │ └── thread_timer.py │ ├── widgets │ │ ├── __init__.py │ │ ├── cute_bitmap_button.py │ │ ├── cute_button.py │ │ ├── cute_control.py │ │ ├── cute_dialog.py │ │ ├── cute_dialog_type.py │ │ ├── cute_dir_dialog.py │ │ ├── cute_error_dialog.py │ │ ├── cute_file_dialog.py │ │ ├── cute_frame.py │ │ ├── cute_hidden_button.py │ │ ├── cute_html_window.py │ │ ├── cute_hyper_tree_list.py │ │ ├── cute_hyperlink_ctrl.py │ │ ├── cute_message_dialog.py │ │ ├── cute_panel.py │ │ ├── cute_scrolled_panel.py │ │ ├── cute_static_text.py │ │ ├── cute_top_level_window.py │ │ ├── cute_tree_ctrl.py │ │ ├── cute_window │ │ │ ├── __init__.py │ │ │ ├── accelerator_savvy_window.py │ │ │ ├── bind_savvy_evt_handler │ │ │ │ ├── __init__.py │ │ │ │ ├── bind_savvy_evt_handler.py │ │ │ │ ├── bind_savvy_evt_handler_type.py │ │ │ │ ├── event_codes.py │ │ │ │ ├── event_handler_grokker.py │ │ │ │ └── name_parser.py │ │ │ └── cute_window.py │ │ ├── hue_control.py │ │ ├── hue_selection_dialog │ │ │ ├── __init__.py │ │ │ ├── comparer.py │ │ │ ├── hue_selection_dialog.py │ │ │ ├── textual.py │ │ │ └── wheel.py │ │ ├── knob │ │ │ ├── __init__.py │ │ │ ├── images │ │ │ │ ├── __init__.py │ │ │ │ └── knob.png │ │ │ ├── knob.py │ │ │ └── snap_map.py │ │ └── third_party │ │ │ ├── __init__.py │ │ │ ├── customtreectrl.py │ │ │ └── hypertreelist.py │ └── window_tools.py └── zip_tools.py ├── setup.py ├── test_python_toolbox ├── __init__.py ├── scripts │ ├── __init__.py │ └── _test_python_toolbox.py ├── test_abc_tools │ ├── __init__.py │ └── test_abstract_static_method.py ├── test_address_tools │ ├── __init__.py │ ├── sample_module_tree │ │ ├── __init__.py │ │ └── w │ │ │ ├── __init__.py │ │ │ └── x │ │ │ ├── __init__.py │ │ │ └── y │ │ │ ├── __init__.py │ │ │ └── z │ │ │ └── __init__.py │ ├── test_describe.py │ └── test_resolve.py ├── test_binary_search │ ├── __init__.py │ └── test.py ├── test_caching │ ├── __init__.py │ ├── test_cache.py │ ├── test_cached_property.py │ └── test_cached_type.py ├── test_cheat_hashing.py ├── test_color_tools │ ├── __init__.py │ └── test.py ├── test_combi │ ├── __init__.py │ ├── test_calculating_length.py │ ├── test_chain_space.py │ ├── test_comb_space.py │ ├── test_extensive.py │ ├── test_misc.py │ ├── test_perm_space.py │ ├── test_product_space.py │ ├── test_selection_space.py │ └── test_variations_meta.py ├── test_context_management │ ├── __init__.py │ ├── test_abstractness.py │ ├── test_as_idempotent.py │ ├── test_as_reentrant.py │ ├── test_context_manager.py │ ├── test_external.py │ ├── test_nested.py │ └── test_problematic_context_managers.py ├── test_cute_iter_tools │ ├── __init__.py │ ├── test_call_until_exception.py │ ├── test_double_filter.py │ ├── test_enumerate.py │ ├── test_fill.py │ ├── test_get_items.py │ ├── test_get_length.py │ ├── test_get_ratio.py │ ├── test_get_single_if_any.py │ ├── test_is_iterable.py │ ├── test_is_sorted.py │ ├── test_iter_with.py │ ├── test_iterate_overlapping_subsequences.py │ ├── test_pop_iterators.py │ ├── test_pushback_iterator.py │ └── test_shorten.py ├── test_cute_profile │ ├── __init__.py │ ├── shared.py │ └── test_cute_profile.py ├── test_cute_testing │ ├── __init__.py │ ├── test_assert_same_signature.py │ └── test_raise_assertor.py ├── test_dict_tools │ ├── __init__.py │ ├── test_devour_items.py │ ├── test_devour_keys.py │ ├── test_get_sorted_values.py │ ├── test_remove_keys.py │ ├── test_reverse.py │ └── test_sum_dicts.py ├── test_emitting │ ├── __init__.py │ └── test_emitter.py ├── test_exceptions │ ├── __init__.py │ ├── test_cute_base_exception.py │ └── test_cute_exception.py ├── test_file_tools │ ├── __init__.py │ ├── test_atomic.py │ └── test_renaming.py ├── test_freezing │ ├── __init__.py │ ├── test_freezer.py │ └── test_freezer_property.py ├── test_future_tools │ ├── __init__.py │ └── test_future_tools.py ├── test_human_names.py ├── test_import_tools │ ├── __init__.py │ └── test_exists │ │ ├── __init__.py │ │ ├── test.py │ │ └── test_zip.py ├── test_introspection_tools │ ├── __init__.py │ └── test_get_default_args_dict.py ├── test_logic_tools │ ├── __init__.py │ ├── test_all_equivalent.py │ ├── test_get_equivalence_classes.py │ └── test_logic_max.py ├── test_math_tools │ ├── __init__.py │ ├── test_binomial.py │ ├── test_convert_to_base_in_tuple.py │ ├── test_cute_floor_div_and_divmod.py │ ├── test_cute_round.py │ ├── test_factorials.py │ ├── test_get_mean.py │ ├── test_get_median.py │ ├── test_restrict_number_to_range.py │ ├── test_sequences.py │ └── test_types.py ├── test_misc_tools │ ├── __init__.py │ ├── test_add_extension_if_plain.py │ ├── test_decimal_number_from_string.py │ ├── test_find_clear_place_on_circle.py │ ├── test_general_product.py │ ├── test_general_sum.py │ ├── test_get_mro_depth_of_method.py │ ├── test_is_legal_variable_name.py │ ├── test_is_magic_variable_name.py │ ├── test_is_subclass.py │ ├── test_name_mangling │ │ ├── __init__.py │ │ ├── test_demangling.py │ │ └── test_repeat_getattr.py │ ├── test_non_instantiable.py │ ├── test_overridable_property.py │ └── test_rotating_log_stream.py ├── test_monkeypatching_tools │ ├── test_change_defaults.py │ └── test_monkeypatch.py ├── test_nifty_collections │ ├── __init__.py │ ├── test_bagging.py │ ├── test_cute_enum │ │ ├── __init__.py │ │ └── test.py │ ├── test_frozen_dict.py │ ├── test_frozen_ordered_dict.py │ ├── test_lazy_tuple │ │ ├── __init__.py │ │ └── test_lazy_tuple.py │ ├── test_ordered_and_definitely_unordered.py │ ├── test_ordered_dict │ │ ├── __init__.py │ │ ├── test.py │ │ └── test_with_stdlib_ordered_dict.py │ ├── test_various_ordered_sets.py │ ├── test_weak_key_default_dict │ │ ├── __init__.py │ │ └── test.py │ └── test_weak_key_identity_dict │ │ ├── __init__.py │ │ ├── test.py │ │ └── test_generic.py ├── test_number_encoding │ ├── __init__.py │ └── test_number_encoding.py ├── test_path_tools │ ├── __init__.py │ └── test_get_root_path_of_module.py ├── test_pickle_tools │ ├── __init__.py │ └── test_compressing.py ├── test_proxy_property.py ├── test_queue_tools │ ├── __init__.py │ └── test_iterate.py ├── test_random_tools │ ├── __init__.py │ ├── test_random_partitions.py │ └── test_shuffled.py ├── test_re_tools.py ├── test_read_write_lock │ ├── __init__.py │ └── test.py ├── test_reasoned_bool.py ├── test_rst_tools │ ├── __init__.py │ └── test.py ├── test_segment_tools │ ├── __init__.py │ ├── test_crop_segment.py │ └── test_merge_segments.py ├── test_sequence_tools │ ├── __init__.py │ ├── test_canonical_slice.py │ ├── test_cute_range.py │ ├── test_divide_to_slices.py │ ├── test_flatten.py │ ├── test_get_recurrences.py │ ├── test_is_subsequence.py │ ├── test_partitions.py │ ├── test_pop_until.py │ └── test_to_tuple.py ├── test_sleek_reffing │ ├── __init__.py │ ├── shared.py │ ├── test_cute_sleek_value_dict │ │ ├── __init__.py │ │ ├── test_generic_dict_tests.py │ │ └── tests.py │ ├── test_sleek_call_args.py │ └── test_sleek_ref.py ├── test_string_cataloging │ ├── __init__.py │ └── test.py ├── test_string_tools │ ├── __init__.py │ ├── test_case_conversions.py │ ├── test_get_n_identical_edge_characters.py │ └── test_rreplace.py ├── test_sys_tools │ ├── __init__.py │ ├── test_output_capturer.py │ └── test_temp_sys_path_adder.py ├── test_temp_file_tools │ ├── __init__.py │ └── test_create_temp_folder.py ├── test_temp_value_setting │ ├── __init__.py │ ├── test_recursion_limit_setter.py │ ├── test_temp_value_setter.py │ └── test_temp_working_directory_setter.py ├── test_tracing_tools │ ├── __init__.py │ └── test.py ├── test_version_info.py ├── test_zip_tools │ ├── __init__.py │ ├── test_zip_folder.py │ └── test_zipping_in_memory.py └── third_party │ ├── __init__.py │ └── forked_mapping_tests.py └── tox.ini /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | branch = True 3 | source = 4 | python_toolbox 5 | 6 | [report] 7 | # Gotta ignore errors because I create temporary code files: 8 | ignore_errors = True 9 | 10 | omit = 11 | *third_party* 12 | 13 | [html] 14 | directory = .coverage_html_report -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.pyo 3 | 4 | *.wpu 5 | 6 | */docs/_build/* 7 | */docs/_deployed_build/* 8 | 9 | dist 10 | *.egg-info 11 | build 12 | *.exe 13 | 14 | nosetests.xml 15 | 16 | coverage.xml 17 | *.coverage 18 | .coverage_html_report 19 | 20 | _build 21 | 22 | __pycache__ 23 | 24 | .pylint -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | language: python 3 | 4 | python: 5 | - 3.10 6 | - 3.11 7 | - 3.12 8 | 9 | install: 10 | - pip install tox-travis 11 | script: 12 | - tox 13 | 14 | stages: 15 | #- lint 16 | - test 17 | #- deploy 18 | 19 | matrix: 20 | allow_failures: 21 | - env: TOXENV=flake8 22 | - env: TOXENV=pylint 23 | - env: TOXENV=bandit 24 | 25 | jobs: 26 | include: 27 | #- { stage: lint, python: 3.7, env: TOXENV=flake8 } 28 | #- { stage: lint, python: 3.7, env: TOXENV=pylint } 29 | #- { stage: lint, python: 3.7, env: TOXENV=bandit } 30 | #- { stage: lint, python: 3.7, env: TOXENV=readme } 31 | 32 | #- stage: deploy 33 | # install: skip 34 | # script: skip 35 | # deploy: 36 | # provider: pypi 37 | # distributions: sdist bdist_wheel 38 | # user: cool-RR 39 | # password: 40 | # secure: 41 | # on: 42 | # tags: true 43 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include python_toolbox * 2 | recursive-exclude python_toolbox *.pyc 3 | recursive-exclude python_toolbox *.pyo 4 | recursive-include test_python_toolbox * 5 | recursive-exclude test_python_toolbox *.pyc 6 | recursive-exclude test_python_toolbox *.pyo 7 | include README.markdown 8 | include MANIFEST.in 9 | include .coveragerc 10 | include setup.cfg 11 | recursive-include docs * 12 | -------------------------------------------------------------------------------- /docs/index.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _index: 8 | 9 | ======================= 10 | The Python Toolbox Documentation 11 | ======================= 12 | 13 | Contents: 14 | 15 | .. toctree:: 16 | :maxdepth: 2 17 | 18 | topics/index 19 | misc/index 20 | 21 | .. 22 | ref/index 23 | 24 | This documentation is still incomplete. If you have any questions or feedback, 25 | say hello on the `mailing list`_! 26 | 27 | ------------------------------------------------------------------------------- 28 | 29 | Python Toolbox on GitHub: https://github.com/cool-RR/python_toolbox 30 | 31 | Python Toolbox on PyPI: https://pypi.python.org/pypi/python_toolbox 32 | 33 | Feel free to fork and send pull requests! 34 | 35 | ------------------------------------------------------------------------------- 36 | 37 | The Python Toolbox was created by Ram Rachum. I provide 38 | `Development services in Python and Django `_. 39 | 40 | 41 | 42 | .. _mailing list: https://groups.google.com/forum/#!forum/python-toolbox 43 | 44 | .. * :ref:`genindex` 45 | .. * :ref:`modindex` 46 | -------------------------------------------------------------------------------- /docs/installation.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _installation: 8 | 9 | ============================= 10 | Installing the Python Toolbox 11 | ============================= 12 | 13 | Requirements 14 | ============ 15 | 16 | * Python, version 2.7 or 3.3 or above. If you're new to Python, `download 17 | version 2.7 `_. 18 | 19 | * `Setuptools`_. 20 | 21 | 22 | Installation 23 | ============ 24 | 25 | The simplest way to install the Python Toolbox is using `pip`_: 26 | 27 | * ``pip install python_toolbox`` 28 | 29 | .. _pip: http://pypi.python.org/pypi/pip 30 | .. _distribute: http://pypi.python.org/pypi/distribute -------------------------------------------------------------------------------- /docs/misc/index.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _misc-index: 8 | 9 | ==================== 10 | Miscellaneous topics 11 | ==================== 12 | 13 | .. toctree:: 14 | :maxdepth: 1 15 | 16 | mailing-lists 17 | -------------------------------------------------------------------------------- /docs/misc/mailing-lists.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _misc-mailing-lists: 8 | 9 | ============= 10 | Mailing Lists 11 | ============= 12 | 13 | There are three Python Toolbox groups, a.k.a. mailing lists: 14 | 15 | - If you need help with Python Toolbox, post a message on `the python-toolbox 16 | Google Group `_. 17 | 18 | - If you want to help on the development of Python Toolbox itself, come say 19 | hello on `the python-toolbox-dev Google Group 20 | `_. 21 | 22 | - If you want to be informed on new releases of the Python Toolbox, sign up for 23 | `the low-traffic python-toolbox-announce Google Group 24 | `_. 25 | -------------------------------------------------------------------------------- /docs/ref/index.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _ref-index: 8 | 9 | Reference to the Python Toolbox 10 | =============================== 11 | 12 | (This section is still incomplete.) 13 | 14 | This section contains reference-style documentation for the various modules in 15 | the Python Toolbox. 16 | 17 | It focuses on giving technical explanations of the various 18 | functions and classes, *without* giving the motivation for using them or a 19 | bigger-picture view of why they were written and what they are good for. For 20 | those, :ref:`check out the topical guides `. 21 | 22 | .. toctree:: 23 | :maxdepth: 1 24 | 25 | 26 | -------------------------------------------------------------------------------- /docs/theme/changes/frameset.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | {% trans version=version|e, docstitle=docstitle|e %}Changes in Version {{ version }} — {{ docstitle }}{% endtrans %} 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /docs/theme/changes/rstsource.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | {% trans filename=filename, docstitle=docstitle|e %}{{ filename }} — {{ docstitle }}{% endtrans %} 6 | 9 | 10 | 11 |
12 |       {{ text }}
13 |     
14 | 15 | 16 | -------------------------------------------------------------------------------- /docs/theme/changes/versionchanges.html: -------------------------------------------------------------------------------- 1 | {% macro entries(changes) %} 2 |
    {% for entry, docname, lineno in changes %} 3 |
  • {{ entry }}
  • 4 | {% endfor %}
5 | {% endmacro -%} 6 | 8 | 9 | 10 | 11 | 12 | {% trans version=version|e, docstitle=docstitle|e %}Changes in Version {{ version }} — {{ docstitle }}{% endtrans %} 13 | 14 | 15 |
16 |
17 |

{% trans version=version|e %}Automatically generated list of changes in version {{ version }}{% endtrans %}

18 |

{{ _('Library changes') }}

19 | {% for modname, changes in libchanges %} 20 |

{{ modname }}

21 | {{ entries(changes) }} 22 | {% endfor %} 23 |

{{ _('C API changes') }}

24 | {{ entries(apichanges) }} 25 |

{{ _('Other changes') }}

26 | {% for (fn, title), changes in otherchanges %} 27 |

{{ title }} ({{ fn }})

28 | {{ entries(changes) }} 29 | {% endfor %} 30 |
31 |
32 | 33 | 34 | -------------------------------------------------------------------------------- /docs/theme/defindex.html: -------------------------------------------------------------------------------- 1 | {% extends "layout.html" %} 2 | {% set title = _('Overview') %} 3 | {% block body %} 4 |

{{ docstitle|e }}

5 |

6 | Welcome! This is 7 | {% block description %}the documentation for {{ project|e }} 8 | {{ release|e }}{% if last_updated %}, last updated {{ last_updated|e }}{% endif %}{% endblock %}. 9 |

10 | {% block tables %} 11 |

{{ _('Indices and tables:') }}

12 | 13 | 24 |
14 | 16 | 18 | 19 | 21 | 23 |
25 | {% endblock %} 26 | {% endblock %} 27 | -------------------------------------------------------------------------------- /docs/theme/genindex-split.html: -------------------------------------------------------------------------------- 1 | {% extends "layout.html" %} 2 | {% set title = _('Index') %} 3 | {% block body %} 4 | 5 |

{{ _('Index') }}

6 | 7 |

{{ _('Index pages by letter') }}:

8 | 9 |

{% for key, dummy in genindexentries -%} 10 | {{ key }} 11 | {% if not loop.last %}| {% endif %} 12 | {%- endfor %}

13 | 14 |

{{ _('Full index on one page') }} 15 | ({{ _('can be huge') }})

16 | 17 | {% endblock %} 18 | 19 | {% block sidebarrel %} 20 | {% if split_index %} 21 |

Index

22 |

{% for key, dummy in genindexentries -%} 23 | {{ key }} 24 | {% if not loop.last %}| {% endif %} 25 | {%- endfor %}

26 | 27 |

{{ _('Full index on one page') }}

28 | {% endif %} 29 | {{ super() }} 30 | {% endblock %} 31 | -------------------------------------------------------------------------------- /docs/theme/opensearch.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{ project|e }} 4 | {% trans docstitle=docstitle|e %}Search {{ docstitle }}{% endtrans %} 5 | utf-8 6 | 8 | {{ docstitle|e }} 9 | {% block extra %} {# Put e.g. an element here. #} {% endblock %} 10 | 11 | -------------------------------------------------------------------------------- /docs/theme/page.html: -------------------------------------------------------------------------------- 1 | {% extends "layout.html" %} 2 | {% block body %} 3 | {{ body }} 4 | {% endblock %} 5 | -------------------------------------------------------------------------------- /docs/theme/static/file.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/docs/theme/static/file.png -------------------------------------------------------------------------------- /docs/theme/static/minus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/docs/theme/static/minus.png -------------------------------------------------------------------------------- /docs/theme/static/plus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/docs/theme/static/plus.png -------------------------------------------------------------------------------- /docs/theme/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = none 3 | stylesheet = default.css 4 | pygments_style = friendly 5 | 6 | [options] 7 | rightsidebar = false 8 | stickysidebar = false 9 | 10 | -------------------------------------------------------------------------------- /docs/topics/abc-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-abc-tools: 8 | 9 | :mod:`abc_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/binary-search.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-binary-search: 8 | 9 | :mod:`binary_search` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/caching/index.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-caching-index: 8 | 9 | :mod:`caching` 10 | ============= 11 | 12 | The :mod:`caching` modules provides tools related to caching: 13 | 14 | .. toctree:: 15 | :maxdepth: 3 16 | 17 | cache 18 | cached_type 19 | cached_property 20 | -------------------------------------------------------------------------------- /docs/topics/change-tracker.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-change-tracker: 8 | 9 | :mod:`change_tracker` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/cheat-hashing.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-cheat-hashing: 8 | 9 | :mod:`cheat_hashing` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/color-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-color-tools: 8 | 9 | :mod:`color_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/combi.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-combi: 8 | 9 | :mod:`combi` - Documentation on Combi site 10 | ========================================== 11 | 12 | `Please go to Combi's documentation here `_\ . 13 | -------------------------------------------------------------------------------- /docs/topics/comparison-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-comparison-tools: 8 | 9 | :mod:`comparison_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/copy-mode.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-copy-mode: 8 | 9 | :mod:`copy_mode` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/copy-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-copy-tools: 8 | 9 | :mod:`copy_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/cute-iter-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-cute-iter-tools: 8 | 9 | :mod:`cute_iter_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/cute-testing.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-cute-testing: 8 | 9 | :mod:`cute_testing` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/decorator-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-decorator-tools: 8 | 9 | :mod:`decorator_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/dict-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-dict-tools: 8 | 9 | :mod:`dict_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/emitting.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-emitting: 8 | 9 | :mod:`emitting` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/exceptions.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-exceptions: 8 | 9 | :mod:`exceptions` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/file-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-file-tools: 8 | 9 | :mod:`file_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/freezing.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-freezing: 8 | 9 | :mod:`freezing` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/function-anchoring-type.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-function-anchoring-type: 8 | 9 | :mod:`function_anchoring_type` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/gc-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-gc-tools: 8 | 9 | :mod:`gc_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/human-names.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-human-names: 8 | 9 | :mod:`human_names` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/identities.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-identities: 8 | 9 | :mod:`identities` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/import-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-import-tools: 8 | 9 | :mod:`import_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/introspection-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-introspection-tools: 8 | 9 | :mod:`introspection_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/locking.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-locking: 8 | 9 | :mod:`locking` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/logic-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-logic-tools: 8 | 9 | :mod:`logic_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/math-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-math-tools: 8 | 9 | :mod:`math_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/misc-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-misc-tools: 8 | 9 | :mod:`misc_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/monkeypatching-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-monkeypatching-tools: 8 | 9 | :mod:`monkeypatching_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/nifty-collections.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-nifty-collections: 8 | 9 | :mod:`nifty_collections` - documentation not written 10 | ==================================================== 11 | -------------------------------------------------------------------------------- /docs/topics/os-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-os-tools: 8 | 9 | :mod:`os_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/path-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-path-tools: 8 | 9 | :mod:`path_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/pickle-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-pickle-tools: 8 | 9 | :mod:`pickle_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/process-priority.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-process-priority: 8 | 9 | :mod:`process_priority` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/queue-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-queue-tools: 8 | 9 | :mod:`queue_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/random-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-random-tools: 8 | 9 | :mod:`random_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/re-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-re-tools: 8 | 9 | :mod:`re_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/read-write-lock.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-locking: 8 | 9 | :mod:`locking` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/reasoned-bool.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-reasoned-bool: 8 | 9 | :mod:`reasoned_bool` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/rst-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-rst-tools: 8 | 9 | :mod:`rst_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/segment-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-segment-tools: 8 | 9 | :mod:`segment_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/sequence-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-sequence-tools: 8 | 9 | :mod:`sequence_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/sleek-reffing.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-sleek-reffing: 8 | 9 | :mod:`sleek_reffind` - documentation not written 10 | ================================================ -------------------------------------------------------------------------------- /docs/topics/string-cataloging.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-string-cataloging: 8 | 9 | :mod:`string_cataloging` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/string-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-string-tools: 8 | 9 | :mod:`string_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/sys-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-sys-tools: 8 | 9 | :mod:`sys_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/temp-file-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-temp-file-tools: 8 | 9 | :mod:`temp_file_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/temp-value-setting.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-temp-value-setting: 8 | 9 | :mod:`temp_value_setting` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/third-party.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-third-party: 8 | 9 | :mod:`third_party` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/tracing-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-tracing-tools: 8 | 9 | :mod:`tracing_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/version-info.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-version-info: 8 | 9 | :mod:`version_info` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/wx-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-wx-tools: 8 | 9 | :mod:`wx_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /docs/topics/zip-tools.txt: -------------------------------------------------------------------------------- 1 | .. 2 | Copyright 2009-2017 Ram Rachum. This work is licensed under a Creative 3 | Commons Attribution-ShareAlike 3.0 Unported License, with attribution to 4 | "Ram Rachum at ram.rachum.com" including link. The license may be obtained 5 | at http://creativecommons.org/licenses/by-sa/3.0/ 6 | 7 | .. _topics-zip-tools: 8 | 9 | :mod:`zip_tools` - documentation not written 10 | ====================================== 11 | -------------------------------------------------------------------------------- /make_release.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | rm -rf dist/* build/* && python setup.py bdist_wheel --universal && twine upload dist/* -------------------------------------------------------------------------------- /misc/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Miscellaenous stuff.''' 5 | 6 | -------------------------------------------------------------------------------- /python_toolbox/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Python Toolbox is a collection of Python tools. 6 | 7 | These tools include caching, context manager tools, data structures, binary 8 | search, import tools, tools for manipulating Python's built-in types, and many 9 | more. 10 | 11 | Visit http://pypi.python.org/pypi/python_toolbox/ for more info. 12 | ''' 13 | 14 | import python_toolbox.version_info 15 | 16 | __version__ = '1.2.3' 17 | __version_info__ = python_toolbox.version_info.VersionInfo( 18 | *(map(int, __version__.split('.'))) 19 | ) 20 | -------------------------------------------------------------------------------- /python_toolbox/abc_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines tools related to abstract base classes from the `abc` module.''' 5 | 6 | 7 | class AbstractStaticMethod(staticmethod): 8 | ''' 9 | A combination of `abc.abstractmethod` and `staticmethod`. 10 | 11 | A method which (a) doesn't take a `self` argument and (b) must be 12 | overridden in any subclass if you want that subclass to be instanciable. 13 | 14 | This class is good only for documentation; it doesn't enforce overriding 15 | methods to be static. 16 | ''' 17 | __slots__ = () 18 | __isabstractmethod__ = True 19 | 20 | def __init__(self, function): 21 | super().__init__(function) 22 | function.__isabstractmethod__ = True 23 | -------------------------------------------------------------------------------- /python_toolbox/address_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | r''' 5 | Module for manipulating Python addresses. 6 | 7 | Use `resolve` to turn a string description into an object, and `describe` to 8 | turn an object into a string. 9 | 10 | For example: 11 | 12 | >>> address_tools.describe(list) 13 | 'list' 14 | >>> address_tools.resolve('list') 15 | 16 | >>> address_tools.describe([1, 2, {3: 4}]) 17 | '[1, 2, {3: 4}]' 18 | >>> address_tools.resolve('{email.encoders: 1}') 19 | {: 1} 20 | 21 | ''' 22 | 23 | 24 | from .string_to_object import resolve 25 | from .object_to_string import describe 26 | from .shared import is_address -------------------------------------------------------------------------------- /python_toolbox/address_tools/shared.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Various objects and tools for `address_tools`.''' 5 | 6 | import re 7 | 8 | 9 | _address_pattern = re.compile( 10 | r"^(?P
([a-zA-Z_][0-9a-zA-Z_]*)(\.[a-zA-Z_][0-9a-zA-Z_]*)*)$" 11 | ) 12 | '''Pattern for Python addresses, like 'email.encoders'.''' 13 | 14 | 15 | _contained_address_pattern = re.compile( 16 | r"(?P
([a-zA-Z_][0-9a-zA-Z_]*)(\.[a-zA-Z_][0-9a-zA-Z_]*)*)" 17 | ) 18 | ''' 19 | Pattern for strings containing Python addresses, like '{email.encoders: 1}'. 20 | ''' 21 | 22 | 23 | def _get_parent_and_dict_from_namespace(namespace): 24 | ''' 25 | Extract the parent object and `dict` from `namespace`. 26 | 27 | For the `namespace`, the user can give either a parent object 28 | (`getattr(namespace, address) is obj`) or a `dict`-like namespace 29 | (`namespace[address] is obj`). 30 | 31 | Returns `(parent_object, namespace_dict)`. 32 | ''' 33 | 34 | if hasattr(namespace, '__getitem__') and hasattr(namespace, 'keys'): 35 | parent_object = None 36 | namespace_dict = namespace 37 | 38 | else: 39 | parent_object = namespace 40 | namespace_dict = vars(parent_object) 41 | 42 | return (parent_object, namespace_dict) 43 | 44 | 45 | def is_address(string): 46 | return bool(_address_pattern.match(string)) -------------------------------------------------------------------------------- /python_toolbox/binary_search/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''A package for doing a binary search in a sequence.''' 5 | 6 | from .roundings import (Rounding, LOW, LOW_IF_BOTH, LOW_OTHERWISE_HIGH, HIGH, 7 | HIGH_IF_BOTH, HIGH_OTHERWISE_LOW, EXACT, CLOSEST, 8 | CLOSEST_IF_BOTH, BOTH) 9 | from .functions import (binary_search, binary_search_by_index, 10 | make_both_data_into_preferred_rounding) 11 | from .binary_search_profile import BinarySearchProfile -------------------------------------------------------------------------------- /python_toolbox/caching/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various caching tools.''' 5 | 6 | # todo: examine thread-safety 7 | 8 | from .decorators import cache 9 | from .cached_type import CachedType 10 | from .cached_property import CachedProperty -------------------------------------------------------------------------------- /python_toolbox/cheat_hashing/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `cheat_hash` function for cheat-hashing mutable objects. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | from . import cheat_hash_functions 11 | from .cheat_hash import cheat_hash -------------------------------------------------------------------------------- /python_toolbox/color_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines tools for manipulating colors.''' 5 | 6 | 7 | def mix_rgb(ratio, rgb1, rgb2): 8 | '''Mix two rgb colors `rgb1` and `rgb2`, according to the given `ratio`.''' 9 | counter_ratio = 1 - ratio 10 | return ( 11 | rgb1[0] * ratio + rgb2[0] * counter_ratio, 12 | rgb1[1] * ratio + rgb2[1] * counter_ratio, 13 | rgb1[2] * ratio + rgb2[2] * counter_ratio 14 | ) 15 | -------------------------------------------------------------------------------- /python_toolbox/combi/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.math_tools import binomial 5 | 6 | from python_toolbox.nifty_collections import (Bag, OrderedBag, FrozenBag, 7 | FrozenOrderedBag) 8 | 9 | from .chain_space import ChainSpace 10 | from .product_space import ProductSpace 11 | from .map_space import MapSpace 12 | from .selection_space import SelectionSpace 13 | 14 | from .perming import (PermSpace, CombSpace, Perm, UnrecurrentedPerm, Comb, 15 | UnrecurrentedComb, UnallowedVariationSelectionException) 16 | -------------------------------------------------------------------------------- /python_toolbox/combi/misc.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import math 5 | 6 | from python_toolbox import misc_tools 7 | from python_toolbox import math_tools 8 | from python_toolbox import cute_iter_tools 9 | 10 | infinity = float('inf') 11 | 12 | 13 | class MISSING_ELEMENT(misc_tools.NonInstantiable): 14 | '''A placeholder for a missing element used in internal calculations.''' 15 | 16 | 17 | def get_short_factorial_string(number, *, minus_one=False): 18 | ''' 19 | Get a short description of the factorial of `number`. 20 | 21 | If the number is long, just uses factorial notation. 22 | 23 | Examples: 24 | 25 | >>> get_short_factorial_string(4) 26 | '24' 27 | >>> get_short_factorial_string(14) 28 | '14!' 29 | 30 | ''' 31 | assert number >= 0 and \ 32 | isinstance(number, math_tools.PossiblyInfiniteIntegral) 33 | if number == infinity: 34 | return "float('inf')" 35 | elif number <= 10: 36 | return str(math.factorial(number) - int(minus_one)) 37 | else: 38 | assert number > 10 39 | return '%s!%s' % (number, ' - 1' if minus_one else '') 40 | 41 | 42 | -------------------------------------------------------------------------------- /python_toolbox/combi/perming/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from .variations import UnallowedVariationSelectionException 5 | from .perm_space import PermSpace 6 | from .comb_space import CombSpace 7 | from .perm import Perm, UnrecurrentedPerm 8 | from .comb import Comb, UnrecurrentedComb 9 | -------------------------------------------------------------------------------- /python_toolbox/comparison_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various tools for comparisons.''' 5 | 6 | import sys 7 | 8 | 9 | def underscore_hating_key(string): 10 | '''Key function for sorting that treats `_` as last character.''' 11 | assert isinstance(string, str) 12 | return str(string).replace('_', chr(sys.maxunicode)) 13 | 14 | 15 | def process_key_function_or_attribute_name(key_function_or_attribute_name): 16 | ''' 17 | Make a key function given either a key function or an attribute name. 18 | 19 | Some functions let you sort stuff by entering a key function or an 20 | attribute name by which the elements will be sorted. This function tells 21 | whether we were given a key function or an attribute name, and generates a 22 | key function out of it if needed. 23 | ''' 24 | if key_function_or_attribute_name is None: 25 | return None 26 | elif callable(key_function_or_attribute_name): 27 | return key_function_or_attribute_name 28 | else: 29 | assert isinstance(key_function_or_attribute_name, str) 30 | return lambda key: getattr(key, key_function_or_attribute_name) 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /python_toolbox/context_management/blank_context_manager.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from __future__ import generator_stop 5 | 6 | from .context_manager import ContextManager 7 | 8 | 9 | class BlankContextManager(ContextManager): 10 | '''A context manager that does nothing.''' 11 | def manage_context(self): 12 | yield self 13 | -------------------------------------------------------------------------------- /python_toolbox/context_management/delegating_context_manager.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import abc 5 | 6 | from python_toolbox import misc_tools 7 | 8 | from .context_manager import ContextManager 9 | 10 | 11 | class DelegatingContextManager(ContextManager): 12 | ''' 13 | Object which delegates its context manager interface to another object. 14 | 15 | You set the delegatee context manager as `self.delegatee_context_manager`, 16 | and whenever someone tries to use the current object as a context manager, 17 | the `__enter__` and `__exit__` methods of the delegatee object will be 18 | called. No other methods of the delegatee will be used. 19 | 20 | This is useful when you are tempted to inherit from some context manager 21 | class, but you don't to inherit all the other methods that it defines. 22 | ''' 23 | 24 | delegatee_context_manager = None 25 | ''' 26 | The context manager whose `__enter__` and `__exit__` method will be used. 27 | 28 | You may implement this as either an instance attribute or a property. 29 | ''' 30 | 31 | __enter__ = misc_tools.ProxyProperty( 32 | '.delegatee_context_manager.__enter__' 33 | ) 34 | __exit__ = misc_tools.ProxyProperty( 35 | '.delegatee_context_manager.__exit__' 36 | ) -------------------------------------------------------------------------------- /python_toolbox/context_management/functions.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | This module defines various functions related to context managers. 6 | 7 | See their documentation for more information. 8 | ''' 9 | 10 | from __future__ import generator_stop 11 | 12 | import sys 13 | 14 | from .context_manager_type import ContextManagerType 15 | 16 | 17 | @ContextManagerType 18 | def nested(*managers): 19 | # Code from `contextlib` 20 | exits = [] 21 | vars = [] 22 | exc = (None, None, None) 23 | try: 24 | for mgr in managers: 25 | exit = mgr.__exit__ 26 | enter = mgr.__enter__ 27 | vars.append(enter()) 28 | exits.append(exit) 29 | yield vars 30 | except: 31 | exc = sys.exc_info() 32 | finally: 33 | while exits: 34 | exit = exits.pop() 35 | try: 36 | if exit(*exc): 37 | exc = (None, None, None) 38 | except: 39 | exc = sys.exc_info() 40 | if exc != (None, None, None): 41 | # Don't rely on sys.exc_info() still containing 42 | # the right information. Another exception may 43 | # have been raised and caught by an exit method 44 | raise exc[1].with_traceback(exc[2]) 45 | 46 | 47 | -------------------------------------------------------------------------------- /python_toolbox/context_management/mixins/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines mixins for `ContextManager`.''' 5 | 6 | 7 | from .decorating_context_manager_mixin import _DecoratingContextManagerMixin -------------------------------------------------------------------------------- /python_toolbox/context_management/mixins/decorating_context_manager_mixin.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.third_party.decorator import decorator 5 | 6 | 7 | class _DecoratingContextManagerMixin: 8 | ''' 9 | Context manager that can decorate a function to use it. 10 | 11 | Example: 12 | 13 | my_context_manager = DecoratingContextManager() 14 | 15 | @my_context_manager 16 | def f(): 17 | pass # Anything that happens here is surrounded by the 18 | # equivalent of `my_context_manager`. 19 | 20 | ''' 21 | 22 | def __call__(self, function): 23 | '''Decorate `function` to use this context manager when it's called.''' 24 | def inner(function_, *args, **kwargs): 25 | with self: 26 | return function_(*args, **kwargs) 27 | return decorator(inner, function) -------------------------------------------------------------------------------- /python_toolbox/context_management/self_hook.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | class SelfHook: 5 | ''' 6 | Hook that a context manager can yield in order to yield itself. 7 | 8 | This is useful in context managers which are created from a generator 9 | function, where the user can't do `yield self` because `self` doesn't exist 10 | yet. 11 | 12 | Example: 13 | 14 | @ContextGeneratorType 15 | def MyContextManager(lock): 16 | with lock.read: 17 | yield SelfHook 18 | 19 | with MyContextManager(my_lock) as my_context_manager: 20 | assert isinstance(my_context_manager, MyContextManager) 21 | 22 | ''' 23 | # todo: make uninstantiable 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /python_toolbox/copy_mode.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | 5 | class CopyMode(dict): 6 | ''' 7 | Passed as a memo to `deepcopy` to specify how objects should be copied. 8 | 9 | This type is meant to be subclassed. `__deepcopy__` methods may check which 10 | class the memo is to know what kind of deepcopying they should do. 11 | 12 | Typical usage: 13 | 14 | class NetworkStyleCopying(CopyMode): pass 15 | 16 | class Something: 17 | def __deepcopy__(self, memo): 18 | if isinstance(memo, NetworkStlyeCopying): 19 | # Do network-style copying, whatever that means. 20 | else: 21 | # Do normal copying. 22 | 23 | s = Something() 24 | 25 | new_copy = copy.deepcopy(s, NetworkStyleCopying()) 26 | # Now the new copy will be created using network style copying 27 | ''' 28 | __repr__ = object.__repr__ -------------------------------------------------------------------------------- /python_toolbox/copy_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | This module defines tools related to copying and deepcopying operations. 6 | ''' 7 | 8 | import copy 9 | 10 | 11 | def deepcopy_as_simple_object(thing, memo=None): 12 | ''' 13 | Deepcopy an object as a simple `object`, ignoring any __deepcopy__ method. 14 | ''' 15 | if memo is None: 16 | memo = {} 17 | klass = thing.__class__ 18 | new_thing = klass.__new__(klass) 19 | memo[id(thing)] = new_thing 20 | for (name, subthing) in vars(thing).items(): 21 | new_thing.__dict__[name] = copy.deepcopy(subthing, memo) 22 | return new_thing 23 | 24 | -------------------------------------------------------------------------------- /python_toolbox/cute_profile/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines `profile_ready`, a decorator for flexibly profiling a function.. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | from . import base_profile 11 | from .cute_profile import profile_ready -------------------------------------------------------------------------------- /python_toolbox/cute_profile/base_profile.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Wraps the `cProfile` module, fixing a little bug in `runctx`. 6 | ''' 7 | 8 | from cProfile import * 9 | 10 | from .pstats_troubleshooting import troubleshoot_pstats 11 | troubleshoot_pstats() 12 | del troubleshoot_pstats 13 | 14 | 15 | def runctx(statement, globals, locals, filename=None, sort=-1): 16 | """Run statement under profiler, supplying your own globals and locals, 17 | optionally saving results in filename. 18 | 19 | statement and filename have the same semantics as profile.run 20 | """ 21 | prof = Profile() 22 | result = None 23 | try: 24 | try: 25 | prof = prof.runctx(statement, globals, locals) 26 | except SystemExit: 27 | pass 28 | finally: 29 | if filename is not None: 30 | prof.dump_stats(filename) 31 | else: 32 | result = prof.print_stats(sort) 33 | return result 34 | 35 | -------------------------------------------------------------------------------- /python_toolbox/cute_profile/pstats_troubleshooting.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `troubleshoot_pstats` function. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | import os 11 | 12 | from python_toolbox import import_tools 13 | 14 | 15 | def troubleshoot_pstats(): 16 | ''' 17 | Let the user know if there might be an error importing `pstats`. 18 | 19 | Raises an exception if it thinks it caught the problem. So if this function 20 | didn't raise an exception, it means it didn't manage to diagnose the 21 | problem. 22 | ''' 23 | if not import_tools.exists('pstats') and os.name == 'posix': 24 | 25 | raise ImportError( 26 | "The required `pstats` Python module is not installed on your " 27 | "computer. Since you are using Linux, it's possible that this is " 28 | "because some Linux distributions don't include `pstats` by " 29 | "default. You should be able to fix this by installing the " 30 | "`python-profiler` package in your OS's package manager. " 31 | "(Possibly you will have to get this package from the multiverse.)" 32 | ) 33 | 34 | -------------------------------------------------------------------------------- /python_toolbox/emitting/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | An emitter mechanism, a variation on the publisher-subscriber design pattern. 6 | ''' 7 | 8 | from .emitter import Emitter 9 | from .emitter_system import EmitterSystem -------------------------------------------------------------------------------- /python_toolbox/emitting/emitter_system/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines an EmitterSystem, which offers some benefits over Emitter. 6 | 7 | See documentation of EmitterSystem for more info. 8 | ''' 9 | 10 | 11 | from .emitter_system import EmitterSystem 12 | from .emitter import Emitter -------------------------------------------------------------------------------- /python_toolbox/exceptions.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines exceptions.''' 5 | 6 | 7 | class CuteBaseException(BaseException): 8 | ''' 9 | Base exception that uses its first docstring line in lieu of a message. 10 | ''' 11 | 12 | def __init__(self, message=None): 13 | # We use `None` as the default for `message`, so the user can input '' 14 | # to force an empty message. 15 | 16 | if message is None: 17 | if self.__doc__ and \ 18 | (type(self) not in (CuteBaseException, CuteException)): 19 | message = self.__doc__.strip().split('\n')[0] 20 | # Getting the first line of the documentation 21 | else: 22 | message = '' 23 | 24 | BaseException.__init__(self, message) 25 | 26 | self.message = message 27 | ''' 28 | The message of the exception, detailing what went wrong. 29 | 30 | We provide this `.message` attribute despite `BaseException.message` 31 | being deprecated in Python. The message can also be accessed as the 32 | Python-approved `BaseException.args[0]`. 33 | ''' 34 | 35 | 36 | class CuteException(CuteBaseException, Exception): 37 | '''Exception that uses its first docstring line in lieu of a message.''' 38 | 39 | 40 | -------------------------------------------------------------------------------- /python_toolbox/freezing/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `Freezer` and `FreezerProperty` classes. 6 | 7 | See their documentation for more information. 8 | ''' 9 | 10 | from .freezer import Freezer 11 | from .freezer_property import FreezerProperty -------------------------------------------------------------------------------- /python_toolbox/freezing/delegatee_context_manager.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import misc_tools 5 | from python_toolbox import context_management 6 | 7 | 8 | @context_management.as_reentrant 9 | class DelegateeContextManager(context_management.ContextManager): 10 | '''Inner context manager used internally by `Freezer`.''' 11 | 12 | def __init__(self, freezer): 13 | ''' 14 | Construct the `DelegateeContextManager`. 15 | 16 | `freezer` is the freezer to which we belong. 17 | ''' 18 | self.freezer = freezer 19 | '''The freezer to which we belong.''' 20 | 21 | 22 | def __enter__(self): 23 | '''Call the freezer's freeze handler.''' 24 | return self.freezer.freeze_handler() 25 | 26 | 27 | def __exit__(self, exc_type, exc_value, exc_traceback): 28 | '''Call the freezer's thaw handler.''' 29 | return self.freezer.thaw_handler() 30 | 31 | depth = misc_tools.ProxyProperty('.__wrapped__.depth') 32 | -------------------------------------------------------------------------------- /python_toolbox/freezing/freezer_property_freezer.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from .freezer import Freezer 5 | 6 | 7 | class FreezerPropertyFreezer(Freezer): 8 | ''' 9 | Freezer used internally by `FreezerProperty`. 10 | 11 | It uses the `FreezerProperty`'s internal freeze/thaw handlers as its own 12 | freeze/thaw handlers. 13 | ''' 14 | 15 | def __init__(self, thing): 16 | ''' 17 | Construct the `FreezerPropertyFreezer`. 18 | 19 | `thing` is the object to whom the `FreezerProperty` belongs. 20 | ''' 21 | 22 | self.thing = thing 23 | '''The object to whom the `FreezerProperty` belongs.''' 24 | 25 | 26 | def freeze_handler(self): 27 | '''Call the `FreezerProperty`'s internal freeze handler.''' 28 | return self.freezer_property._freeze_handler(self.thing) 29 | 30 | 31 | def thaw_handler(self): 32 | '''Call the `FreezerProperty`'s internal thaw handler.''' 33 | return self.freezer_property._thaw_handler(self.thing) 34 | -------------------------------------------------------------------------------- /python_toolbox/gc_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various tools for working with garbage-collection.''' 5 | 6 | import gc 7 | 8 | from python_toolbox import sys_tools 9 | 10 | def collect(): 11 | ''' 12 | Garbage-collect any items that don't have any references to them anymore. 13 | ''' 14 | if sys_tools.is_pypy: 15 | for _ in range(3): 16 | gc.collect() 17 | else: 18 | gc.collect() -------------------------------------------------------------------------------- /python_toolbox/human_names/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Provides a list of human names as `name_list`.''' 5 | 6 | from . import _name_list 7 | 8 | name_list = _name_list.data.split('\n') 9 | -------------------------------------------------------------------------------- /python_toolbox/introspection_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various introspection tools, similar to the stdlib's `inspect`.''' 5 | 6 | import inspect 7 | 8 | from python_toolbox.nifty_collections import OrderedDict 9 | 10 | 11 | def get_default_args_dict(function): 12 | ''' 13 | Get ordered dict from arguments which have a default to their default. 14 | 15 | Example: 16 | 17 | >>> def f(a, b, c=1, d='meow'): pass 18 | >>> get_default_args_dict(f) 19 | OrderedDict([('c', 1), ('d', 'meow')]) 20 | 21 | ''' 22 | arg_spec = inspect.getfullargspec(function) 23 | (s_args, s_star_args, s_star_kwargs, s_defaults, *_) = arg_spec 24 | 25 | # `getargspec` has a weird policy, when inspecting a function with no 26 | # defaults, to give a `defaults` of `None` instead of the more consistent 27 | # `()`. We fix that here: 28 | if s_defaults is None: 29 | s_defaults = () 30 | 31 | # The number of args which have default values: 32 | n_defaultful_args = len(s_defaults) 33 | 34 | defaultful_args = s_args[-n_defaultful_args:] if n_defaultful_args \ 35 | else [] 36 | 37 | return OrderedDict(zip(defaultful_args, s_defaults)) 38 | 39 | -------------------------------------------------------------------------------- /python_toolbox/locking/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from .read_write_lock import ReadWriteLock 5 | -------------------------------------------------------------------------------- /python_toolbox/math_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from .factorials import * 5 | from .misc import * 6 | from .sequences import * 7 | from .statistics import * 8 | from .types import * -------------------------------------------------------------------------------- /python_toolbox/math_tools/statistics.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import numbers 5 | 6 | 7 | infinity = float('inf') 8 | infinities = (infinity, -infinity) 9 | 10 | 11 | def get_median(iterable): 12 | '''Get the median of an iterable of numbers.''' 13 | sorted_values = sorted(iterable) 14 | 15 | if len(iterable) % 2 == 0: 16 | higher_midpoint = len(iterable) // 2 17 | lower_midpoint = higher_midpoint - 1 18 | return (sorted_values[lower_midpoint] + 19 | sorted_values[higher_midpoint]) / 2 20 | else: 21 | midpoint = len(iterable) // 2 22 | return sorted_values[midpoint] 23 | 24 | 25 | def get_mean(iterable): 26 | '''Get the mean (average) of an iterable of numbers.''' 27 | sum_ = 0 28 | for i, value in enumerate(iterable): 29 | sum_ += value 30 | return sum_ / (i + 1) 31 | 32 | 33 | -------------------------------------------------------------------------------- /python_toolbox/math_tools/types.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import abc 5 | import numbers 6 | 7 | infinity = float('inf') 8 | infinities = (infinity, -infinity) 9 | 10 | 11 | class _PossiblyInfiniteIntegralType(abc.ABCMeta): 12 | def __instancecheck__(self, thing): 13 | return isinstance(thing, numbers.Integral) or (thing in infinities) 14 | class PossiblyInfiniteIntegral(numbers.Number, 15 | metaclass=_PossiblyInfiniteIntegralType): 16 | '''An integer or infinity (including negative infinity.)''' 17 | 18 | class _PossiblyInfiniteRealType(abc.ABCMeta): 19 | def __instancecheck__(self, thing): 20 | return isinstance(thing, numbers.Real) or (thing in infinities) 21 | class PossiblyInfiniteReal(numbers.Number, 22 | metaclass=_PossiblyInfiniteRealType): 23 | '''A real number or infinity (including negative infinity.)''' 24 | 25 | class _NaturalType(abc.ABCMeta): 26 | def __instancecheck__(self, thing): 27 | return isinstance(thing, numbers.Integral) and thing >= 1 28 | class Natural(numbers.Number, 29 | metaclass=_NaturalType): 30 | '''A natural number, meaning a positive integer (0 not included.)''' 31 | -------------------------------------------------------------------------------- /python_toolbox/misc_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''This module defines miscellaneous tools.''' 5 | 6 | from .misc_tools import * 7 | from . import name_mangling 8 | from .proxy_property import ProxyProperty 9 | from .overridable_property import OverridableProperty -------------------------------------------------------------------------------- /python_toolbox/monkeypatch_envelopes.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Module for monkeypatching our own copy of `envelopes`.''' 5 | 6 | ### Monkeypatching envelopes: ################################################# 7 | # # 8 | from python_toolbox.third_party import envelopes 9 | from python_toolbox import monkeypatching_tools 10 | 11 | 12 | @monkeypatching_tools.monkeypatch(envelopes.Envelope) 13 | def add_attachment_from_string(self, file_data, file_name, 14 | mimetype='application/octet-stream'): 15 | from python_toolbox.third_party.envelopes.envelope import \ 16 | MIMEBase, email_encoders, os 17 | type_maj, type_min = mimetype.split('/') 18 | 19 | part = MIMEBase(type_maj, type_min) 20 | part.set_payload(file_data) 21 | email_encoders.encode_base64(part) 22 | 23 | part.add_header('Content-Disposition', 24 | f'attachment; filename="{file_name}"') 25 | 26 | self._parts.append((mimetype, part)) 27 | 28 | # # 29 | ### Finished monkeypatching envelopes. ######################################## 30 | -------------------------------------------------------------------------------- /python_toolbox/nifty_collections/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various data types, similarly to the stdlib's `collections`.''' 5 | 6 | from .ordered_dict import OrderedDict 7 | from .various_ordered_sets import OrderedSet, FrozenOrderedSet, EmittingOrderedSet 8 | from .weak_key_default_dict import WeakKeyDefaultDict 9 | from .weak_key_identity_dict import WeakKeyIdentityDict 10 | from .lazy_tuple import LazyTuple 11 | from .various_frozen_dicts import FrozenDict, FrozenOrderedDict 12 | from .bagging import Bag, OrderedBag, FrozenBag, FrozenOrderedBag 13 | from .frozen_bag_bag import FrozenBagBag 14 | from ..cute_enum import CuteEnum 15 | 16 | from .emitting_weak_key_default_dict import EmittingWeakKeyDefaultDict 17 | 18 | from .abstract import Ordered -------------------------------------------------------------------------------- /python_toolbox/nifty_collections/abstract.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import abc 5 | import collections 6 | import queue 7 | import multiprocessing.queues 8 | 9 | 10 | ############################################################################### 11 | 12 | class Ordered(metaclass=abc.ABCMeta): 13 | ''' 14 | A data structure that has a defined order. 15 | 16 | This is an abstract type. You can use `isinstance(whatever, Ordered)` to 17 | check whether a data structure is ordered. (Note that there will be false 18 | negatives.) 19 | ''' 20 | __slots__ = () 21 | 22 | 23 | Ordered.register(collections.abc.Sequence) 24 | Ordered.register(collections.OrderedDict) 25 | Ordered.register(collections.deque) 26 | Ordered.register(queue.Queue) 27 | Ordered.register(multiprocessing.queues.Queue) 28 | 29 | ############################################################################### 30 | 31 | -------------------------------------------------------------------------------- /python_toolbox/os_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Various os-related tools.''' 5 | 6 | import subprocess 7 | import sys 8 | import os.path 9 | 10 | 11 | def start_file(path): 12 | '''Open a file by launching the program that handles its kind.''' 13 | path = pathlib.Path(path) 14 | assert path.exists() 15 | 16 | if sys.platform.startswith('linux'): # Linux: 17 | subprocess.check_call(['xdg-open', str(path)]) 18 | 19 | elif sys.platform == 'darwin': # Mac: 20 | subprocess.check_call(['open', '--', str(path)]) 21 | 22 | elif sys.platform in ('win32', 'cygwin'): # Windows: 23 | os.startfile(path) 24 | 25 | else: 26 | raise NotImplementedError( 27 | f"Your operating system {sys.platform} isn't supported by " 28 | f"`start_file`.") 29 | 30 | -------------------------------------------------------------------------------- /python_toolbox/pickle_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various tools for pickling and unpickling.''' 5 | 6 | 7 | import zlib 8 | import pickle as pickle_module 9 | 10 | 11 | def compickle(thing): 12 | '''Pickle `thing` and compress it using `zlib`.''' 13 | return zlib.compress(pickle_module.dumps(thing)) 14 | 15 | def decompickle(thing): 16 | '''Unpickle `thing` after decompressing it using `zlib`.''' 17 | return pickle_module.loads(zlib.decompress(thing)) -------------------------------------------------------------------------------- /python_toolbox/process_priority.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `set_process_priority` function. 6 | 7 | See its documentation for more info. 8 | ''' 9 | 10 | def set_process_priority(priority, pid=None): 11 | ''' 12 | Set the priority of a Windows process. 13 | 14 | Priority is a value between 0-5 where 2 is normal priority. Default sets 15 | the priority of the current Python process but can take any valid process 16 | ID. 17 | ''' 18 | 19 | import win32process, win32con, win32api 20 | 21 | priorityclasses = [ 22 | win32process.IDLE_PRIORITY_CLASS, 23 | win32process.BELOW_NORMAL_PRIORITY_CLASS, 24 | win32process.NORMAL_PRIORITY_CLASS, 25 | win32process.ABOVE_NORMAL_PRIORITY_CLASS, 26 | win32process.HIGH_PRIORITY_CLASS, 27 | win32process.REALTIME_PRIORITY_CLASS 28 | ] 29 | 30 | if pid is None: 31 | pid = win32api.GetCurrentProcessId() 32 | handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid) 33 | win32process.SetPriorityClass(handle, priorityclasses[priority]) -------------------------------------------------------------------------------- /python_toolbox/re_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import re 5 | 6 | 7 | def searchall(pattern, string, flags=0): 8 | ''' 9 | Return all the substrings of `string` that match `pattern`. 10 | 11 | Note: Currently returns only non-overlapping matches. 12 | ''' 13 | if isinstance(pattern, str): 14 | pattern = re.compile(pattern, flags=flags) 15 | matches = [] 16 | start = 0 17 | end = len(string) 18 | 19 | while True: 20 | match = pattern.search(string, start, end) 21 | if match: 22 | matches.append(match) 23 | start = match.end() 24 | else: 25 | break 26 | 27 | return matches 28 | -------------------------------------------------------------------------------- /python_toolbox/reasoned_bool.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | 5 | class ReasonedBool: 6 | ''' 7 | A variation on `bool` that also gives a `.reason`. 8 | 9 | This is useful when you want to say "This is False because... (reason.)" 10 | 11 | Unfortunately this class is not a subclass of `bool`, since Python doesn't 12 | allow subclassing `bool`. 13 | ''' 14 | 15 | def __init__(self, value, reason=None): 16 | ''' 17 | Construct the `ReasonedBool`. 18 | 19 | `reason` is the reason *why* it has a value of `True` or `False`. It is 20 | usually a string, but is allowed to be of any type. 21 | ''' 22 | self.value = bool(value) 23 | self.reason = reason 24 | 25 | 26 | def __repr__(self): 27 | if self.reason is not None: 28 | return f'<{self.value} because {repr(self.reason)}>' 29 | else: # self.reason is None 30 | return f'<{self.value} with no reason>' 31 | 32 | 33 | def __eq__(self, other): 34 | return bool(self) == other 35 | 36 | 37 | def __hash__(self): 38 | return hash(bool(self)) 39 | 40 | 41 | def __neq__(self, other): 42 | return not self.__eq__(other) 43 | 44 | 45 | def __bool__(self): 46 | return self.value -------------------------------------------------------------------------------- /python_toolbox/rst_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Tools for handling ReStructuredText.''' 5 | 6 | import docutils.core 7 | 8 | 9 | def rst_to_html(rst_text): 10 | '''Convert a piece of `rst_text` into HTML.''' 11 | return docutils.core.publish_parts(rst_text, writer_name='html')['body'] -------------------------------------------------------------------------------- /python_toolbox/sequence_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from .misc import * 5 | from .cute_range import CuteRange 6 | from .canonical_slice import CanonicalSlice -------------------------------------------------------------------------------- /python_toolbox/sleek_reffing/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `SleekRef` class and various data types using it. 6 | 7 | See documentation of `SleekRef` for more details. `SleekCallArgs` and 8 | `CuteSleekValueDict` are data types which rely on `SleekRef`. 9 | ''' 10 | 11 | from .sleek_ref import SleekRef 12 | from .exceptions import SleekRefDied 13 | from .sleek_call_args import SleekCallArgs 14 | from .cute_sleek_value_dict import CuteSleekValueDict 15 | 16 | 17 | __all__ = ['SleekRef', 'SleekRefDied', 'SleekCallArgs', 'CuteSleekValueDict'] 18 | -------------------------------------------------------------------------------- /python_toolbox/sleek_reffing/exceptions.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines exceptions.''' 5 | 6 | from python_toolbox.exceptions import CuteException 7 | 8 | 9 | class SleekRefDied(CuteException): 10 | '''You tried to access a sleekref's value but it's already dead.''' -------------------------------------------------------------------------------- /python_toolbox/string_cataloging.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''A little module for cataloging strings.''' 5 | 6 | 7 | _catalog = [] 8 | 9 | 10 | def string_to_integer(string): 11 | ''' 12 | If the string isn't cataloged already, catalog it. 13 | 14 | In any case, returns the number associated with the string. 15 | ''' 16 | global _catalog 17 | if string in _catalog: 18 | return _catalog.index(string) + 1 19 | else: 20 | _catalog.append(string) 21 | return _catalog.index(string) + 1 22 | 23 | 24 | def integer_to_string(integer): 25 | '''Get the string cataloged under the given integer.''' 26 | return _catalog[integer - 1] 27 | -------------------------------------------------------------------------------- /python_toolbox/string_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines string-related tools.''' 5 | 6 | from .string_tools import (docstring_trim, get_n_identical_edge_characters, 7 | rreplace) 8 | from . import case_conversions -------------------------------------------------------------------------------- /python_toolbox/temp_value_setting/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines `TempValueSetter` and various useful subclasses of it. 6 | 7 | See documentation of `TempValueSetter` for more details. 8 | `TempWorkingDirectorySetter` and `TempRecursionLimitSetter` are useful 9 | subclasses of it. 10 | ''' 11 | 12 | from .temp_value_setter import TempValueSetter 13 | from .temp_working_directory_setter import TempWorkingDirectorySetter 14 | from .temp_recursion_limit_setter import TempRecursionLimitSetter 15 | from .temp_import_hook_setter import TempImportHookSetter -------------------------------------------------------------------------------- /python_toolbox/temp_value_setting/temp_import_hook_setter.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `TempImportHookSetter` class. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | import builtins 11 | 12 | from .temp_value_setter import TempValueSetter 13 | import collections 14 | 15 | 16 | class TempImportHookSetter(TempValueSetter): 17 | ''' 18 | Context manager for temporarily setting a function as the import hook. 19 | ''' 20 | def __init__(self, import_hook): 21 | ''' 22 | Construct the `TempImportHookSetter`. 23 | 24 | `import_hook` is the function to be used as the import hook. 25 | ''' 26 | assert callable(import_hook) 27 | TempValueSetter.__init__(self, 28 | (__builtin__, '__import__'), 29 | value=import_hook) -------------------------------------------------------------------------------- /python_toolbox/temp_value_setting/temp_recursion_limit_setter.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `TempRecursionLimitSetter` class. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | import sys 11 | 12 | from .temp_value_setter import TempValueSetter 13 | 14 | 15 | class TempRecursionLimitSetter(TempValueSetter): 16 | ''' 17 | Context manager for temporarily changing the recurstion limit. 18 | 19 | The temporary recursion limit comes into effect before the suite starts, 20 | and the original recursion limit returns after the suite finishes. 21 | ''' 22 | 23 | def __init__(self, recursion_limit): 24 | ''' 25 | Construct the `TempRecursionLimitSetter`. 26 | 27 | `recursion_limit` is the temporary recursion limit to use. 28 | ''' 29 | assert isinstance(recursion_limit, int) 30 | TempValueSetter.__init__( 31 | self, 32 | (sys.getrecursionlimit, sys.setrecursionlimit), 33 | value=recursion_limit 34 | ) -------------------------------------------------------------------------------- /python_toolbox/temp_value_setting/temp_working_directory_setter.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `TempWorkingDirectorySetter` class. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | import os 11 | 12 | from .temp_value_setter import TempValueSetter 13 | 14 | 15 | class TempWorkingDirectorySetter(TempValueSetter): 16 | ''' 17 | Context manager for temporarily changing the working directory. 18 | 19 | The temporary working directory is set before the suite starts, and the 20 | original working directory is used again after the suite finishes. 21 | ''' 22 | def __init__(self, working_directory): 23 | ''' 24 | Construct the `TempWorkingDirectorySetter`. 25 | 26 | `working_directory` is the temporary working directory to use. 27 | ''' 28 | TempValueSetter.__init__(self, 29 | (os.getcwd, os.chdir), 30 | value=str(working_directory)) -------------------------------------------------------------------------------- /python_toolbox/third_party/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Collection of third-party modules.''' 5 | -------------------------------------------------------------------------------- /python_toolbox/third_party/envelopes/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright (c) 2013 Tomasz Wójcik 3 | # 4 | # Permission is hereby granted, free of charge, to any person obtaining a copy 5 | # of this software and associated documentation files (the "Software"), to deal 6 | # in the Software without restriction, including without limitation the rights 7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | # copies of the Software, and to permit persons to whom the Software is 9 | # furnished to do so, subject to the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included in 12 | # all copies or substantial portions of the Software. 13 | # 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | # THE SOFTWARE. 21 | # 22 | 23 | """ 24 | envelopes 25 | --------- 26 | 27 | Mailing for human beings. 28 | """ 29 | 30 | __version__ = '0.4' 31 | 32 | 33 | from .conn import * 34 | from .envelope import Envelope 35 | -------------------------------------------------------------------------------- /python_toolbox/tracing_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines tools for tracing function calls.''' 5 | 6 | from .count_calls import count_calls 7 | from .temp_function_call_counter import TempFunctionCallCounter -------------------------------------------------------------------------------- /python_toolbox/tracing_tools/count_calls.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | ''' 5 | Defines the `count_calls` decorator. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | from python_toolbox.third_party.decorator import decorator 11 | 12 | 13 | def count_calls(function): 14 | ''' 15 | Decorator for counting the calls made to a function. 16 | 17 | The number of calls is available in the decorated function's `.call_count` 18 | attribute. 19 | 20 | Example usage: 21 | 22 | >>> @count_calls 23 | ... def f(x): 24 | ... return x*x 25 | ... 26 | >>> f(3) 27 | 9 28 | >>> f(6) 29 | 36 30 | >>> f.call_count 31 | 2 32 | >>> f(9) 33 | 81 34 | >>> f.call_count 35 | 3 36 | 37 | ''' 38 | def _count_calls(function, *args, **kwargs): 39 | decorated_function.call_count += 1 40 | return function(*args, **kwargs) 41 | 42 | decorated_function = decorator(_count_calls, function) 43 | 44 | decorated_function.call_count = 0 45 | 46 | return decorated_function 47 | 48 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various tools for wxPython.''' 5 | 6 | import wx 7 | 8 | 9 | is_mac = (wx.Platform == '__WXMAC__') 10 | is_gtk = (wx.Platform == '__WXGTK__') 11 | is_win = (wx.Platform == '__WXMSW__') 12 | 13 | 14 | from . import colors 15 | from . import keyboard 16 | from . import window_tools 17 | from . import bitmap_tools 18 | from . import cursors 19 | from . import event_tools 20 | from . import generic_bitmaps 21 | from . import drawing_tools 22 | from . import timing 23 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/bitmap_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines bitmap-related tools.''' 5 | 6 | import pkg_resources 7 | import wx 8 | 9 | 10 | def color_replaced_bitmap(bitmap, old_rgb, new_rgb): 11 | '''Replace all appearances of `old_rgb` with `new_rgb` in `bitmap`.''' 12 | old_r, old_g, old_b = old_rgb 13 | new_r, new_g, new_b = new_rgb 14 | image = wx.ImageFromBitmap(bitmap) 15 | assert isinstance(image, wx.Image) 16 | image.Replace(old_r, old_g, old_b, new_r, new_g, new_b) 17 | return wx.BitmapFromImage(image) 18 | 19 | 20 | def bitmap_from_pkg_resources(package_or_requirement, resource_name): 21 | ''' 22 | Get a bitmap from a file using `pkg_resources`. 23 | 24 | Example: 25 | 26 | my_bitmap = bitmap_from_pkg_resources('whatever.images', 'image.jpg') 27 | 28 | ''' 29 | return wx.Bitmap( 30 | wx.Image( 31 | pkg_resources.resource_stream(package_or_requirement, 32 | resource_name), 33 | wx.BITMAP_TYPE_ANY 34 | ) 35 | ) -------------------------------------------------------------------------------- /python_toolbox/wx_tools/cursors/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various cursor-related tools.''' 5 | 6 | from . import collection 7 | from .cursor_changer import CursorChanger -------------------------------------------------------------------------------- /python_toolbox/wx_tools/cursors/collection/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''A collection of cursors.''' 5 | 6 | from .collection import get_open_grab, get_closed_grab -------------------------------------------------------------------------------- /python_toolbox/wx_tools/cursors/collection/images/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Images package.''' 5 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/cursors/collection/images/closed_grab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/python_toolbox/wx_tools/cursors/collection/images/closed_grab.png -------------------------------------------------------------------------------- /python_toolbox/wx_tools/cursors/collection/images/open_grab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/python_toolbox/wx_tools/cursors/collection/images/open_grab.png -------------------------------------------------------------------------------- /python_toolbox/wx_tools/cursors/cursor_changer.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import wx 5 | 6 | from python_toolbox.temp_value_setting import TempValueSetter 7 | 8 | 9 | class CursorChanger(TempValueSetter): 10 | '''Context manager for showing specified cursor while suite executes.''' 11 | def __init__(self, window, cursor): 12 | ''' 13 | Construct the `CursorChanger`. 14 | 15 | `cursor` may be either a `wx.Cursor` object or a constant like 16 | `wx.CURSOR_BULLSEYE`. 17 | ''' 18 | assert isinstance(window, wx.Window) 19 | self.window = window 20 | self.cursor = cursor if isinstance(cursor, wx.Cursor) \ 21 | else wx.StockCursor(cursor) 22 | TempValueSetter.__init__(self, 23 | (window.GetCursor, window.SetCursor), 24 | self.cursor, 25 | assert_no_fiddling=False) -------------------------------------------------------------------------------- /python_toolbox/wx_tools/drawing_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines tools for drawing with wxPython.''' 5 | 6 | from . import pens -------------------------------------------------------------------------------- /python_toolbox/wx_tools/drawing_tools/pens.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import wx 5 | 6 | from python_toolbox import caching 7 | 8 | 9 | is_mac = (wx.Platform == '__WXMAC__') 10 | is_gtk = (wx.Platform == '__WXGTK__') 11 | is_win = (wx.Platform == '__WXMSW__') 12 | 13 | 14 | @caching.cache(max_size=100) 15 | def get_focus_pen(color='black', width=1, dashes=[1, 4]): 16 | ''' ''' 17 | if isinstance(color, basestring): 18 | color = wx.NamedColour(color) 19 | 20 | # todo: do `if is_mac`, also gtk maybe 21 | 22 | pen = wx.Pen(color, width, wx.USER_DASH) 23 | pen.SetDashes(dashes) 24 | return pen 25 | 26 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/keyboard/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various keyboard-related tools.''' 5 | 6 | from .key import Key 7 | from . import keys -------------------------------------------------------------------------------- /python_toolbox/wx_tools/keyboard/keys/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines various keys and strings that describe them.''' 5 | 6 | import wx 7 | 8 | 9 | is_mac = (wx.Platform == '__WXMAC__') 10 | is_gtk = (wx.Platform == '__WXGTK__') 11 | is_win = (wx.Platform == '__WXMSW__') 12 | 13 | 14 | from .global_keys import menu_keys, enter_keys 15 | 16 | if is_win: 17 | from .win_keys import (back_keys, back_key_string, 18 | forward_keys, forward_key_string) 19 | elif is_gtk: 20 | from .gtk_keys import (back_keys, back_key_string, 21 | forward_keys, forward_key_string) 22 | else: 23 | assert is_mac 24 | from .mac_keys import (back_keys, back_key_string, 25 | forward_keys, forward_key_string) -------------------------------------------------------------------------------- /python_toolbox/wx_tools/keyboard/keys/global_keys.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines keys that work on all operating systems.''' 5 | 6 | import wx 7 | 8 | from ..key import Key 9 | 10 | 11 | menu_keys = [Key(wx.WXK_MENU), Key(wx.WXK_WINDOWS_MENU), 12 | Key(wx.WXK_F10, shift=True)] 13 | '''Keys used for raising a context menu.''' 14 | # todo: explode into OSes and get the Mac one. (I think it's Function-Ctrl-5 or 15 | # something.) 16 | 17 | enter_keys = [Key(wx.WXK_RETURN), Key(wx.WXK_NUMPAD_ENTER)] -------------------------------------------------------------------------------- /python_toolbox/wx_tools/keyboard/keys/gtk_keys.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''This module defines GTK-specific keys.''' 5 | 6 | import wx 7 | 8 | from ..key import Key 9 | 10 | 11 | back_keys = ( 12 | Key(wx.WXK_LEFT, alt=True), 13 | ) 14 | 15 | back_key_string = u'Alt-\u00ab' 16 | 17 | forward_keys = ( 18 | Key(wx.WXK_RIGHT, alt=True), 19 | ) 20 | 21 | forward_key_string = u'Alt-\u00bb' -------------------------------------------------------------------------------- /python_toolbox/wx_tools/keyboard/keys/mac_keys.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''This module defines Mac-specific keys.''' 5 | 6 | import wx 7 | 8 | from ..key import Key 9 | 10 | 11 | back_keys = ( 12 | Key(ord('['), cmd=True), 13 | Key(wx.WXK_LEFT, cmd=True) 14 | ) 15 | 16 | back_key_string = u'\u2318\u00ab' 17 | 18 | forward_keys = ( 19 | Key(ord(']'), cmd=True), 20 | Key(wx.WXK_RIGHT, cmd=True) 21 | ) 22 | 23 | forward_key_string = u'\u2318\u00bb' 24 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/keyboard/keys/win_keys.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''This module defines Windows-specific keys.''' 5 | 6 | import wx 7 | 8 | from ..key import Key 9 | 10 | 11 | back_keys = ( 12 | Key(wx.WXK_LEFT, alt=True), 13 | ) 14 | 15 | back_key_string = u'Alt-\u00ab' 16 | 17 | forward_keys = ( 18 | Key(wx.WXK_RIGHT, alt=True), 19 | ) 20 | 21 | forward_key_string = u'Alt-\u00bb' -------------------------------------------------------------------------------- /python_toolbox/wx_tools/timing/__init__.py: -------------------------------------------------------------------------------- 1 | from .cute_base_timer import CuteBaseTimer 2 | from .thread_timer import ThreadTimer -------------------------------------------------------------------------------- /python_toolbox/wx_tools/timing/cute_base_timer.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | 5 | class CuteBaseTimer: 6 | '''A base class for timers, allowing easy central stopping.''' 7 | __timers = [] # todo: change to weakref list 8 | 9 | def __init__(self, parent): 10 | self.__parent = parent 11 | CuteBaseTimer.__timers.append(self) 12 | 13 | 14 | @staticmethod # should be classmethod? 15 | def stop_timers_by_frame(frame): 16 | '''Stop all the timers that are associated with the given frame.''' 17 | for timer in CuteBaseTimer.__timers: 18 | ancestor = timer.__parent 19 | while ancestor: 20 | if ancestor == frame: 21 | timer.Stop() 22 | break 23 | ancestor = ancestor.GetParent() 24 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | '''This package defines generic wxPython widgets.''' -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_bitmap_button.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from python_toolbox.wx_tools.widgets.cute_button import CuteButton 7 | 8 | 9 | class CuteBitmapButton(wx.BitmapButton, CuteButton): 10 | def __init__(self, parent, id=-1, bitmap=wx.NullBitmap, 11 | pos=wx.DefaultPosition, size=wx.DefaultSize, 12 | style=wx.BU_AUTODRAW, validator=wx.DefaultValidator, 13 | name=wx.ButtonNameStr, bitmap_disabled=None, tool_tip=None, 14 | help_text=None): 15 | 16 | wx.BitmapButton.__init__(self, parent=parent, id=id, bitmap=bitmap, 17 | pos=pos, size=size, style=style, 18 | validator=validator, name=name) 19 | if bitmap_disabled is not None: 20 | self.SetBitmapDisabled(bitmap_disabled) 21 | self.set_tool_tip_and_help_text(tool_tip, help_text) 22 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_button.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from python_toolbox.wx_tools.widgets.cute_control import CuteControl 7 | 8 | 9 | class CuteButton(wx.Button, CuteControl): 10 | pass -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_control.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from python_toolbox.wx_tools.widgets.cute_window import CuteWindow 7 | 8 | 9 | class CuteControl(wx.Control, CuteWindow): 10 | pass -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_dialog_type.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from python_toolbox.wx_tools.cursors import CursorChanger 7 | from python_toolbox import context_management 8 | 9 | from python_toolbox.wx_tools.widgets.cute_window import CuteWindow 10 | 11 | 12 | class CuteDialogType(type(CuteWindow)): 13 | def __call__(self, parent, *args, **kwargs): 14 | context_manager = \ 15 | CursorChanger(parent, wx.CURSOR_WAIT) if parent else \ 16 | context_management.BlankContextManager() 17 | with context_manager: 18 | return type.__call__(self, parent, *args, **kwargs) -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_error_dialog.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | ''' 5 | Defines the `CuteErrorDialog` class. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | import wx 11 | 12 | from python_toolbox.wx_tools.widgets.cute_dialog import CuteDialog 13 | 14 | 15 | class CuteErrorDialog(wx.MessageDialog, CuteDialog): 16 | '''Dialog showing error message with an error icon.''' 17 | def __init__(self, parent, message, caption='Error', 18 | style=(wx.OK | wx.ICON_ERROR)): 19 | wx.MessageDialog.__init__(self, parent, message, caption, 20 | style=style) 21 | CuteDialog.__init__(self, skip_wx_init=True) 22 | self.ExtraStyle &= ~wx.FRAME_EX_CONTEXTHELP 23 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_frame.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from .cute_top_level_window import CuteTopLevelWindow 7 | 8 | 9 | class CuteFrame(wx.Frame, CuteTopLevelWindow): 10 | ''' 11 | An improved `wx.Frame`. 12 | 13 | See `CuteTopLevelWindow` for what this class gives over `wx.Frame`. 14 | ''' 15 | def __init__(self, parent, id=-1, title=wx.EmptyString, 16 | pos=wx.DefaultPosition, size=wx.DefaultSize, 17 | style=wx.DEFAULT_FRAME_STYLE, name=wx.FrameNameStr): 18 | wx.Frame.__init__(self, parent=parent, id=id, title=title, 19 | pos=pos, size=size, style=style, name=name) 20 | CuteTopLevelWindow.__init__(self, parent=parent, id=id, title=title, 21 | pos=pos, size=size, style=style, name=name) -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_hidden_button.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from python_toolbox.wx_tools.widgets.cute_control import CuteControl 7 | 8 | class CuteHiddenButton(wx.Button, CuteControl): 9 | def __init__(self, parent, *args, **kwargs): 10 | ''' ''' 11 | wx.Button.__init__(self, parent, *args, **kwargs) 12 | self.Hide() -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_html_window.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import webbrowser 5 | 6 | import wx.html 7 | 8 | from python_toolbox.wx_tools.widgets.cute_window import CuteWindow 9 | 10 | 11 | class CuteHtmlWindow(wx.html.HtmlWindow, CuteWindow): 12 | 13 | event_modules = wx.html 14 | 15 | def __init__(self, parent, id=-1, pos=wx.DefaultPosition, 16 | size=wx.DefaultSize, style=wx.html.HW_DEFAULT_STYLE, 17 | name=wx.html.HtmlWindowNameStr): 18 | wx.html.HtmlWindow.__init__(self, parent=parent, id=id, pos=pos, 19 | size=size, style=style, name=name) 20 | self.bind_event_handlers(CuteHtmlWindow) 21 | 22 | 23 | def _on_html_link_clicked(self, event): 24 | webbrowser.open_new_tab( 25 | event.GetLinkInfo().GetHref() 26 | ) -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_hyperlink_ctrl.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from python_toolbox.wx_tools.widgets.cute_control import CuteControl 7 | 8 | 9 | class CuteHyperlinkCtrl(wx.HyperlinkCtrl, CuteControl): 10 | ''' ''' 11 | def __init__(self, parent, id=-1, label='', url='', pos=wx.DefaultPosition, 12 | size=wx.DefaultSize, style=wx.HL_DEFAULT_STYLE, 13 | name=wx.HyperlinkCtrlNameStr): 14 | 15 | wx.HyperlinkCtrl.__init__( 16 | self, parent=parent, id=id, label=label, url=url, pos=pos, 17 | size=size, style=style, name=name 18 | ) 19 | 20 | 21 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_message_dialog.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | ''' 5 | Defines the `CuteMessageDialog` class. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | import wx 11 | 12 | from python_toolbox.wx_tools.widgets.cute_dialog import CuteDialog 13 | 14 | 15 | class CuteMessageDialog(wx.MessageDialog, CuteDialog): 16 | def __init__(self, parent, message, caption='Message', style=wx.OK): 17 | wx.MessageDialog.__init__(self, parent, message, caption, 18 | style=style) 19 | CuteDialog.__init__(self, skip_wx_init=True) 20 | self.ExtraStyle &= ~wx.FRAME_EX_CONTEXTHELP 21 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_panel.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from python_toolbox.wx_tools.widgets.cute_window import CuteWindow 7 | 8 | 9 | class CutePanel(wx.Panel, CuteWindow): 10 | ''' 11 | 12 | This class doesn't require calling its `__init__` when subclassing. (i.e., 13 | you *may* call its `__init__` if you want, but it will do the same as 14 | calling `wx.Window.__init__`.) 15 | ''' 16 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_scrolled_panel.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx.lib.scrolledpanel 5 | 6 | from python_toolbox.wx_tools.widgets.cute_panel import CutePanel 7 | 8 | 9 | class CuteScrolledPanel(wx.lib.scrolledpanel.ScrolledPanel, CutePanel): 10 | ''' 11 | 12 | ''' 13 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_static_text.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | ''' 5 | Defines the `CuteStaticText` class. 6 | 7 | See its documentation for more info. 8 | ''' 9 | 10 | import wx 11 | 12 | from .cute_window import CuteWindow 13 | 14 | 15 | class CuteStaticText(wx.StaticText, CuteWindow): 16 | ''' 17 | 18 | 19 | ''' 20 | def __init__(self, parent, id=-1, label=wx.EmptyString, 21 | pos=wx.DefaultPosition, size=wx.DefaultSize, 22 | style=0, name=wx.StaticTextNameStr, skip_wx_init=False): 23 | 24 | if not skip_wx_init: 25 | wx.StaticText.__init__(self, parent=parent, id=id, label=label, 26 | pos=pos, size=size, style=style, name=name) 27 | self.label = label 28 | self.bind_event_handlers(CuteStaticText) 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_top_level_window.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | import wx 5 | 6 | from .cute_window import CuteWindow 7 | 8 | 9 | class CuteTopLevelWindow(wx.TopLevelWindow, CuteWindow): 10 | ''' 11 | An improved `wx.TopLevelWindow`. 12 | 13 | The advantages of this class over `wx.TopLevelWindow`: 14 | 15 | - A good background color. 16 | - Advantages given by `CuteWindow` 17 | 18 | ''' 19 | def __init__(self, *args, **kwargs): 20 | self.set_good_background_color() -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_window/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | ''' 5 | Defines the `CuteWindow` class. 6 | 7 | See its documentation for more information. 8 | ''' 9 | 10 | from .cute_window import CuteWindow -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/cute_window/bind_savvy_evt_handler/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | from .bind_savvy_evt_handler import BindSavvyEvtHandler -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/hue_selection_dialog/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | ''' 5 | Defines the `HueSelectionDialog` class. 6 | 7 | See its documentation for more details. 8 | ''' 9 | 10 | from .hue_selection_dialog import HueSelectionDialog -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/knob/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | ''' 5 | Defines the `Knob` class. 6 | 7 | See its documentation for more info. 8 | ''' 9 | 10 | from .knob import Knob -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/knob/images/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | '''Images package.''' -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/knob/images/knob.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/python_toolbox/wx_tools/widgets/knob/images/knob.png -------------------------------------------------------------------------------- /python_toolbox/wx_tools/widgets/third_party/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2011 Ram Rachum. 2 | # This program is distributed under the LGPL2.1 license. 3 | 4 | '''Collection of third-party widgets.''' -------------------------------------------------------------------------------- /test_python_toolbox/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox`.''' 5 | 6 | import sys 7 | import os 8 | import pathlib 9 | import importlib 10 | 11 | import pytest 12 | 13 | 14 | 15 | def __bootstrap(): 16 | ''' 17 | Add needed packages in repo to path if we can't find them. 18 | 19 | This adds `python_toolbox`'s root folder to `sys.path` if it can't 20 | currently be imported. 21 | ''' 22 | if not importlib.util.find_spec('python_toolbox'): 23 | python_toolbox_candidate_path = \ 24 | pathlib(__file__).parent.parent.absolute() 25 | sys.path.append(python_toolbox_candidate_path) 26 | 27 | 28 | __bootstrap() 29 | 30 | 31 | def invoke_tests(): 32 | '''Start Pytest using this `test_python_toolbox` test package.''' 33 | os.chdir(os.path.dirname(__file__)) 34 | pytest.main() 35 | # nose.run(defaultTest='test_python_toolbox', 36 | # argv=(arguments + sys.argv[1:])) 37 | -------------------------------------------------------------------------------- /test_python_toolbox/scripts/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Scripts for `test_python_toolbox`.''' 5 | 6 | from . import _test_python_toolbox -------------------------------------------------------------------------------- /test_python_toolbox/scripts/_test_python_toolbox.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # Copyright 2009-2017 Ram Rachum. 4 | # This program is distributed under the MIT license. 5 | 6 | ''' 7 | Script for launching `python_toolbox` tests when installed in local Python. 8 | ''' 9 | 10 | 11 | import test_python_toolbox 12 | 13 | 14 | if __name__ == '__main__': 15 | test_python_toolbox.invoke_tests() -------------------------------------------------------------------------------- /test_python_toolbox/test_abc_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.abc_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_abc_tools/test_abstract_static_method.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.abc_tools.AbstractStaticMethod`.''' 5 | 6 | import sys 7 | import abc 8 | 9 | import pytest 10 | 11 | from python_toolbox.abc_tools import AbstractStaticMethod 12 | 13 | 14 | def test_instantiate_without_subclassing(): 15 | '''Test you can't instantiate a class with an `AbstractStaticMethod`.''' 16 | 17 | class A(metaclass=abc.ABCMeta): 18 | @AbstractStaticMethod 19 | def f(): 20 | pass 21 | 22 | pytest.raises(TypeError, lambda: A()) 23 | 24 | 25 | def test_override(): 26 | ''' 27 | Can't instantiate subclass that doesn't override `AbstractStaticMethod`. 28 | ''' 29 | 30 | class B(metaclass=abc.ABCMeta): 31 | @AbstractStaticMethod 32 | def f(): 33 | pass 34 | 35 | class C(B): 36 | @staticmethod 37 | def f(): 38 | return 7 39 | 40 | c = C() 41 | 42 | assert C.f() == c.f() == 7 43 | 44 | -------------------------------------------------------------------------------- /test_python_toolbox/test_address_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.address_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_address_tools/sample_module_tree/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''A sample module tree for testing.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_address_tools/sample_module_tree/w/__init__.py: -------------------------------------------------------------------------------- 1 | from .x import y -------------------------------------------------------------------------------- /test_python_toolbox/test_address_tools/sample_module_tree/w/x/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_address_tools/sample_module_tree/w/x/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_address_tools/sample_module_tree/w/x/y/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_address_tools/sample_module_tree/w/x/y/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_address_tools/sample_module_tree/w/x/y/z/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_address_tools/sample_module_tree/w/x/y/z/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_binary_search/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_binary_search/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_caching/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.caching`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_caching/test_cached_type.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.caching.CachedType`.''' 5 | 6 | from python_toolbox.caching import CachedType 7 | 8 | 9 | def test(): 10 | '''Test basic workings of `CachedType`.''' 11 | class A(metaclass=CachedType): 12 | def __init__(self, a=1, b=2, *args, **kwargs): 13 | pass 14 | 15 | assert A() is A(1) is A(b=2) is A(1, 2) is A(1, b=2) 16 | assert A() is not A(3) is not A(b=7) is not A(1, 2, 'meow') is not A(x=9) 17 | 18 | def test_keyword_only_separator_and_annotations(): 19 | class B(metaclass=CachedType): 20 | def __init__(self, a: int, b: float, *, c: 'lol' = 7) -> None: 21 | pass 22 | 23 | assert B(1, 2) is B(b=2, a=1, c=7) is not B(b=2, a=1, c=8) -------------------------------------------------------------------------------- /test_python_toolbox/test_cheat_hashing.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.abc_tools.AbstractStaticMethod`.''' 5 | 6 | import copy 7 | 8 | from python_toolbox.cheat_hashing import cheat_hash 9 | 10 | 11 | def test_cheat_hash(): 12 | '''Test `cheat_hash` on various objects.''' 13 | 14 | things = [ 15 | 1, 16 | 7, 17 | 4.5, 18 | [1, 2, 3.4], 19 | (1, 2, 3.4), 20 | {1: 2, 3: 4.5}, 21 | {1, 2, 3.4}, 22 | [1, [1, 2], 3], 23 | [1, {frozenset((1, 2)): 'meow'}, 3], 24 | sum, 25 | None, 26 | (None, {None: None}) 27 | ] 28 | 29 | things_copy = copy.deepcopy(things) 30 | 31 | for thing, thing_copy in zip(things, things_copy): 32 | assert cheat_hash(thing) == cheat_hash(thing) == \ 33 | cheat_hash(thing_copy) == cheat_hash(thing_copy) 34 | -------------------------------------------------------------------------------- /test_python_toolbox/test_color_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_color_tools/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_color_tools/test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import color_tools 5 | 6 | def test(): 7 | ''' ''' 8 | assert color_tools.mix_rgb(0.5, (0, 1, 0.5), (1, 0, 0)) == (0.5, 0.5, 0.25) -------------------------------------------------------------------------------- /test_python_toolbox/test_combi/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_combi/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_combi/test_calculating_length.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.combi.perming.calculating_length import * 5 | 6 | def test_recurrent_perm_space_length(): 7 | assert calculate_length_of_recurrent_perm_space(3, (3, 1, 1)) == 13 8 | assert calculate_length_of_recurrent_perm_space(2, (3, 2, 2, 1)) == 15 9 | assert calculate_length_of_recurrent_perm_space(3, (3, 2, 2, 1)) == 52 10 | 11 | 12 | def test_recurrent_comb_space_length(): 13 | assert calculate_length_of_recurrent_comb_space(3, (3, 1, 1)) == 4 14 | assert calculate_length_of_recurrent_comb_space(2, (3, 2, 2, 1)) == 9 15 | assert calculate_length_of_recurrent_comb_space(3, (3, 2, 2, 1)) == 14 16 | -------------------------------------------------------------------------------- /test_python_toolbox/test_combi/test_chain_space.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox.combi import * 7 | 8 | 9 | def test_chain_spaces(): 10 | chain_space = ChainSpace((range(3), 'meow', range(22, 19, -1))) 11 | assert tuple(chain_space) == (0, 1, 2, 'm', 'e', 'o', 'w', 22, 21, 20) 12 | assert len(chain_space) == chain_space.length == 10 13 | assert bool(chain_space) is True 14 | for i, item in enumerate(chain_space): 15 | assert chain_space[i] == item 16 | assert chain_space.index(item) == i 17 | 18 | assert chain_space == chain_space 19 | 20 | assert 0 in chain_space 21 | assert 'm' in chain_space 22 | assert [] not in chain_space 23 | 24 | with cute_testing.RaiseAssertor(ValueError): chain_space.index('nope') 25 | with cute_testing.RaiseAssertor(IndexError): chain_space[-11] 26 | with cute_testing.RaiseAssertor(IndexError): chain_space[-110] 27 | with cute_testing.RaiseAssertor(IndexError): chain_space[11] 28 | with cute_testing.RaiseAssertor(IndexError): chain_space[1100] 29 | 30 | assert chain_space[-1] == 20 31 | assert chain_space[-2] == 21 32 | assert chain_space[-10] == 0 33 | 34 | assert not ChainSpace(()) 35 | 36 | 37 | -------------------------------------------------------------------------------- /test_python_toolbox/test_combi/test_misc.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | from python_toolbox import math_tools 6 | 7 | from python_toolbox import combi 8 | 9 | 10 | def test(): 11 | assert combi.misc.get_short_factorial_string(7) == \ 12 | str(math_tools.factorial(7)) 13 | assert combi.misc.get_short_factorial_string(7, minus_one=True) == \ 14 | str(math_tools.factorial(7) - 1) 15 | 16 | assert combi.misc.get_short_factorial_string(17) == '17!' 17 | assert combi.misc.get_short_factorial_string(17, minus_one=True) == \ 18 | '17! - 1' 19 | 20 | assert combi.misc.get_short_factorial_string(float('inf')) == \ 21 | '''float('inf')''' 22 | assert combi.misc.get_short_factorial_string(float('inf'), 23 | minus_one=True) == '''float('inf')''' 24 | 25 | def test_things_in_root_namespace(): 26 | combi.binomial 27 | combi.Bag 28 | combi.OrderedBag 29 | combi.FrozenBag 30 | combi.FrozenOrderedBag -------------------------------------------------------------------------------- /test_python_toolbox/test_combi/test_selection_space.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.combi import * 5 | 6 | 7 | def test(): 8 | selection_space = SelectionSpace(range(5)) 9 | assert len(tuple(selection_space)) == len(selection_space) == 2 ** 5 10 | assert selection_space[0] == set() 11 | assert selection_space[-1] == set(range(5)) 12 | 13 | for i, selection in enumerate(selection_space): 14 | assert selection in selection_space 15 | assert selection_space.index(selection) == i 16 | 17 | assert (1, 6) not in selection_space 18 | assert 'foo' not in selection_space 19 | assert 7 not in selection_space 20 | assert (1, 3, 4) in selection_space 21 | assert selection_space 22 | assert repr(selection_space) == '' 23 | assert {SelectionSpace(range(4)), SelectionSpace(range(4)), 24 | SelectionSpace(range(5)), SelectionSpace(range(4))} == \ 25 | {SelectionSpace(range(4)), SelectionSpace(range(5))} 26 | 27 | assert SelectionSpace(range(5)) == SelectionSpace(range(5)) 28 | assert SelectionSpace(range(5)) != SelectionSpace(range(4)) 29 | assert SelectionSpace(range(5)) != SelectionSpace(range(5, 0, -1)) 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /test_python_toolbox/test_combi/test_variations_meta.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import pickle 5 | import itertools 6 | 7 | from python_toolbox import cute_iter_tools 8 | from python_toolbox import sequence_tools 9 | 10 | from python_toolbox import combi 11 | from python_toolbox.combi import * 12 | 13 | infinity = float('inf') 14 | infinities = (infinity, -infinity) 15 | 16 | 17 | def test(): 18 | assert len(combi.perming.variations.variation_selection_space) == \ 19 | 2 ** len(combi.perming.variations.Variation) 20 | 21 | for i, variation_selection in \ 22 | enumerate(combi.perming.variations.variation_selection_space): 23 | assert isinstance(variation_selection, 24 | combi.perming.variations.VariationSelection) 25 | assert combi.perming.variations.variation_selection_space. \ 26 | index(variation_selection) == i 27 | assert cute_iter_tools.is_sorted(variation_selection.variations) 28 | 29 | assert isinstance(variation_selection.is_allowed, bool) 30 | 31 | 32 | -------------------------------------------------------------------------------- /test_python_toolbox/test_context_management/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Test package for the `python_toolbox.context_manager` module.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.cute_iter_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_call_until_exception.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import collections 5 | 6 | from python_toolbox.cute_iter_tools import call_until_exception 7 | 8 | 9 | def test(): 10 | 11 | assert list(call_until_exception(collections.deque(range(7)).popleft, 12 | IndexError)) == list(range(7)) 13 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_enumerate.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `cute_iter_tools.enumerate`.''' 5 | 6 | 7 | from python_toolbox import nifty_collections 8 | from python_toolbox import cute_iter_tools 9 | 10 | 11 | def test(): 12 | '''Test the basic workings of `cute_iter_tools.enumerate`.''' 13 | 14 | for i, j in cute_iter_tools.enumerate(range(5)): 15 | assert i == j 16 | 17 | for i, j in cute_iter_tools.enumerate(range(5), reverse_index=True): 18 | assert i + j == 4 19 | 20 | for i, j in cute_iter_tools.enumerate(range(4, -1, -1), reverse_index=True): 21 | assert i == j 22 | 23 | lazy_tuple = cute_iter_tools.enumerate(range(4, -1, -1), reverse_index=True, 24 | lazy_tuple=True) 25 | 26 | assert isinstance(lazy_tuple, nifty_collections.LazyTuple) 27 | assert not lazy_tuple.collected_data 28 | 29 | for i, j in lazy_tuple: 30 | assert i == j 31 | 32 | assert lazy_tuple.is_exhausted -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_fill.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import itertools 5 | import types 6 | 7 | from python_toolbox import nifty_collections 8 | 9 | from python_toolbox.cute_iter_tools import fill 10 | 11 | 12 | 13 | def test(): 14 | assert fill(range(4), fill_value='Meow', length=7, sequence_type=list) == [ 15 | 0, 1, 2, 3, 'Meow', 'Meow', 'Meow' 16 | ] 17 | assert isinstance(fill(range(4), fill_value='Meow'), types.GeneratorType) 18 | 19 | assert fill(range(4), fill_value_maker=iter(range(10)).__next__, length=7, 20 | sequence_type=tuple) == (0, 1, 2, 3, 0, 1, 2) 21 | 22 | lazy_tuple = fill(range(4), fill_value='Meow', length=7, lazy_tuple=True) 23 | 24 | assert isinstance(lazy_tuple, nifty_collections.LazyTuple) 25 | assert not lazy_tuple.collected_data 26 | 27 | assert lazy_tuple == (0, 1, 2, 3, 'Meow', 'Meow', 'Meow') 28 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_get_items.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `cute_iter_tools.get_items`.''' 5 | 6 | import itertools 7 | 8 | 9 | from python_toolbox.cute_iter_tools import get_items 10 | 11 | 12 | 13 | def test(): 14 | '''Test the basic workings of `get_items`.''' 15 | 16 | iterable = iter(range(10)) 17 | assert get_items(iterable, 3) == (0, 1, 2) 18 | assert get_items(iterable, 0) == () 19 | assert get_items(iterable, 2) == (3, 4) 20 | assert get_items(iterable, 4) == (5, 6, 7, 8) 21 | assert get_items(iterable, 3) == (9,) 22 | assert get_items(iterable, 3) == () 23 | assert get_items(iterable, 4) == () -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_get_length.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `cute_iter_tools.get_length`.''' 5 | 6 | from python_toolbox.cute_iter_tools import get_length 7 | 8 | 9 | def test(): 10 | '''Test the basic workings of `get_length`.''' 11 | assert get_length(list(range(3))) == 3 12 | assert get_length(range(4)) == 4 13 | assert get_length(set(range(5))) == 5 14 | assert get_length(iter(set(range(16, 10, -1)))) == 6 15 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_get_ratio.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from __future__ import division 5 | 6 | from python_toolbox import cute_iter_tools 7 | 8 | 9 | def test(): 10 | ratio = cute_iter_tools.get_ratio('real', [1, 2, 3, 1j, 2j, 3j, 4j]) 11 | assert ratio == 3 / 7 12 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_get_single_if_any.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox.cute_iter_tools import get_single_if_any 7 | 8 | 9 | def test_get_single_if_any(): 10 | 11 | assert get_single_if_any(()) is get_single_if_any([]) is \ 12 | get_single_if_any({}) is get_single_if_any(iter({})) is \ 13 | get_single_if_any('') is None 14 | 15 | assert get_single_if_any(('g',)) == get_single_if_any(['g']) == \ 16 | get_single_if_any({'g'}) == get_single_if_any(iter({'g'})) == \ 17 | get_single_if_any('g') == 'g' 18 | 19 | with cute_testing.RaiseAssertor(): 20 | get_single_if_any(('g', 'e', 'e')) 21 | 22 | with cute_testing.RaiseAssertor(): 23 | get_single_if_any('gee') 24 | 25 | assert get_single_if_any('gee', exception_on_multiple=False) == 'g' 26 | assert get_single_if_any('gee', none_on_multiple=True) is None 27 | assert get_single_if_any('gee', none_on_multiple=True, 28 | exception_on_multiple=False) is None -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_is_iterable.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.cute_iter_tools.is_iterable`.''' 5 | 6 | from python_toolbox import cute_iter_tools 7 | from python_toolbox.cute_iter_tools import is_iterable 8 | 9 | 10 | infinity = float('inf') 11 | 12 | 13 | def test(): 14 | '''Test basic workings of `is_iterable`.''' 15 | 16 | iterables = [ 17 | [1, 2, 3], 18 | (1, 2), 19 | {}, 20 | (), 21 | [[1]], 22 | 'asdfasdf', 23 | '' 24 | ] 25 | 26 | non_iterables = [ 27 | dict, 28 | list, 29 | type, 30 | None, 31 | True, 32 | False, 33 | Exception, 34 | lambda x: x 35 | ] 36 | 37 | for iterable in iterables: 38 | assert is_iterable(iterable) 39 | 40 | for non_iterable in non_iterables: 41 | assert not is_iterable(non_iterable) -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_is_sorted.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import pytest 5 | 6 | from python_toolbox import nifty_collections 7 | from python_toolbox import cute_iter_tools 8 | from python_toolbox.cute_iter_tools import is_sorted 9 | 10 | 11 | infinity = float('inf') 12 | 13 | 14 | def test(): 15 | r = (1, 2, 3, 7, 10) 16 | assert is_sorted(r) is True 17 | assert is_sorted(r, rising=False) is False 18 | assert is_sorted(r[::-1], rising=False) is True 19 | assert is_sorted(r, strict=True) is True 20 | assert is_sorted(r, rising=False, strict=True) is False 21 | assert is_sorted(r, key=lambda x: x % 3) is False 22 | assert is_sorted(r, rising=False, key=lambda x: x % 3) is False 23 | assert is_sorted(r, key=lambda x: -x) is False 24 | assert is_sorted(r, rising=False, key=lambda x: -x) is True 25 | assert is_sorted(r, rising=False, strict=True, key=lambda x: -x) is True 26 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_pop_iterators.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import collections 5 | from python_toolbox import nifty_collections 6 | 7 | from python_toolbox.cute_iter_tools import (iterate_pop, iterate_popitem, 8 | iterate_popleft) 9 | 10 | 11 | def test(): 12 | 13 | deque = collections.deque(range(10)) 14 | assert tuple(iterate_pop(deque)) == tuple(range(9, -1, -1)) 15 | assert not deque 16 | 17 | deque = collections.deque(range(10)) 18 | assert tuple(iterate_popleft(deque)) == tuple(range(10)) 19 | assert not deque 20 | 21 | dict_ = {1: 2, 3: 4, 5: 6,} 22 | assert dict(iterate_popitem(dict_)) == {1: 2, 3: 4, 5: 6,} 23 | assert not dict_ 24 | 25 | lazy_tuple = iterate_pop(list(range(5)), lazy_tuple=True) 26 | assert isinstance(lazy_tuple, nifty_collections.LazyTuple) 27 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_iter_tools/test_pushback_iterator.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox.cute_iter_tools import PushbackIterator 7 | 8 | 9 | def test_pushback_iterator(): 10 | 11 | pushback_iterator = PushbackIterator(iter([1, 2, 3])) 12 | assert next(pushback_iterator) == 1 13 | assert next(pushback_iterator) == 2 14 | pushback_iterator.push_back() 15 | assert next(pushback_iterator) == 2 16 | assert next(pushback_iterator) == 3 17 | pushback_iterator.push_back() 18 | assert next(pushback_iterator) == 3 19 | with cute_testing.RaiseAssertor(StopIteration): 20 | next(pushback_iterator) 21 | pushback_iterator.push_back() 22 | assert next(pushback_iterator) == 3 23 | 24 | with cute_testing.RaiseAssertor(StopIteration): 25 | next(pushback_iterator) -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_profile/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.cute_profile`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_profile/shared.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Defines tools for testing `python_toolbox.cute_profile`.''' 5 | 6 | import sys 7 | 8 | from python_toolbox.sys_tools import OutputCapturer 9 | from python_toolbox import logic_tools 10 | 11 | segments = ('function calls in', 'Ordered by', 'ncalls', 'tottime', 'percall', 12 | 'cumtime') 13 | 14 | 15 | def call_and_check_if_profiled(f): 16 | '''Call the function `f` and return whether it profiled itself.''' 17 | 18 | with OutputCapturer() as output_capturer: 19 | f() 20 | 21 | output = output_capturer.output 22 | 23 | segments_found = [(segment in output) for segment in segments] 24 | 25 | if not logic_tools.all_equivalent(segments_found): 26 | raise Exception("Some segments were found, but some weren't; can't " 27 | "know if this was a profiled call or not. Possibly " 28 | "some of our segments are wrong.") 29 | 30 | return segments_found[0] 31 | 32 | 33 | -------------------------------------------------------------------------------- /test_python_toolbox/test_cute_testing/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.cute_testing`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_dict_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Test package for `python_toolbox.dict_tools`.''' 5 | 6 | -------------------------------------------------------------------------------- /test_python_toolbox/test_dict_tools/test_devour_items.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Test package for `dict_tools.devour_items`.''' 5 | 6 | from python_toolbox import dict_tools 7 | 8 | 9 | def test(): 10 | '''Test the basic workings of `devour_items`.''' 11 | my_dict = {1: 2, 3: 4, 5: 6,} 12 | assert set(dict_tools.devour_items(my_dict)) == {(1, 2), (3, 4), (5, 6)} 13 | assert not my_dict 14 | -------------------------------------------------------------------------------- /test_python_toolbox/test_dict_tools/test_devour_keys.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Test package for `dict_tools.devour_keys`.''' 5 | 6 | from python_toolbox import dict_tools 7 | 8 | 9 | def test(): 10 | '''Test the basic workings of `devour_keys`.''' 11 | my_dict = {1: 2, 3: 4, 5: 6,} 12 | assert set(dict_tools.devour_keys(my_dict)) == {1, 3, 5} 13 | assert not my_dict 14 | -------------------------------------------------------------------------------- /test_python_toolbox/test_dict_tools/test_get_sorted_values.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import dict_tools 5 | 6 | 7 | def test(): 8 | d = {1: 'a', 2: 'd', 3: 'j', 4: 'b',} 9 | assert dict_tools.get_sorted_values(d) == ('a', 'd', 'j', 'b') 10 | assert dict_tools.get_sorted_values(d, key=lambda x: -x) == \ 11 | ('b', 'j', 'd', 'a') -------------------------------------------------------------------------------- /test_python_toolbox/test_dict_tools/test_remove_keys.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import numbers 5 | 6 | from python_toolbox.dict_tools import remove_keys 7 | 8 | 9 | def test(): 10 | '''Test the basic workings of `sum_dicts`.''' 11 | origin_dict = {1: 2, 3: 4, 5: 6, 7: 8, 9: 10, 11: 12, 13: 14, 15: 16,} 12 | 13 | not_divide_by_three_dict = dict(origin_dict) 14 | remove_keys(not_divide_by_three_dict, range(0, 50, 3)) 15 | assert not_divide_by_three_dict == {1: 2, 5: 6, 7: 8, 11: 12, 13: 14} 16 | 17 | below_ten_dict = dict(origin_dict) 18 | remove_keys(below_ten_dict, lambda value: value >= 10) 19 | assert below_ten_dict == {1: 2, 3: 4, 5: 6, 7: 8, 9: 10} 20 | 21 | class HoledNumbersContainer: 22 | '''Contains only numbers that have a digit with a hole in it.''' 23 | def __contains__(self, number): 24 | if not isinstance(number, numbers.Integral): 25 | return False 26 | return bool(set(str(number)).intersection({'0', '4', '6', '8', '9'})) 27 | 28 | 29 | non_holed_numbers_dict = dict(origin_dict) 30 | remove_keys(non_holed_numbers_dict, HoledNumbersContainer()) 31 | assert non_holed_numbers_dict == {1: 2, 3: 4, 5: 6, 7: 8, 11: 12, 13: 14, 32 | 15: 16,} 33 | 34 | -------------------------------------------------------------------------------- /test_python_toolbox/test_dict_tools/test_reverse.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox import dict_tools 7 | 8 | 9 | def test(): 10 | assert dict_tools.reverse({'one': 1, 'two': 2, 'three': 3}) == \ 11 | {1: 'one', 2: 'two', 3: 'three'} 12 | assert dict_tools.reverse({}) == {} 13 | with cute_testing.RaiseAssertor(): 14 | dict_tools.reverse({1: 0, 2: 0}) 15 | with cute_testing.RaiseAssertor(): 16 | dict_tools.reverse({1: []}) 17 | -------------------------------------------------------------------------------- /test_python_toolbox/test_dict_tools/test_sum_dicts.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import dict_tools 5 | 6 | 7 | def test(): 8 | '''Test the basic workings of `sum_dicts`.''' 9 | dict_1 = {1: 2, 3: 4, 5: 6, 1j: 1, 2j: 1, 3j: 1,} 10 | dict_2 = {'a': 'b', 'c': 'd', 'e': 'f', 2j: 2, 3j: 2,} 11 | dict_3 = {'A': 'B', 'C': 'D', 'E': 'F', 3j: 3,} 12 | 13 | assert dict_tools.sum_dicts((dict_1, dict_2, dict_3)) == { 14 | 1: 2, 3: 4, 5: 6, 15 | 'a': 'b', 'c': 'd', 'e': 'f', 16 | 'A': 'B', 'C': 'D', 'E': 'F', 17 | 1j: 1, 2j: 2, 3j: 3, 18 | } 19 | 20 | assert dict_tools.sum_dicts((dict_3, dict_2, dict_1)) == { 21 | 1: 2, 3: 4, 5: 6, 22 | 'a': 'b', 'c': 'd', 'e': 'f', 23 | 'A': 'B', 'C': 'D', 'E': 'F', 24 | 1j: 1, 2j: 1, 3j: 1, 25 | } -------------------------------------------------------------------------------- /test_python_toolbox/test_emitting/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_emitting/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_emitting/test_emitter.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import misc_tools 5 | 6 | from python_toolbox import emitting 7 | 8 | 9 | def test(): 10 | emitter_1 = emitting.Emitter() 11 | emitter_2 = emitting.Emitter(inputs=emitter_1) # Single item without tuple 12 | emitter_0 = emitting.Emitter(outputs=(emitter_1,)) 13 | 14 | @misc_tools.set_attributes(call_counter=0) 15 | def my_function(): 16 | my_function.call_counter += 1 17 | 18 | emitter_1.add_output(my_function) 19 | 20 | assert my_function.call_counter == 0 21 | emitter_1.emit() 22 | assert my_function.call_counter == 1 23 | emitter_1.emit() 24 | emitter_1.emit() 25 | emitter_1.emit() 26 | assert my_function.call_counter == 4 27 | emitter_0.emit() 28 | assert my_function.call_counter == 5 29 | emitter_0.emit() 30 | emitter_0.emit() 31 | emitter_0.emit() 32 | assert my_function.call_counter == 8 33 | emitter_2.emit() 34 | assert my_function.call_counter == 8 35 | emitter_2.emit() 36 | emitter_2.emit() 37 | emitter_2.emit() 38 | assert my_function.call_counter == 8 39 | 40 | 41 | -------------------------------------------------------------------------------- /test_python_toolbox/test_exceptions/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.exceptions`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_exceptions/test_cute_base_exception.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `exceptions.CuteBaseException`.''' 5 | 6 | from python_toolbox import cute_testing 7 | 8 | from python_toolbox.exceptions import CuteBaseException, CuteException 9 | 10 | 11 | def test(): 12 | 13 | try: 14 | raise CuteBaseException 15 | except BaseException as base_exception: 16 | assert base_exception.message == '' 17 | else: 18 | raise cute_testing.Failure 19 | 20 | try: 21 | raise CuteBaseException() 22 | except BaseException as base_exception: 23 | assert base_exception.message == '' 24 | else: 25 | raise cute_testing.Failure 26 | 27 | 28 | class MyBaseException(CuteBaseException): 29 | '''My hovercraft is full of eels.''' 30 | 31 | 32 | try: 33 | raise MyBaseException() 34 | except BaseException as base_exception: 35 | assert base_exception.message == '''My hovercraft is full of eels.''' 36 | else: 37 | raise cute_testing.Failure 38 | 39 | try: 40 | raise MyBaseException 41 | except BaseException as base_exception: 42 | assert base_exception.message == '''My hovercraft is full of eels.''' 43 | else: 44 | raise cute_testing.Failure -------------------------------------------------------------------------------- /test_python_toolbox/test_exceptions/test_cute_exception.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `exceptions.CuteException`.''' 5 | 6 | from python_toolbox import cute_testing 7 | 8 | from python_toolbox.exceptions import CuteBaseException, CuteException 9 | 10 | 11 | def test(): 12 | 13 | try: 14 | raise CuteException 15 | except Exception as exception: 16 | assert exception.message == '' 17 | else: 18 | raise cute_testing.Failure 19 | 20 | try: 21 | raise CuteException() 22 | except Exception as exception: 23 | assert exception.message == '' 24 | else: 25 | raise cute_testing.Failure 26 | 27 | 28 | class MyException(CuteException): 29 | '''My hovercraft is full of eels.''' 30 | 31 | 32 | try: 33 | raise MyException() 34 | except Exception as exception: 35 | assert exception.message == '''My hovercraft is full of eels.''' 36 | else: 37 | raise cute_testing.Failure 38 | 39 | try: 40 | raise MyException 41 | except Exception as exception: 42 | assert exception.message == '''My hovercraft is full of eels.''' 43 | else: 44 | raise cute_testing.Failure -------------------------------------------------------------------------------- /test_python_toolbox/test_file_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_file_tools/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_freezing/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.freezing`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_future_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_future_tools/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_human_names.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import human_names 5 | 6 | 7 | def test(): 8 | assert 'John' in human_names.name_list 9 | assert 'Janet' in human_names.name_list -------------------------------------------------------------------------------- /test_python_toolbox/test_import_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.import_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_import_tools/test_exists/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.import_tools.exists`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_import_tools/test_exists/test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.import_tools.exists`.''' 5 | 6 | import pytest 7 | 8 | from python_toolbox import import_tools 9 | from python_toolbox.import_tools import exists 10 | 11 | def test(): 12 | '''Test the basic workings of `exists`.''' 13 | assert not exists('adfgadbnv5nrn') 14 | assert not exists('45gse_e5b6_DFDF') 15 | assert not exists('VWEV65hnrt___a4') 16 | assert exists('email') 17 | assert exists('re') 18 | assert exists('sys') 19 | pytest.raises(NotImplementedError, 20 | lambda: exists('email.encoders')) -------------------------------------------------------------------------------- /test_python_toolbox/test_introspection_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.introspection_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_introspection_tools/test_get_default_args_dict.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing for `python_toolbox.introspection_tools.get_default_args_dict`.''' 5 | 6 | from __future__ import generator_stop 7 | 8 | from python_toolbox.introspection_tools import get_default_args_dict 9 | from python_toolbox.nifty_collections import OrderedDict 10 | 11 | 12 | def test(): 13 | '''Test the basic workings of `get_default_args_dict`.''' 14 | def f(a, b, c=3, d=4): 15 | pass 16 | 17 | assert get_default_args_dict(f) == \ 18 | OrderedDict((('c', 3), ('d', 4))) 19 | 20 | 21 | def test_generator(): 22 | '''Test `get_default_args_dict` on a generator function.''' 23 | def f(a, meow='frr', d={}): 24 | yield None 25 | 26 | assert get_default_args_dict(f) == \ 27 | OrderedDict((('meow', 'frr'), ('d', {}))) 28 | 29 | 30 | def test_empty(): 31 | '''Test `get_default_args_dict` on a function with no defaultful args.''' 32 | def f(a, b, c, *args, **kwargs): 33 | pass 34 | 35 | assert get_default_args_dict(f) == \ 36 | OrderedDict() 37 | -------------------------------------------------------------------------------- /test_python_toolbox/test_logic_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.logic_tools`.''' -------------------------------------------------------------------------------- /test_python_toolbox/test_math_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_math_tools/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_math_tools/test_binomial.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import sys 5 | 6 | from python_toolbox.math_tools import binomial 7 | 8 | 9 | def test(): 10 | assert binomial(7, 3) == 35 11 | assert binomial(0, 0) == 1 12 | assert binomial(1, 0) == 1 13 | assert binomial(0, 1) == 0 14 | assert binomial(1543, 634) == 127103521979248139661884595050302692072114625333816461647571438364482801578062268185939019831927710543644891679108093639691467979466411177318250931548804667267192030646116875003881007119966764992383016174407823444352165249728639847571592229832113238415348489512831060701471487834812550521403788703534654431344329462541634971732197170414906629071055802381322184009159362499960475076746698583466181504060523973736761406471112019069930703149760846502951040 -------------------------------------------------------------------------------- /test_python_toolbox/test_math_tools/test_get_mean.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import sys 5 | 6 | from python_toolbox.math_tools import get_mean 7 | 8 | 9 | def test_mean(): 10 | assert get_mean((1, 2, 3)) == 2 11 | assert get_mean((1, 2, 3, 4)) == 2.5 12 | assert get_mean((1, 2, 3, 4, 5)) == 3 13 | assert get_mean((-1, -2, -3, -4, -5)) == -3 14 | assert get_mean((-0.5, -2, -3, -4, -5.5)) == -3 15 | assert 1821.666 < get_mean((1000, 10000.5, -2, -4, -14, -50.5)) < 1821.667 -------------------------------------------------------------------------------- /test_python_toolbox/test_math_tools/test_get_median.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import sys 5 | 6 | import pytest 7 | 8 | from python_toolbox.math_tools import get_median 9 | 10 | 11 | def test_median(): 12 | assert get_median((1, 2, 3)) == 2 13 | assert get_median((1, 2, 3, 4)) == 2.5 14 | assert get_median((1, 2, 3, 4, 5)) == 3 15 | assert get_median((-1, -2, -3, -4, -5)) == -3 16 | assert get_median((-0.5, -2, -3, -4, -5.5)) == -3 17 | assert get_median((-0.5, -2, -3, -4, -50.5)) == -3 18 | assert get_median((10000.5, -2, -3, -14, -50.5)) == -3 19 | assert get_median((10000.5, -2, -4, -14, -50.5)) == -4 20 | assert get_median((1000, 1000, 10000.5, -2, -4, -14, -50.5)) == -2 -------------------------------------------------------------------------------- /test_python_toolbox/test_math_tools/test_restrict_number_to_range.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | 5 | import sys 6 | 7 | 8 | from python_toolbox.math_tools import restrict_number_to_range 9 | 10 | 11 | def test_restrict_number_to_range(): 12 | my_restrict = lambda number: restrict_number_to_range(number, 13 | low_cutoff=3.5, 14 | high_cutoff=7.8) 15 | assert list(map(my_restrict, range(10))) == [ 16 | 3.5, 3.5, 3.5, 3.5, 4, 5, 6, 7, 7.8, 7.8 17 | ] -------------------------------------------------------------------------------- /test_python_toolbox/test_math_tools/test_sequences.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.math_tools import * 5 | 6 | 7 | def test_abs_stirling(): 8 | assert tuple(abs_stirling(0, i) for i in range(-1, 2)) == (0, 1, 0, ) 9 | assert tuple(abs_stirling(1, i) for i in range(-1, 3)) == (0, 0, 1, 0, ) 10 | assert tuple(abs_stirling(2, i) for i in range(-1, 4)) == (0, 0, 1, 1, 0) 11 | assert tuple(abs_stirling(3, i) for i in range(-1, 5)) == (0, 0, 2, 3, 1, 12 | 0) 13 | assert tuple(abs_stirling(4, i) for i in range(-1, 6)) == (0, 0, 6, 11, 6, 14 | 1, 0) 15 | assert tuple(abs_stirling(5, i) for i in range(-1, 7)) == (0, 0, 24, 50, 16 | 35, 10, 1, 0) 17 | 18 | assert abs_stirling(200, 50) == 525010571470323062300307763288024029929662200077890908912803398279686186838073914722860457474159887042512346530620756231465891831828236378945598188429630326359716300315479010640625526167635598138598969330736141913019490812196987045505021083120744610946447254207252791218757775609887718753072629854788563118348792912143712216969484697600 19 | # The number was verified with Wolfram Mathematica. 20 | 21 | -------------------------------------------------------------------------------- /test_python_toolbox/test_math_tools/test_types.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.math_tools import (infinity, PossiblyInfiniteIntegral, 5 | PossiblyInfiniteReal) 6 | 7 | 8 | def test_possibly_infinite_integral(): 9 | matches = [0, 1, -100, 102341, 232, 10 ** 1000, infinity, -infinity, 10 | True, False] 11 | non_matches = [0.3, 1j, -100.313, None, 'meow', (1, 2)] 12 | for match in matches: 13 | assert isinstance(match, PossiblyInfiniteIntegral) 14 | for non_match in non_matches: 15 | assert not isinstance(non_match, PossiblyInfiniteIntegral) 16 | 17 | 18 | def test_possibly_infinite_real(): 19 | matches = [0, 1, -100, 102341, 232, 10 ** 1000, infinity, -infinity, 20 | 0.2, 1.5, -100.7, 102341.345, 232.23424, True, False] 21 | non_matches = [1j, None, 'meow', (1, 2)] 22 | for match in matches: 23 | assert isinstance(match, PossiblyInfiniteReal) 24 | for non_match in non_matches: 25 | assert not isinstance(non_match, PossiblyInfiniteReal) 26 | -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.misc_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_add_extension_if_plain.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import temp_file_tools 5 | 6 | from python_toolbox.misc_tools import add_extension_if_plain 7 | 8 | 9 | def test(): 10 | assert str(add_extension_if_plain(r'''c:\hello.zip''', '.ogg')) == \ 11 | r'''c:\hello.zip''' 12 | assert str(add_extension_if_plain(r'''c:\hello''', '.ogg')) == \ 13 | r'''c:\hello.ogg''' 14 | assert str(add_extension_if_plain(r'''c:\hello''', '.mkv')) == \ 15 | r'''c:\hello.mkv''' -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_decimal_number_from_string.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox.misc_tools import decimal_number_from_string 7 | 8 | 9 | def test(): 10 | assert decimal_number_from_string('7') == 7 11 | assert type(decimal_number_from_string('7')) == int 12 | assert decimal_number_from_string('-12.34') == -12.34 13 | assert type(decimal_number_from_string('-12.34')) == float 14 | with cute_testing.RaiseAssertor(): 15 | decimal_number_from_string(31412) 16 | with cute_testing.RaiseAssertor(): 17 | decimal_number_from_string('JJfa') -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_find_clear_place_on_circle.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import pytest 5 | 6 | from python_toolbox.misc_tools import find_clear_place_on_circle 7 | 8 | def test_wraparound(): 9 | '''Test when clear place is on the wraparound.''' 10 | result = find_clear_place_on_circle((0.3, 0.5, 0.8), 1) 11 | assert abs(result - 0.05) <= 0.001 -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_general_product.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.misc_tools import general_product 5 | 6 | 7 | def test(): 8 | assert general_product((1, 2, 3)) == 6 9 | assert general_product((1, 2, 3, 4)) == 24 10 | assert general_product(((0, 1), 2, 3)) == (0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 11 | 1) 12 | assert general_product((2, 3), start=(0, 1)) == (0, 1, 0, 1, 0, 1, 0, 1, 0, 13 | 1, 0, 1) 14 | -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_general_sum.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.misc_tools import general_sum 5 | 6 | 7 | def test(): 8 | assert general_sum((1, 2, 3)) == 6 9 | assert general_sum((1, 2, 3, 4)) == 10 10 | assert general_sum(('abra', 'ca', 'dabra')) == 'abracadabra' 11 | assert general_sum(((0, 1), (0, 2), (0, 3))) == (0, 1, 0, 2, 0, 3) 12 | 13 | assert general_sum(((0, 1), (0, 2), (0, 3)), start=(9,)) == (9, 0, 1, 0, 14 | 2, 0, 3) -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_get_mro_depth_of_method.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.misc_tools import get_mro_depth_of_method 5 | 6 | 7 | def test(): 8 | '''Test the basic workings of `get_mro_depth_of_method`.''' 9 | class A: 10 | def a_method(self): 11 | pass 12 | 13 | class B(A): 14 | def b_method(self): 15 | pass 16 | 17 | class C(A): 18 | def c_method(self): 19 | pass 20 | 21 | class D: 22 | def d_method(self): 23 | pass 24 | 25 | class E(B, D, C): 26 | def e_method(self): 27 | pass 28 | 29 | assert get_mro_depth_of_method(A, 'a_method') == 0 30 | 31 | assert get_mro_depth_of_method(B, 'a_method') == 1 32 | assert get_mro_depth_of_method(B, 'b_method') == 0 33 | 34 | assert get_mro_depth_of_method(C, 'a_method') == 1 35 | assert get_mro_depth_of_method(C, 'c_method') == 0 36 | 37 | assert get_mro_depth_of_method(D, 'd_method') == 0 38 | 39 | assert get_mro_depth_of_method(E, 'e_method') == 0 40 | assert get_mro_depth_of_method(E, 'b_method') == 1 41 | assert get_mro_depth_of_method(E, 'd_method') == 2 42 | assert get_mro_depth_of_method(E, 'c_method') == 3 43 | assert get_mro_depth_of_method(E, 'a_method') == 4 44 | -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_is_legal_variable_name.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.misc_tools import is_legal_ascii_variable_name 5 | 6 | 7 | def test(): 8 | '''Test `is_legal_variable_name` on various legal and illegal inputs.''' 9 | legals = ['qwerqw', 'wer23434f3', 'VDF4vr', '_4523ga', 'AGF___43___4_', 10 | '_', '__', '___'] 11 | illegals = ['1dgfads', 'aga`fdg', '-haeth', '4gag5h+sdfh.', '.afdg', 12 | 'fdga"adfg', 'afdga afd'] 13 | 14 | for legal in legals: 15 | assert is_legal_ascii_variable_name(legal) 16 | 17 | for illegal in illegals: 18 | assert not is_legal_ascii_variable_name(illegal) -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_is_magic_variable_name.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.misc_tools import is_magic_variable_name 5 | 6 | 7 | def test(): 8 | magic_variable_names = ('__init__', '__iter__', '__subclasscheck__') 9 | non_magic_variable_names = ('sup_yo', '__nope!__', ) 10 | for magic_variable_name in magic_variable_names: 11 | assert is_magic_variable_name(magic_variable_name) 12 | for non_magic_variable_name in non_magic_variable_names: 13 | assert not is_magic_variable_name(non_magic_variable_name) 14 | -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_is_subclass.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.misc_tools import is_subclass 5 | 6 | 7 | def test(): 8 | assert is_subclass(object, object) 9 | assert is_subclass(object, (object, str)) 10 | assert not is_subclass(object, str) 11 | 12 | assert not is_subclass(7, object) 13 | assert not is_subclass('meow', object) 14 | -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_name_mangling/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.misc_tools.name_mangling`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_name_mangling/test_repeat_getattr.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox.misc_tools import repeat_getattr 7 | 8 | 9 | class Object: 10 | def __init__(self, tag): 11 | self.tag = tag 12 | __eq__ = lambda self, other: (self.tag == other.tag) 13 | 14 | 15 | x = Object('x') 16 | x.y = Object('y') 17 | x.y.z = Object('z') 18 | x.y.meow = Object('meow') 19 | 20 | 21 | def test(): 22 | assert repeat_getattr(x, None) == repeat_getattr(x, '') == x 23 | with cute_testing.RaiseAssertor(): 24 | repeat_getattr(x, 'y') 25 | 26 | assert x != x.y != x.y.z != x.y.meow 27 | assert repeat_getattr(x, '.y') == x.y 28 | assert repeat_getattr(x, '.y.z') == x.y.z 29 | assert repeat_getattr(x, '.y.meow') == x.y.meow 30 | 31 | assert repeat_getattr(x.y, '.meow') == x.y.meow 32 | assert repeat_getattr(x.y, '.z') == x.y.z -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_non_instantiable.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox.misc_tools import NonInstantiable 7 | 8 | 9 | def test(): 10 | class MyNonInstantiable(NonInstantiable): 11 | pass 12 | 13 | with cute_testing.RaiseAssertor(exception_type=RuntimeError): 14 | MyNonInstantiable() -------------------------------------------------------------------------------- /test_python_toolbox/test_misc_tools/test_overridable_property.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox.misc_tools import OverridableProperty 7 | 8 | 9 | def test(): 10 | class A: 11 | @OverridableProperty 12 | def meow(self): 13 | return 'bark bark!' 14 | 15 | a = A() 16 | assert a.meow == 'bark bark!' 17 | assert a.meow == 'bark bark!' 18 | assert a.meow == 'bark bark!' 19 | a.meow = 'Meow indeed, ma chérie.' 20 | assert a.meow == 'Meow indeed, ma chérie.' 21 | 22 | -------------------------------------------------------------------------------- /test_python_toolbox/test_nifty_collections/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.nifty_collections`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_nifty_collections/test_cute_enum/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. -------------------------------------------------------------------------------- /test_python_toolbox/test_nifty_collections/test_frozen_dict.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import uuid 5 | import pickle 6 | import itertools 7 | import collections 8 | 9 | from python_toolbox import cute_iter_tools 10 | from python_toolbox import sequence_tools 11 | from python_toolbox import cute_testing 12 | 13 | 14 | from python_toolbox.nifty_collections import FrozenDict 15 | 16 | 17 | def test(): 18 | frozen_dict = FrozenDict({'1': 'a', '2': 'b', '3': 'c',}) 19 | assert len(frozen_dict) == 3 20 | assert set(frozen_dict) == set(frozen_dict.keys()) == set('123') 21 | assert set(frozen_dict.values()) == set('abc') 22 | assert set(frozen_dict.items()) == {('1', 'a'), ('2', 'b'), ('3', 'c'),} 23 | assert frozen_dict['1'] == 'a' 24 | with cute_testing.RaiseAssertor(exception_type=LookupError): 25 | frozen_dict['missing value'] 26 | assert {frozen_dict, frozen_dict} == {frozen_dict} 27 | assert {frozen_dict: frozen_dict} == {frozen_dict: frozen_dict} 28 | assert isinstance(hash(frozen_dict), int) 29 | 30 | assert frozen_dict.copy({'meow': 'frrr'}) == \ 31 | frozen_dict.copy(meow='frrr') == \ 32 | FrozenDict({'1': 'a', '2': 'b', '3': 'c', 'meow': 'frrr',}) 33 | 34 | assert repr(frozen_dict).startswith('FrozenDict(') 35 | 36 | assert pickle.loads(pickle.dumps(frozen_dict)) == frozen_dict -------------------------------------------------------------------------------- /test_python_toolbox/test_nifty_collections/test_lazy_tuple/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.nifty_collections.LazyTuple`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_nifty_collections/test_ordered_dict/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.nifty_collections.OrderedDict`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_nifty_collections/test_weak_key_default_dict/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `WeakKeyDefaultDict`.''' 5 | 6 | -------------------------------------------------------------------------------- /test_python_toolbox/test_nifty_collections/test_weak_key_identity_dict/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `WeakKeyIdentityDict`.''' 5 | 6 | -------------------------------------------------------------------------------- /test_python_toolbox/test_nifty_collections/test_weak_key_identity_dict/test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `WeakKeyIdentityDict`.''' 5 | 6 | import pytest 7 | 8 | from python_toolbox.nifty_collections import WeakKeyIdentityDict 9 | 10 | 11 | class WeakreffableList(list): 12 | '''A `list` subclass which can be weakreffed.''' 13 | 14 | 15 | def test(): 16 | '''Test the basic workings of `WeakKeyIdentityDict`.''' 17 | wki_dict = WeakKeyIdentityDict() 18 | my_weakreffable_list = WeakreffableList([1, 2]) 19 | wki_dict[my_weakreffable_list] = 7 20 | assert my_weakreffable_list in wki_dict 21 | assert wki_dict[my_weakreffable_list] == 7 22 | identical_weakreffable_list = WeakreffableList([1, 2]) 23 | assert identical_weakreffable_list not in wki_dict 24 | pytest.raises(KeyError, lambda: wki_dict[identical_weakreffable_list]) 25 | 26 | my_weakreffable_list.append(3) 27 | assert my_weakreffable_list in wki_dict 28 | assert wki_dict[my_weakreffable_list] == 7 29 | 30 | del wki_dict[my_weakreffable_list] 31 | assert my_weakreffable_list not in wki_dict 32 | pytest.raises(KeyError, lambda: wki_dict[my_weakreffable_list]) -------------------------------------------------------------------------------- /test_python_toolbox/test_number_encoding/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_number_encoding/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_number_encoding/test_number_encoding.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | from python_toolbox import number_encoding 6 | 7 | numbers = (234123, 63435, 2222, 643534, 34, 233) 8 | 9 | def test_number_encoding(): 10 | my_encoder = number_encoding.NumberEncoder('isogram') 11 | 12 | for number in numbers: 13 | string = my_encoder.encode(number) 14 | assert my_encoder.decode(string) == number 15 | assert set(string) <= set(my_encoder.characters) 16 | 17 | padded_string = my_encoder.encode(number, 100) 18 | assert len(padded_string) >= 100 19 | assert padded_string.endswith(string) 20 | -------------------------------------------------------------------------------- /test_python_toolbox/test_path_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_path_tools/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_path_tools/test_get_root_path_of_module.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.path_tools import get_root_path_of_module 5 | 6 | def test(): 7 | ''' ''' 8 | import email.charset 9 | assert get_root_path_of_module(email) == \ 10 | get_root_path_of_module(email.charset) 11 | 12 | import python_toolbox.path_tools 13 | assert get_root_path_of_module(python_toolbox) == \ 14 | get_root_path_of_module(python_toolbox.path_tools) 15 | -------------------------------------------------------------------------------- /test_python_toolbox/test_pickle_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.pickle_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_pickle_tools/test_compressing.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | # We're importing `pickle_module` from `pickle_tools`, so we get the exact same 5 | # pickle module it's using. (Giving it the freedom to change between `cPickle` 6 | # and `pickle`.) 7 | from python_toolbox.pickle_tools import pickle_module 8 | 9 | from python_toolbox import import_tools 10 | 11 | from python_toolbox import pickle_tools 12 | 13 | 14 | my_messy_object = ( 15 | 'Whatever', 16 | {1: 2,}, 17 | {3, 4}, 18 | frozenset([3, 4]), 19 | ((((((((((((())))))))))))), 20 | u'unicode_too', 21 | (((((3, 4, 5j))))) 22 | ) * 100 23 | 24 | def test(): 25 | compickled = pickle_tools.compickle(my_messy_object) 26 | 27 | # assert len(compickled) < len(pickle_module.dumps(my_messy_object)) 28 | # I commented out the assert above, because possibly new versions of Python 29 | # possibly have built-in compression in `pickle`. 30 | 31 | assert pickle_tools.decompickle(compickled) == my_messy_object -------------------------------------------------------------------------------- /test_python_toolbox/test_queue_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.queue_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_queue_tools/test_iterate.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `queue_tools.iterate`.''' 5 | 6 | import queue as queue_module 7 | 8 | from python_toolbox import cute_testing 9 | 10 | from python_toolbox import queue_tools 11 | 12 | 13 | def test(): 14 | '''Test `iterate`.''' 15 | queue = queue_module.Queue() 16 | queue.put(1) 17 | queue.put(2) 18 | queue.put(3) 19 | assert list(queue_tools.iterate(queue)) == [1, 2, 3] -------------------------------------------------------------------------------- /test_python_toolbox/test_random_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.random_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_random_tools/test_random_partitions.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.random_tools`.''' 5 | 6 | from python_toolbox import random_tools 7 | from python_toolbox import sequence_tools 8 | 9 | 10 | def test(): 11 | '''Test the basic workings of `random_partitions`.''' 12 | 13 | def assert_correct_members(partitions): 14 | ''' 15 | Assert that the `partitions` contain exactly all of `r`'s members. 16 | ''' 17 | members = sequence_tools.flatten(partitions) 18 | assert len(members) == len(r) 19 | assert set(members) == set(r) 20 | 21 | r = list(range(10)) 22 | 23 | for partition_size in range(1, len(r)): 24 | partitions = random_tools.random_partitions(r, partition_size) 25 | for partition in partitions[:-1]: 26 | assert len(partition) == partition_size 27 | assert len(partitions[-1]) <= partition_size 28 | assert_correct_members(partitions) 29 | 30 | for n_partitions in range(1, len(r)): 31 | partitions = random_tools.random_partitions(r, 32 | n_partitions=n_partitions) 33 | assert len(partitions) == n_partitions 34 | assert_correct_members(partitions) 35 | 36 | 37 | -------------------------------------------------------------------------------- /test_python_toolbox/test_random_tools/test_shuffled.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `random_tools.shuffled`.''' 5 | 6 | from python_toolbox import random_tools 7 | 8 | 9 | def test(): 10 | '''Test the basic workings of `shuffled`.''' 11 | my_range = list(range(50)) 12 | shuffled_list = random_tools.shuffled(my_range) 13 | assert type(my_range) is type(shuffled_list) is list 14 | 15 | # The shuffled list has the same numbers... 16 | assert set(my_range) == set(shuffled_list) 17 | 18 | # ...But in a different order... 19 | assert my_range != shuffled_list 20 | 21 | # ...And the original list was not changed. 22 | assert my_range == list(range(50)) 23 | 24 | # Immutable sequences work too: 25 | assert set(random_tools.shuffled((1, 2, 3))) == {1, 2, 3} -------------------------------------------------------------------------------- /test_python_toolbox/test_re_tools.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.re_tools`.''' 5 | 6 | import re 7 | 8 | from python_toolbox import re_tools 9 | from python_toolbox.re_tools import searchall 10 | 11 | 12 | def test_searchall(): 13 | '''Test the basic workings of `searchall`.''' 14 | s = 'asdf df sfg s' 15 | result = searchall(r'(\w+)', s) 16 | assert len(result) == 4 -------------------------------------------------------------------------------- /test_python_toolbox/test_read_write_lock/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_read_write_lock/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_read_write_lock/test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.locking import ReadWriteLock 5 | 6 | 7 | def test(): 8 | ''' ''' 9 | read_write_lock = ReadWriteLock() 10 | with read_write_lock.read: 11 | pass 12 | with read_write_lock.write: 13 | pass 14 | with read_write_lock.read as enter_return_value: 15 | assert enter_return_value is read_write_lock 16 | 17 | with read_write_lock.read: 18 | with read_write_lock.read: 19 | with read_write_lock.read: 20 | with read_write_lock.read: 21 | with read_write_lock.write: 22 | with read_write_lock.write: 23 | with read_write_lock.write: 24 | with read_write_lock.write: 25 | pass 26 | 27 | with read_write_lock.write: 28 | with read_write_lock.write: 29 | with read_write_lock.write: 30 | with read_write_lock.write: 31 | with read_write_lock.read: 32 | with read_write_lock.read: 33 | with read_write_lock.read: 34 | with read_write_lock.read: 35 | pass 36 | 37 | -------------------------------------------------------------------------------- /test_python_toolbox/test_reasoned_bool.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.reasoned_bool.ReasonedBool`.''' 5 | 6 | from python_toolbox.reasoned_bool import ReasonedBool 7 | 8 | def test(): 9 | '''Test the basic workings of `ReasonedBool`.''' 10 | assert True == ReasonedBool(True) 11 | assert True == ReasonedBool(True, "Because I feel like it") 12 | assert ReasonedBool(True) 13 | assert ReasonedBool(True, "Because I feel like it") 14 | assert bool(ReasonedBool(True)) is True 15 | assert bool(ReasonedBool(True, "Because I feel like it")) is True 16 | 17 | assert False == ReasonedBool(False) 18 | assert False == ReasonedBool(False, "Because I don't feel like it") 19 | assert not ReasonedBool(False) 20 | assert not ReasonedBool(False, "Because I don't feel like it") 21 | assert bool(ReasonedBool(False)) is False 22 | assert bool(ReasonedBool(False, "Because I don't feel like it")) is False 23 | 24 | 25 | assert ReasonedBool(True, "Meow") == ReasonedBool(True, "Woof") 26 | 27 | assert ReasonedBool(False, "Meow") == ReasonedBool(False, "Woof") 28 | -------------------------------------------------------------------------------- /test_python_toolbox/test_rst_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_rst_tools/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_rst_tools/test.py: -------------------------------------------------------------------------------- 1 | from python_toolbox import rst_tools 2 | 3 | def test(): 4 | ''' ''' 5 | html = rst_tools.rst_to_html("Title\n" 6 | "=====\n" 7 | "\n" 8 | "What's up doc?") 9 | assert "What's up doc?" in html -------------------------------------------------------------------------------- /test_python_toolbox/test_segment_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_segment_tools/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_segment_tools/test_crop_segment.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox.segment_tools import crop_segment 7 | 8 | 9 | base_segment = (10, 20) 10 | 11 | segment_to_cropped_segment = { 12 | (0, 15): (10, 15), 13 | (0, 12): (10, 12), 14 | (0, 10 ** 10): (10, 20), 15 | (5, 10 ** 10): (10, 20), 16 | (10, 10 ** 10): (10, 20), 17 | (15, 17): (15, 17), 18 | (19, 20): (19, 20), 19 | (20, 23): (20, 20), 20 | (20, 10 ** 10): (20, 20), 21 | } 22 | 23 | bad_segments = ( 24 | (0, 5), 25 | (0, 7), 26 | (23, 25), 27 | (10 ** 10, 10 ** 11) 28 | ) 29 | 30 | 31 | def test(): 32 | for segment, cropped_segment in segment_to_cropped_segment.items(): 33 | assert crop_segment(segment, base_segment) == cropped_segment 34 | for bad_segment in bad_segments: 35 | with cute_testing.RaiseAssertor(): 36 | cropped_segment(segment, base_segment) 37 | 38 | -------------------------------------------------------------------------------- /test_python_toolbox/test_segment_tools/test_merge_segments.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.segment_tools import merge_segments 5 | 6 | segments_to_fixed_segments = { 7 | (): (), 8 | ((0, 1),): ((0, 1),), 9 | ((0, 1), (0, 1)): ((0, 1),), 10 | ((0, 1), (0, 1), (0, 1)): ((0, 1),), 11 | ((0, 1), (0, 1), (3, 4)): ((0, 1), (3, 4)), 12 | ((0, 1), (0, 1), (3, 4), (4, 5)): ((0, 1), (3, 5)), 13 | ((0, 1), (0, 1), (3, 4), (4, 5), (4, 6), (6, 8), (6, 9), (6, 7), (11, 12)): 14 | ((0, 1), (3, 9), (11, 12)), 15 | ((0, 10), (4, 16), (16, 17)): ((0, 17),), 16 | ((0, 10), (4, 16), (16, 17), (19, 20), (20, 22), (21, 30), (21, 24), 17 | (100, 110)): ((0, 17), (19, 30), (100, 110)), 18 | ((0, 10), (4, 7),): ((0, 10),), 19 | ((0, 10), (4, 7), (5, 8), (4, 5), (20, 22),): ((0, 10), (20, 22),), 20 | } 21 | 22 | def test_merge_segments(): 23 | for segments, fixed_segments in segments_to_fixed_segments.items(): 24 | assert merge_segments(segments) == \ 25 | merge_segments(list(reversed(segments))) == \ 26 | fixed_segments -------------------------------------------------------------------------------- /test_python_toolbox/test_sequence_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.sequence_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_sequence_tools/test_divide_to_slices.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.sequence_tools import divide_to_slices 5 | 6 | 7 | def test(): 8 | assert divide_to_slices(range(10), 3) == \ 9 | [range(0, 4), range(4, 7), range(7, 10)] 10 | -------------------------------------------------------------------------------- /test_python_toolbox/test_sequence_tools/test_flatten.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `sequence_tools.flatten`.''' 5 | 6 | from python_toolbox.sequence_tools import flatten 7 | 8 | 9 | def test(): 10 | '''Test the basic workings of `sequence_tools.flatten`.''' 11 | assert flatten([]) == flatten(()) == [] 12 | assert flatten([[1], [2], [3]]) == flatten(([1], [2], [3])) == [1, 2, 3] 13 | assert flatten(((1,), (2,), (3,))) == flatten([(1,), (2,), (3,)]) == \ 14 | (1, 2, 3) -------------------------------------------------------------------------------- /test_python_toolbox/test_sequence_tools/test_get_recurrences.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.sequence_tools import get_recurrences 5 | 6 | 7 | def test(): 8 | assert get_recurrences([]) == get_recurrences(range(10)) == \ 9 | get_recurrences(range(100)) == {} 10 | assert get_recurrences((1, 1, 1, 2, 2, 3)) == {1: 3, 2: 2,} -------------------------------------------------------------------------------- /test_python_toolbox/test_sequence_tools/test_is_subsequence.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | from python_toolbox import sequence_tools 6 | from python_toolbox.sequence_tools import is_subsequence 7 | 8 | 9 | 10 | def test(): 11 | true_pairs = ( 12 | ([1, 2, 3, 4], [2, 3]), 13 | ([1, 2, 3, 4], (2, 3)), 14 | ([1, 2, 'meow', 3, 4], (2, 'meow', 3)), 15 | ('abracadabra', 'cad'), 16 | ('abracadabra', 'dab'), 17 | ('abracadabra', 'a'), 18 | ('abracadabra', 'ab'), 19 | ('abracadabra', 'bra'), 20 | (range(10000), (range(7, 14))), 21 | (range(10000), [99]), 22 | ) 23 | false_pairs = ( 24 | ([1, 2, 3, 4], [2, 4]), 25 | ([1, 2, 3, 4], (2, 4)), 26 | ([1, 2, 'meow', 3, 4], (2, 3)), 27 | ('abracadabra', 'cab'), 28 | ('abracadabra', 'darb'), 29 | ('abracadabra', 'z'), 30 | ('abracadabra', 'bab'), 31 | ('abracadabra', 'arb'), 32 | (range(10000), (range(14, 7, -1))), 33 | (range(100), [100]), 34 | (range(100), [109]), 35 | ) 36 | 37 | for true_pair in true_pairs: 38 | assert is_subsequence(*true_pair) 39 | for false_pair in false_pairs: 40 | assert not is_subsequence(*false_pair) -------------------------------------------------------------------------------- /test_python_toolbox/test_sequence_tools/test_pop_until.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.sequence_tools import pop_until 5 | 6 | 7 | def test(): 8 | l = list(range(7)) 9 | four = pop_until(l, condition=lambda i: i == 4) 10 | assert four == 4 11 | assert l == [0, 1, 2, 3] -------------------------------------------------------------------------------- /test_python_toolbox/test_sleek_reffing/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.sleek_reffing`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_sleek_reffing/shared.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Tools for testing `python_toolbox.sleek_reffing`.''' 5 | 6 | import weakref 7 | 8 | from python_toolbox import misc_tools 9 | 10 | 11 | def _is_weakreffable(thing): 12 | '''Return whether a weakref can be created to `thing`.''' 13 | try: 14 | weakref.ref(thing) 15 | except TypeError: 16 | return False 17 | else: 18 | return True 19 | 20 | 21 | class A: 22 | '''A class with a static method.''' 23 | @staticmethod 24 | def s(): 25 | pass 26 | 27 | 28 | @misc_tools.set_attributes(count=0) 29 | def counter(*args, **kwargs): 30 | '''Function that returns a higher number every time it's called.''' 31 | try: 32 | return counter.count 33 | finally: 34 | counter.count += 1 35 | -------------------------------------------------------------------------------- /test_python_toolbox/test_sleek_reffing/test_cute_sleek_value_dict/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.sleek_reffing.CuteSleekValueDict`.''' -------------------------------------------------------------------------------- /test_python_toolbox/test_string_cataloging/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | -------------------------------------------------------------------------------- /test_python_toolbox/test_string_cataloging/test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import string_cataloging 5 | 6 | 7 | def test(): 8 | x = string_cataloging.string_to_integer('ein') 9 | y = string_cataloging.string_to_integer('zwei') 10 | z = string_cataloging.string_to_integer('drei') 11 | 12 | assert string_cataloging.integer_to_string(x) == 'ein' 13 | assert string_cataloging.integer_to_string(y) == 'zwei' 14 | assert string_cataloging.integer_to_string(z) == 'drei' 15 | 16 | assert {string_cataloging.string_to_integer('zwei') for i in range(10)} \ 17 | == {y} -------------------------------------------------------------------------------- /test_python_toolbox/test_string_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.string_tools`.''' 5 | 6 | -------------------------------------------------------------------------------- /test_python_toolbox/test_string_tools/test_case_conversions.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.string_tools import case_conversions 5 | 6 | 7 | def test(): 8 | assert case_conversions.camel_case_to_space_case('HelloWorld') == \ 9 | 'Hello world' 10 | assert case_conversions.camel_case_to_lower_case('HelloWorld') == \ 11 | 'hello_world' 12 | assert case_conversions.lower_case_to_camel_case('hello_world') == \ 13 | 'HelloWorld' 14 | assert case_conversions.camel_case_to_upper_case('HelloWorld') == \ 15 | 'HELLO_WORLD' 16 | assert case_conversions.upper_case_to_camel_case('HELLO_WORLD') == \ 17 | 'HelloWorld' -------------------------------------------------------------------------------- /test_python_toolbox/test_string_tools/test_get_n_identical_edge_characters.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `string_tools.get_n_identical_edge_characters`.''' 5 | 6 | from python_toolbox.string_tools import get_n_identical_edge_characters 7 | 8 | 9 | def test(): 10 | '''Test the basics of `get_n_identical_edge_characters`.''' 11 | assert get_n_identical_edge_characters('qqqwee') == 3 12 | assert get_n_identical_edge_characters('qqqqwee') == 4 13 | assert get_n_identical_edge_characters('qqqqwee', head=False) == 2 14 | assert get_n_identical_edge_characters('1234') == 1 15 | assert get_n_identical_edge_characters('1234', character='4') == 0 16 | assert get_n_identical_edge_characters('1234', 17 | character='4', 18 | head=False) == 1 19 | assert get_n_identical_edge_characters('1234', 20 | character='&', 21 | head=False) == 0 22 | assert get_n_identical_edge_characters('pppp') == \ 23 | get_n_identical_edge_characters('pppp', head=False) == \ 24 | get_n_identical_edge_characters('pppp', character='p', 25 | head=False) == 4 -------------------------------------------------------------------------------- /test_python_toolbox/test_string_tools/test_rreplace.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox.string_tools import rreplace 5 | 6 | 7 | def test(): 8 | assert rreplace('meow meow meow', 'meow', 'woof') == \ 9 | rreplace('meow meow meow', 'meow', 'woof', 3) == \ 10 | rreplace('meow meow meow', 'meow', 'woof', 3000) == 'woof woof woof' 11 | 12 | assert rreplace('meow meow meow', 'meow', 'woof', 2) == 'meow woof woof' 13 | assert rreplace('meow meow meow', 'meow', 'woof', 1) == 'meow meow woof' 14 | assert rreplace('meow meow meow', 'meow', 'woof', 0) == 'meow meow meow' 15 | 16 | assert rreplace('aaa', 'aa', 'AA') == rreplace('aaa', 'aa', 'AA', 1) == \ 17 | 'aAA' -------------------------------------------------------------------------------- /test_python_toolbox/test_sys_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.sys_tools`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_sys_tools/test_temp_sys_path_adder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.sys_tools.TempSysPathAdder`.''' 5 | 6 | import sys 7 | 8 | from python_toolbox.sys_tools import TempSysPathAdder 9 | 10 | 11 | def test_single(): 12 | '''Test using `TempSysPathAdder` to add a single path.''' 13 | other_path = 'afdgfasgg38gjh3908ga' 14 | assert other_path not in sys.path 15 | with TempSysPathAdder(other_path): 16 | assert other_path in sys.path 17 | assert other_path not in sys.path 18 | 19 | 20 | def test_multiple(): 21 | '''Test using `TempSysPathAdder` to add multiple paths.''' 22 | other_paths = ['wf43f3_4f', 'argaer\\5g_'] 23 | for other_path in other_paths: 24 | assert other_path not in sys.path 25 | with TempSysPathAdder(other_paths): 26 | for other_path in other_paths: 27 | assert other_path in sys.path 28 | for other_path in other_paths: 29 | assert other_path not in sys.path 30 | -------------------------------------------------------------------------------- /test_python_toolbox/test_temp_file_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing package for `python_toolbox.temp_file_tools`.''' 5 | 6 | -------------------------------------------------------------------------------- /test_python_toolbox/test_temp_value_setting/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Testing module for `python_toolbox.temp_value_setting`.''' 5 | -------------------------------------------------------------------------------- /test_python_toolbox/test_temp_value_setting/test_recursion_limit_setter.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Tests for `python_toolbox.temp_value_setting.TempRecursionLimitSetter`.''' 5 | 6 | import sys 7 | 8 | from python_toolbox import cute_testing 9 | 10 | from python_toolbox.temp_value_setting import TempRecursionLimitSetter 11 | 12 | 13 | def test(): 14 | '''Test basic workings of `TempRecursionLimitSetter`.''' 15 | old_recursion_limit = sys.getrecursionlimit() 16 | assert sys.getrecursionlimit() == old_recursion_limit 17 | with TempRecursionLimitSetter(old_recursion_limit + 3): 18 | assert sys.getrecursionlimit() == old_recursion_limit + 3 19 | assert sys.getrecursionlimit() == old_recursion_limit 20 | 21 | 22 | def test_as_decorator(): 23 | '''Test `TempRecursionLimitSetter` when used as a decorator.''' 24 | old_recursion_limit = sys.getrecursionlimit() 25 | @TempRecursionLimitSetter(1234) 26 | def f(): 27 | assert sys.getrecursionlimit() == 1234 28 | assert sys.getrecursionlimit() == old_recursion_limit 29 | f() 30 | assert sys.getrecursionlimit() == old_recursion_limit 31 | 32 | cute_testing.assert_polite_wrapper(f) -------------------------------------------------------------------------------- /test_python_toolbox/test_tracing_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cool-RR/python_toolbox/cb580f5c2ca5ec6c826dc2589c4055dd5b740d21/test_python_toolbox/test_tracing_tools/__init__.py -------------------------------------------------------------------------------- /test_python_toolbox/test_tracing_tools/test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import tracing_tools 5 | 6 | 7 | def my_function(): 8 | ''' ''' 9 | 10 | def test(): 11 | ''' ''' 12 | 13 | with tracing_tools.TempFunctionCallCounter(my_function) as \ 14 | temp_function_call_counter: 15 | assert temp_function_call_counter.call_count == 0 16 | my_function() 17 | assert temp_function_call_counter.call_count == 1 18 | my_function() 19 | my_function() 20 | my_function() 21 | assert temp_function_call_counter.call_count == 4 22 | 23 | assert temp_function_call_counter.call_count == 4 24 | my_function() 25 | assert temp_function_call_counter.call_count == 4 26 | -------------------------------------------------------------------------------- /test_python_toolbox/test_zip_tools/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | -------------------------------------------------------------------------------- /test_python_toolbox/test_zip_tools/test_zip_folder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | import pathlib 5 | 6 | 7 | from python_toolbox import cute_testing 8 | from python_toolbox import temp_file_tools 9 | 10 | from python_toolbox import zip_tools 11 | 12 | 13 | def test(): 14 | with temp_file_tools.create_temp_folder() as temp_folder: 15 | assert isinstance(temp_folder, pathlib.Path) 16 | 17 | folder_to_zip = (temp_folder / 'folder_to_zip') 18 | folder_to_zip.mkdir() 19 | assert isinstance(folder_to_zip, pathlib.Path) 20 | 21 | (folder_to_zip / 'some_file.txt').open('w').write('hello there!') 22 | (folder_to_zip / 'some_other_file.txt').open('w').write( 23 | 'hello there again!') 24 | 25 | zip_file_path = temp_folder / 'archive.zip' 26 | assert isinstance(zip_file_path, pathlib.Path) 27 | zip_tools.zip_folder(folder_to_zip, temp_folder / 'archive.zip') 28 | 29 | assert zip_file_path.is_file() 30 | assert set( 31 | zip_tools.unzip_in_memory(zip_file_path.open('rb').read()) 32 | ) == { 33 | ('folder_to_zip/some_file.txt', b'hello there!'), 34 | ('folder_to_zip/some_other_file.txt', b'hello there again!'), 35 | } -------------------------------------------------------------------------------- /test_python_toolbox/test_zip_tools/test_zipping_in_memory.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | from python_toolbox import cute_testing 5 | 6 | from python_toolbox import zip_tools 7 | 8 | 9 | def test(): 10 | ''' ''' 11 | files = ( 12 | ('meow.txt', b"I'm a cat."), 13 | ('dog.txt', b"I'm a dog."), 14 | ('folder/binary.bin', bytes(bytearray(range(256)))) 15 | ) 16 | 17 | zip_archive = zip_tools.zip_in_memory(files) 18 | assert isinstance(zip_archive, bytes) 19 | assert set(zip_tools.unzip_in_memory(zip_archive)) == set(files) 20 | -------------------------------------------------------------------------------- /test_python_toolbox/third_party/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2009-2017 Ram Rachum. 2 | # This program is distributed under the MIT license. 3 | 4 | '''Third-party testing tools.''' 5 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | # tox (https://tox.readthedocs.io/) is a tool for running tests 2 | # Run tests in multiple virtualenvs. 3 | 4 | [tox] 5 | envlist = 6 | flake8 7 | pylint 8 | bandit 9 | py{36,37,38,py,py3} 10 | readme 11 | requirements 12 | clean 13 | 14 | [testenv] 15 | description = Unit tests 16 | deps = 17 | pytest 18 | commands = pytest 19 | 20 | [testenv:bandit] 21 | description = PyCQA security linter 22 | deps = bandit 23 | commands = bandit -r --ini tox.ini 24 | 25 | [testenv:clean] 26 | description = Clean up bytecode 27 | deps = pyclean 28 | commands = py3clean -v {toxinidir} 29 | 30 | [testenv:flake8] 31 | description = Static code analysis and code style 32 | deps = flake8 33 | commands = flake8 34 | 35 | [testenv:pylint] 36 | description = Check for errors and code smells 37 | deps = pylint 38 | commands = pylint python_toolbox setup 39 | 40 | [testenv:readme] 41 | description = Ensure README renders on PyPI 42 | deps = twine 43 | commands = 44 | {envpython} setup.py -q sdist bdist_wheel 45 | twine check dist/* 46 | 47 | [testenv:requirements] 48 | description = Update requirements.txt 49 | deps = pip-tools 50 | commands = pip-compile --output-file requirements.txt requirements.in 51 | changedir = {toxinidir} 52 | 53 | [bandit] 54 | exclude = .tox,build,dist,tests 55 | targets = . 56 | 57 | [flake8] 58 | exclude = .tox,build,dist,python_toolbox.egg-info 59 | 60 | [pytest] 61 | addopts = --strict 62 | --------------------------------------------------------------------------------