├── frameworks
├── __init__.py
├── pytest_sp
│ ├── __init__.py
│ ├── samples
│ │ ├── __init__.py
│ │ ├── sample_test.py
│ │ ├── test_sample.py
│ │ ├── test_exception.py
│ │ ├── test_tmpdir.py
│ │ ├── test_class.py
│ │ └── test_fixtures.py
│ └── readme.md
└── flask_snippets
│ ├── __init__.py
│ ├── blueprint_app
│ ├── __init__.py
│ ├── readme.md
│ ├── app.py
│ └── bp.py
│ ├── sample_app
│ ├── my_app
│ │ ├── __init__.py
│ │ ├── views
│ │ │ ├── __init__.py
│ │ │ └── hello.py
│ │ ├── app.py
│ │ └── templates
│ │ │ └── index.html
│ ├── run_app.py
│ └── readme.md
│ ├── before_after_request_sp
│ ├── __init__.py
│ ├── bp.py
│ └── app.py
│ ├── flask_bin
│ ├── readme.md
│ └── run_app.py
│ ├── hello_flask
│ └── app.py
│ ├── route_sp
│ └── catch_all_url.py
│ ├── errorhandler_app
│ ├── readme.md
│ └── app2.py
│ ├── flask_request
│ └── app.py
│ └── flask_json_encoder
│ └── app.py
├── sdks
└── qiniu
│ ├── __init__.py
│ ├── config
│ ├── sample_config.py
│ └── __init__.py
│ └── app.py
├── packages
├── _common_text
│ └── foo.txt
├── peewee_sp
│ ├── __init__.py
│ ├── transcation
│ │ ├── __init__.py
│ │ └── deco.py
│ ├── sqlite_db.py
│ ├── readme.md
│ ├── mysql_conn.py
│ ├── models.py
│ ├── mysql_db.py
│ ├── index_sp.py
│ ├── databases.py
│ ├── model_and_dict.py
│ ├── exceptions_sp.py
│ └── group_by_and_count.py
├── config_snippets
│ ├── __init__.py
│ ├── config
│ │ ├── online_config.py
│ │ ├── sample_config.py
│ │ └── __init__.py
│ └── read_config.py
├── jieba_sp
│ ├── recipe.txt
│ ├── recipe.py
│ └── jieba_sp.py
├── json_sp
│ ├── hello.json
│ ├── hello_u.json
│ └── json_encoder_sp.py
├── mako_sp
│ ├── mytmpl.txt
│ ├── readme.md
│ ├── render_file.py
│ ├── hello_mako.py
│ └── python_block.py
├── package_snippets
│ ├── test_all
│ │ ├── all_a
│ │ │ └── __init__.py
│ │ └── __init__.py
│ ├── foo
│ │ ├── foo_a.py
│ │ ├── bar
│ │ │ └── __init__.py
│ │ └── __init__.py
│ ├── __init__.py
│ ├── print_all.py
│ ├── print_all_2.py
│ └── print_package.py
├── _common_img
│ ├── in.jpg
│ ├── xxx.png
│ ├── google.jpg
│ ├── google.jpg.png
│ └── jump_jump.jpg
├── pendulum_sp
│ ├── readme.md
│ ├── now.py
│ ├── timezone.py
│ ├── with_datetime.py
│ └── parse_sp.py
├── heapq_sp
│ ├── readme.md
│ └── heapq_sp.py
├── pypinyin_sp
│ ├── readme.md
│ └── pypinyin_sp.py
├── tqdm_sp
│ ├── readmd.md
│ ├── tqdm_sp.py
│ ├── manual.py
│ └── description.py
├── pil_sp
│ ├── readme.md
│ ├── create_image.py
│ ├── hello_pil.py
│ ├── write_text.py
│ └── read_write_color.py
├── typing_snippets
│ ├── readme.md
│ ├── any_sp.py
│ ├── self_sp.py
│ ├── callable_sp.py
│ ├── int_it.py
│ ├── new_type_sp.py
│ ├── union_sp.py
│ ├── type_hint_sp.py
│ └── type_var_sp.py
├── cursor_size_snippets
│ └── hello_cursor.py
├── fire_sp
│ ├── hello_fire.py
│ ├── fire_multi_func.py
│ └── readme.md
├── input
│ └── hello_input.py
├── string_io_snippets
│ ├── readme.md
│ ├── with_string_io_as_f.py
│ └── hello_string_io.py
├── xml_parse_sp
│ ├── demo.xml
│ └── readme.md
├── requests_sp
│ ├── get.py
│ ├── hello_requests.py
│ ├── request_cookies.py
│ ├── post_json.py
│ ├── post_file.py
│ ├── request_header.py
│ ├── sessions.py
│ ├── request_through_proxy.py
│ ├── resp.py
│ ├── download_and_upload_img.py
│ ├── timeout_demo.py
│ └── save_img.py
├── bytes_io_sp
│ └── hello_bytes_io.py
├── coroutine_sp
│ ├── readme.md
│ ├── yield_sample.py
│ └── yield_task_scheduler.py
├── tempfile_sp
│ └── hello_tempfile.py
├── fake_snippets
│ ├── hello_fake.py
│ └── fake_demo.py
├── retrying_sp
│ └── readme.md
├── filter_snippets
│ ├── hello_filter.py
│ └── filter_performance.py
├── xerox_sp
│ └── xerox_sp.py
├── aiohttp
│ ├── readme.md
│ ├── aiohttp_client.py
│ └── simple_aiohttp_server.py
├── tuple_snippets
│ └── hello_tuple.py
├── pydantic_sp
│ ├── default_value.py
│ ├── inherit.py
│ ├── default_multable_value_pitfall.py
│ ├── fields.py
│ ├── field_alias_config.py
│ ├── model_init.py
│ ├── hello_pydantic.py
│ ├── readme.md
│ ├── custom_data_type.py
│ ├── setting_sp.py
│ ├── recursive_models.py
│ ├── demo_subclass_and_load_data.py
│ └── pep_484_types.py
├── dict_snippets
│ ├── dict_exception.py
│ ├── iter_dict.py
│ ├── dict_update.py
│ └── dict_compare.py
├── fileinput_sp
│ └── hello_fileinput.py
├── dataclasses_sp
│ ├── readme.md
│ ├── default_sp.py
│ ├── field_sp.py
│ ├── poly_field_sp.py
│ ├── load_sp.py
│ └── hello_dataclasses.py
├── base64_snippets
│ └── hello_base64.py
├── function_snippets
│ └── func_args.py
├── redis_sp
│ ├── hello_redis.py
│ ├── play_with_key.py
│ ├── redis_set.py
│ ├── batch_read_performance.py
│ ├── batch_insert_performance.py
│ └── redis_list.py
├── marshmallow_sp
│ ├── readme.md
│ ├── fields_sp.py
│ ├── nest_field.py
│ └── marshmallow_demo.py
├── mimetypes_sp
│ └── hello_mimetypes.py
├── misaka_snippets
│ └── misaka_demo.py
├── filesystem
│ ├── mk_dir.py
│ ├── files_in_path.py
│ ├── rm_dir.py
│ ├── join_path.py
│ ├── rm_file.py
│ ├── get_path.py
│ └── read_write_file.py
├── packaging_sp
│ └── packaging_sp.py
├── threading_snippets
│ ├── var_in_thread.py
│ ├── thread_and_queue.py
│ └── thread_and_log.py
├── statsd_snippets
│ └── hello_statsd.py
├── socket_sp
│ ├── socket_client_sp.py
│ ├── readme.md
│ └── socket_server_sp.py
├── gevent_snippets
│ ├── hello_gevent.py
│ └── patch_stdout.py
├── schematics_snippets
│ ├── schematics_types.py
│ ├── schematics_list_type.py
│ ├── schematics_dict_type.py
│ └── validation.py
├── log_snippets
│ ├── logger_sp.py
│ ├── readme.md
│ ├── log_demo.py
│ └── log_to_stdout.py
├── asyncio_sp
│ ├── readme.md
│ ├── hello_asyncio.py
│ ├── async_wget.py
│ └── asyncio_wget.py
├── mypy_sp
│ └── typed_dict.py
├── stdout_snippets
│ ├── print_to_stderr.py
│ └── get_shell_width_height.py
├── pyjwt_sp
│ ├── pyjwt_sp.py
│ └── readme.md
├── geetest_sp
│ ├── geetest_sp.py
│ └── readme.md
├── struct_sp
│ └── hello_struct.py
└── wtforms_sp
│ └── hello_wtforms.py
├── basic
├── context_manager_sp
│ ├── test_file
│ ├── readme.md
│ ├── context_manager_generator_approach.py
│ └── context_manager_class_approach.py
├── hello_world.py
├── int_sp
│ ├── int_sp.py
│ └── int_str.py
├── bytes_sp
│ ├── bytes_sp.py
│ ├── char_and_int.py
│ └── bytes_int.py
├── zip_demo.py
├── str_sp
│ ├── str_startswith_endswith.py
│ ├── str_strip.py
│ ├── upper_sp.py
│ ├── str_num.py
│ ├── str_unicode.py
│ ├── str_escape.py
│ └── str_demo.py
├── pass_sp
│ └── pass_sp.py
├── print.py
├── python_version.py
├── dict_sp
│ ├── traverse_dict.py
│ ├── dict_data.py
│ ├── filter_dict.py
│ └── update_dict.py
├── max_sp
│ └── max_sp.py
├── bool_sp.py
├── decorator_sp
│ ├── hello_deco.py
│ ├── register_deco.py
│ └── retry_with_decorator.py
├── i__dict__.py
├── range_sp
│ ├── datetime_range.py
│ ├── range.py
│ └── range_py2.py
├── exception_sp
│ ├── catch_sp.py
│ ├── exception_to_str.py
│ └── hello_exception.py
├── run_file.py
├── return_demo.py
├── yield_sp
│ ├── chunks_with_yield.py
│ └── yield_.py
├── any_demo.py
├── type_sp
│ └── type_sp.py
├── func_with_docstring.py
├── class_sp
│ ├── class_demo.py
│ ├── inner_meta_class.py
│ └── enum_class_demo.py
├── i__str____repr__.py
├── i__slots__.py
├── attribute_demo.py
├── iter_sp
│ └── iter_sp.py
├── doctest_sp
│ └── doctest_sp.py
├── copy_sp
│ └── copy_sp.py
└── inhert_sp
│ └── multi_inhert.py
├── utils
├── 001-read-ids-from-file
│ ├── ids2.txt
│ ├── ids.txt
│ ├── write_ids_to_file.py
│ └── app.py
├── 010-stack-overflow
│ └── app.py
├── 005-string-generator
│ └── app.py
├── 006-text-mosaic
│ └── app.py
├── 014-mean
│ └── app.py
├── 002-show-progress
│ └── app.py
├── 013-this-week
│ └── app.py
├── 003-upload-temp-file-with-string-io
│ └── app.py
├── 012-yes-or-no
│ └── app.py
├── 005-get-image-type
│ └── app.py
├── 009-download-then-upload-image-file
│ └── app.py
├── 015-base36
│ └── app.py
├── 004-get-title-from-url
│ └── app.py
└── 007-add-params-to-url
│ └── app.py
├── prjs
├── 014-count-text
│ ├── foo.txt
│ └── app.py
├── 002-covert-mindmap-csv-to-dict
│ └── test.csv
├── 008-marshmallow-schematics
│ ├── daos.py
│ └── app.py
├── 012-draw-text-on-img
│ ├── bg.png
│ └── app.py
├── 001-covert-img-to-rgb
│ ├── img
│ │ ├── in.jpg
│ │ ├── out_a.jpg
│ │ └── out_b.jpg
│ ├── icc
│ │ ├── JapanColor2001Coated.icc
│ │ └── sRGB Color Space Profile.ICM
│ ├── app.py
│ └── notes.md
├── 013-download-ow365pages
│ └── readme.md
├── 016-common-3500-hanzi-pinyin
│ └── app.py
├── 022-http-chunked-exp-with-flask
│ ├── readme.md
│ └── app.py
├── 006-make-1-million-requests-with-python-aiohttp
│ ├── s1_requests.py
│ ├── s1_aiohttp.py
│ ├── s2_requests.py
│ ├── s3_requests.py
│ └── s3_server.py
├── 024-how-long-the-course
│ ├── readme.md
│ └── app.py
├── 023-socket-http-client
│ ├── readme.md
│ ├── get.py
│ └── post.py
├── 010-add-bom-to-file
│ └── app.py
├── 017-url-encode
│ └── app.py
├── 026-json2csv
│ └── app.py
├── 019-read-video-duration
│ └── app.py
└── 020-random-yuque-page-id
│ └── app.py
├── playground.py
├── builtin_packages
├── csv_sp
│ ├── some.tsv
│ ├── some.csv
│ ├── writer_sp.py
│ ├── reader_for_tsv_sp.py
│ ├── reader_sp.py
│ └── append_writer.py
├── os_sp
│ ├── readme.md
│ ├── hello_os.py
│ ├── check_and_make_dir.py
│ └── read_write_os_environment_variables.py
├── hashlib_sp
│ └── readme.md
├── time_sp
│ ├── timestamp.py
│ └── execution_time_b.py
├── re_sp
│ ├── readme.md
│ ├── re_replace.py
│ ├── re_find_all.py
│ ├── hello_re.py
│ ├── group_search.py
│ ├── re_collect.py
│ └── re_tokens.py
├── datetime_sp
│ ├── isoformat_sp.py
│ ├── strftime_sp.py
│ ├── hello_datetime.py
│ ├── timedelta_sp.py
│ ├── datetime_operation.py
│ ├── datetime_util.py
│ ├── timezone_sp.py
│ ├── datetime_and_timestamp.py
│ └── week_day.py
├── urllib_parse_sp
│ ├── parse_qsl_sp.py
│ ├── parse_qs_sp.py
│ ├── readme.md
│ ├── urljoin_py2.py
│ └── urlencode_sp.py
├── math_sp
│ ├── ceil_and_floor.py
│ └── math_log.py
├── statistics_sp
│ └── mean_sp.py
├── argparse_sp
│ ├── note.md
│ └── argparse_demo.py
├── subprocess_sp
│ ├── get_std_out.py
│ ├── readme.md
│ ├── error_code_of_call.py
│ ├── hello_shell.py
│ ├── hello_shell_old.py
│ └── run_apple_script.py
├── inspect_sp
│ ├── member.py
│ ├── defines.py
│ ├── inspect_sp.py
│ ├── readme.md
│ ├── signature.py
│ └── source_code.py
├── enum_sp
│ ├── readme.md
│ ├── tuple_enum_sp.py
│ ├── hello_enum.py
│ └── enum_for_error_code.py
├── collections_snippets
│ ├── hello_counter.py
│ ├── hello_deque.py
│ ├── use_ordered_dict_remove_duplicate.py
│ ├── hello_namedtuple.py
│ └── hello_defaultdict.py
├── uuid_sp
│ ├── hello_uuid.py
│ └── part_or_uuid.py
├── base64_sp
│ ├── base64_decode.py
│ ├── urlsafe_encode_decode.py
│ └── base64_encode.py
└── sys_sp
│ └── sys_stdin.py
├── README.md
└── Pipfile
/frameworks/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/sdks/qiniu/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/frameworks/pytest_sp/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/packages/_common_text/foo.txt:
--------------------------------------------------------------------------------
1 | bar
--------------------------------------------------------------------------------
/packages/peewee_sp/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/packages/config_snippets/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/frameworks/pytest_sp/samples/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/packages/jieba_sp/recipe.txt:
--------------------------------------------------------------------------------
1 | 老北京
2 | 烫饭
3 |
--------------------------------------------------------------------------------
/packages/peewee_sp/transcation/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/packages/json_sp/hello.json:
--------------------------------------------------------------------------------
1 | {"hello": "world"}
--------------------------------------------------------------------------------
/packages/json_sp/hello_u.json:
--------------------------------------------------------------------------------
1 | {"hello": "世界"}
--------------------------------------------------------------------------------
/packages/mako_sp/mytmpl.txt:
--------------------------------------------------------------------------------
1 | hello, ${name}!
2 |
--------------------------------------------------------------------------------
/basic/context_manager_sp/test_file:
--------------------------------------------------------------------------------
1 | hello world
2 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/blueprint_app/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/utils/001-read-ids-from-file/ids2.txt:
--------------------------------------------------------------------------------
1 | 3
2 | 5
3 | 7
--------------------------------------------------------------------------------
/frameworks/flask_snippets/sample_app/my_app/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/before_after_request_sp/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/sample_app/my_app/views/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/prjs/014-count-text/foo.txt:
--------------------------------------------------------------------------------
1 | foo
2 | bar
3 | foo
4 |
5 | baz
6 | baz
7 | baz
8 |
--------------------------------------------------------------------------------
/packages/package_snippets/test_all/all_a/__init__.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 | all_a_a = 'aaa'
4 |
--------------------------------------------------------------------------------
/basic/hello_world.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 | if __name__ == '__main__':
4 | print("Hello, World!")
5 |
--------------------------------------------------------------------------------
/packages/mako_sp/readme.md:
--------------------------------------------------------------------------------
1 | [Mako 1.0.8 Documentation](http://docs.makotemplates.org/en/latest/index.html)
--------------------------------------------------------------------------------
/playground.py:
--------------------------------------------------------------------------------
1 | def main():
2 | print('Hello')
3 |
4 |
5 | if __name__ == '__main__':
6 | main()
7 |
--------------------------------------------------------------------------------
/packages/peewee_sp/sqlite_db.py:
--------------------------------------------------------------------------------
1 | from peewee import SqliteDatabase
2 |
3 | db = SqliteDatabase('data.db')
4 |
--------------------------------------------------------------------------------
/packages/package_snippets/foo/foo_a.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def print_foo_a():
5 | print('>>> foo_a')
6 |
--------------------------------------------------------------------------------
/packages/_common_img/in.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/packages/_common_img/in.jpg
--------------------------------------------------------------------------------
/packages/_common_img/xxx.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/packages/_common_img/xxx.png
--------------------------------------------------------------------------------
/frameworks/pytest_sp/readme.md:
--------------------------------------------------------------------------------
1 | backref: [pytest usage](https://www.notion.so/pytest-usage-9d63001e07c941d699d2449724c77d9c)
2 |
--------------------------------------------------------------------------------
/packages/peewee_sp/readme.md:
--------------------------------------------------------------------------------
1 | backref: [peewee usage](https://www.notion.so/peewee-usage-07d691919fd7494cbd578d59277b5a45)
2 |
--------------------------------------------------------------------------------
/packages/pendulum_sp/readme.md:
--------------------------------------------------------------------------------
1 | [Documentation | Pendulum - Python datetimes made easy](https://pendulum.eustace.io/docs/)
2 |
--------------------------------------------------------------------------------
/prjs/002-covert-mindmap-csv-to-dict/test.csv:
--------------------------------------------------------------------------------
1 | 如何种西瓜,,
2 | ,准备种植,
3 | ,,决定是播种还是移植
4 | ,种植西瓜,
5 | ,,制造土墩
6 | ,,播种种子
7 | ,收获西瓜,
8 |
--------------------------------------------------------------------------------
/prjs/008-marshmallow-schematics/daos.py:
--------------------------------------------------------------------------------
1 | class FooDAO(object):
2 | def __init__(self, bar):
3 | self.bar = bar
4 |
--------------------------------------------------------------------------------
/packages/_common_img/google.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/packages/_common_img/google.jpg
--------------------------------------------------------------------------------
/packages/config_snippets/config/online_config.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | class Config(object):
5 |
6 | ENV = 'online'
7 |
--------------------------------------------------------------------------------
/packages/config_snippets/config/sample_config.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | class Config(object):
5 |
6 | ENV = 'sample'
7 |
--------------------------------------------------------------------------------
/packages/heapq_sp/readme.md:
--------------------------------------------------------------------------------
1 | # readme
2 |
3 | [heapq --- 堆队列算法 — Python 3.7.4 文档](https://docs.python.org/zh-cn/3/library/heapq.html)
--------------------------------------------------------------------------------
/prjs/012-draw-text-on-img/bg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/prjs/012-draw-text-on-img/bg.png
--------------------------------------------------------------------------------
/builtin_packages/csv_sp/some.tsv:
--------------------------------------------------------------------------------
1 | n_followers to_id
2 | 2932945 111111111
3 | 691671 10008888
4 | 548603 10444444
5 | 369772 104388888
6 |
--------------------------------------------------------------------------------
/packages/_common_img/google.jpg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/packages/_common_img/google.jpg.png
--------------------------------------------------------------------------------
/packages/_common_img/jump_jump.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/packages/_common_img/jump_jump.jpg
--------------------------------------------------------------------------------
/frameworks/pytest_sp/samples/sample_test.py:
--------------------------------------------------------------------------------
1 | def inc(x):
2 | return x + 1
3 |
4 |
5 | def test_answer():
6 | assert inc(4) == 5
7 |
--------------------------------------------------------------------------------
/packages/pypinyin_sp/readme.md:
--------------------------------------------------------------------------------
1 | # pypinyin
2 |
3 | [mozillazg/python-pinyin: 汉字转拼音(pypinyin)](https://github.com/mozillazg/python-pinyin)
4 |
--------------------------------------------------------------------------------
/packages/tqdm_sp/readmd.md:
--------------------------------------------------------------------------------
1 | # tqdm
2 |
3 | [tqdm/tqdm: A fast, extensible progress bar for Python and CLI](https://github.com/tqdm/tqdm)
4 |
--------------------------------------------------------------------------------
/prjs/001-covert-img-to-rgb/img/in.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/prjs/001-covert-img-to-rgb/img/in.jpg
--------------------------------------------------------------------------------
/basic/int_sp/int_sp.py:
--------------------------------------------------------------------------------
1 |
2 | def main():
3 | print(11 // 10)
4 | print(11 / 10)
5 |
6 |
7 | if __name__ == '__main__':
8 | main()
9 |
--------------------------------------------------------------------------------
/prjs/001-covert-img-to-rgb/img/out_a.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/prjs/001-covert-img-to-rgb/img/out_a.jpg
--------------------------------------------------------------------------------
/prjs/001-covert-img-to-rgb/img/out_b.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/prjs/001-covert-img-to-rgb/img/out_b.jpg
--------------------------------------------------------------------------------
/prjs/013-download-ow365pages/readme.md:
--------------------------------------------------------------------------------
1 | # 下载 ow365 页面中的图片
2 |
3 | 在最后才发现它的跳转过程中有一步是能得到源 pdf 文件的地址的,开始的时候被我忽略了,以为没有这一步的存在。
4 |
5 | 那么这个脚本也就不需要了。
6 |
--------------------------------------------------------------------------------
/prjs/016-common-3500-hanzi-pinyin/app.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/prjs/016-common-3500-hanzi-pinyin/app.py
--------------------------------------------------------------------------------
/packages/pil_sp/readme.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | ref:
4 |
5 | - [Pillow — Pillow (PIL Fork) 4.4.0.dev0 documentation](http://pillow.readthedocs.io/en/latest/)
6 |
--------------------------------------------------------------------------------
/basic/bytes_sp/bytes_sp.py:
--------------------------------------------------------------------------------
1 |
2 | def main():
3 | print(b'Hello')
4 | print(type(b'Hello'))
5 |
6 |
7 | if __name__ == '__main__':
8 | main()
9 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/blueprint_app/readme.md:
--------------------------------------------------------------------------------
1 | [Modular Applications with Blueprints — Flask Documentation (0.12)](http://flask.pocoo.org/docs/0.12/blueprints/)
--------------------------------------------------------------------------------
/basic/zip_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 | if __name__ == '__main__':
4 | l1 = [1, 2, 3, 4]
5 | l2 = [11, 12, 13]
6 | l3 = zip(l1, l2)
7 | print(l3)
8 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/flask_bin/readme.md:
--------------------------------------------------------------------------------
1 | [httpbin/core.py at master · kennethreitz/httpbin](https://github.com/kennethreitz/httpbin/blob/master/httpbin/core.py)
--------------------------------------------------------------------------------
/packages/package_snippets/foo/bar/__init__.py:
--------------------------------------------------------------------------------
1 | print('__name__ of foo/bar/__init__.py: {}'.format(__name__))
2 |
3 |
4 | def print_bar():
5 | print('>>> bar')
6 |
--------------------------------------------------------------------------------
/packages/package_snippets/test_all/__init__.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from .all_a import all_a_a
3 |
4 | __all__ = ["a", "b"]
5 |
6 | a = 'aa'
7 | b = 'bb'
8 | c = 'cc'
9 |
--------------------------------------------------------------------------------
/packages/typing_snippets/readme.md:
--------------------------------------------------------------------------------
1 | - [26.1. typing — Support for type hints — Python 3.6.4 documentation](https://docs.python.org/3/library/typing.html#module-typing)
2 |
--------------------------------------------------------------------------------
/packages/package_snippets/foo/__init__.py:
--------------------------------------------------------------------------------
1 | import bar
2 | print('__name__ of foo/__init__.py: {}'.format(__name__))
3 |
4 |
5 | def print_foo():
6 | print('>>> foo')
7 |
--------------------------------------------------------------------------------
/prjs/001-covert-img-to-rgb/icc/JapanColor2001Coated.icc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/prjs/001-covert-img-to-rgb/icc/JapanColor2001Coated.icc
--------------------------------------------------------------------------------
/packages/cursor_size_snippets/hello_cursor.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | print('Hello')
6 |
7 |
8 | if __name__ == '__main__':
9 | main()
10 |
--------------------------------------------------------------------------------
/prjs/022-http-chunked-exp-with-flask/readme.md:
--------------------------------------------------------------------------------
1 | # http chuncked exp with flask
2 |
3 | [[doing]HTTP Content-Length、Range 以及 chunked 实验 · 语雀](https://www.yuque.com/clip/dev-wiki/inif76)
--------------------------------------------------------------------------------
/basic/str_sp/str_startswith_endswith.py:
--------------------------------------------------------------------------------
1 | def main():
2 | print('Hello'.startswith('H'))
3 | print('Hello'.endswith('o'))
4 |
5 |
6 | if __name__ == '__main__':
7 | main()
8 |
--------------------------------------------------------------------------------
/builtin_packages/os_sp/readme.md:
--------------------------------------------------------------------------------
1 | - [How to set environment variables in Python - Stack Overflow](https://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python)
2 |
--------------------------------------------------------------------------------
/packages/pendulum_sp/now.py:
--------------------------------------------------------------------------------
1 | import pendulum
2 |
3 |
4 | def main():
5 | now = pendulum.now()
6 | print(now)
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/prjs/001-covert-img-to-rgb/icc/sRGB Color Space Profile.ICM:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/binderclip/code-snippets-python/HEAD/prjs/001-covert-img-to-rgb/icc/sRGB Color Space Profile.ICM
--------------------------------------------------------------------------------
/packages/fire_sp/hello_fire.py:
--------------------------------------------------------------------------------
1 | import fire
2 |
3 |
4 | def say_hello(name):
5 | print(f'hello {name}')
6 |
7 |
8 | if __name__ == '__main__':
9 | fire.Fire(say_hello)
10 |
--------------------------------------------------------------------------------
/basic/context_manager_sp/readme.md:
--------------------------------------------------------------------------------
1 | ## refs
2 |
3 | - [Structuring Your Project — The Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/writing/structure/#context-managers)
4 |
--------------------------------------------------------------------------------
/basic/pass_sp/pass_sp.py:
--------------------------------------------------------------------------------
1 | def pass_test():
2 | pass
3 | print('hello')
4 |
5 |
6 | def main():
7 | pass_test()
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/builtin_packages/hashlib_sp/readme.md:
--------------------------------------------------------------------------------
1 | [arrays - Python: How to hash a string into 8 digits? - Stack Overflow](https://stackoverflow.com/questions/16008670/python-how-to-hash-a-string-into-8-digits)
--------------------------------------------------------------------------------
/packages/input/hello_input.py:
--------------------------------------------------------------------------------
1 | def main():
2 | text = input('input your text: ')
3 | print('=== got it ===')
4 | print(text)
5 |
6 |
7 | if __name__ == '__main__':
8 | main()
9 |
--------------------------------------------------------------------------------
/basic/print.py:
--------------------------------------------------------------------------------
1 | def main():
2 | print('Hello')
3 | print('a', end='')
4 | print('b', end='')
5 | print('c', end='')
6 |
7 |
8 | if __name__ == '__main__':
9 | main()
10 |
--------------------------------------------------------------------------------
/prjs/006-make-1-million-requests-with-python-aiohttp/s1_requests.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def hello():
5 | return requests.get("http://httpbin.org/get")
6 |
7 |
8 | print(hello().text)
9 |
--------------------------------------------------------------------------------
/utils/001-read-ids-from-file/ids.txt:
--------------------------------------------------------------------------------
1 | 123
2 | 456
3 | 789
4 | 123
5 | 456
6 |
7 |
8 | 789
9 | 123
10 | 456
11 | 789
12 | 123
13 | 456
14 | 789
15 | 123
16 | 456
17 | 789
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/basic/python_version.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import sys
3 |
4 |
5 | def main():
6 | print('Python Version: {}'.format(sys.version))
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/frameworks/pytest_sp/samples/test_sample.py:
--------------------------------------------------------------------------------
1 | import pytest
2 |
3 |
4 | def inc(x):
5 | return x + 1
6 |
7 |
8 | def test_answer():
9 | assert inc(4) == 5
10 | # assert inc(3) == 5
11 |
--------------------------------------------------------------------------------
/prjs/024-how-long-the-course/readme.md:
--------------------------------------------------------------------------------
1 | # readme
2 |
3 | 统计网易云客网一整节课的时间。
4 |
5 | 网页上每节课里面有 '12:34' 这样的时长,把这些识别出来,做个和就行。
6 |
7 | - [x] 从标准输入读取字符串
8 | - [x] 正则识别时间串
9 | - [x] 时间串转分钟
10 | - [x] 所有相加
--------------------------------------------------------------------------------
/basic/str_sp/str_strip.py:
--------------------------------------------------------------------------------
1 | def main():
2 | print(repr(' Hello\n\r'))
3 | print(repr(' Hello\n\r'.strip()))
4 | print('x大西瓜'.strip('大瓜西'))
5 |
6 |
7 | if __name__ == '__main__':
8 | main()
9 |
--------------------------------------------------------------------------------
/builtin_packages/time_sp/timestamp.py:
--------------------------------------------------------------------------------
1 | import time
2 |
3 |
4 | def main():
5 | print(time.time())
6 | print(int(time.time() * 1000))
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/packages/pil_sp/create_image.py:
--------------------------------------------------------------------------------
1 | from PIL import Image
2 |
3 |
4 | def main():
5 | im = Image.new("RGB", (200, 200), "#FFF")
6 | im.show()
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/flask_bin/run_app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from app import app
3 |
4 |
5 | def main():
6 | app.run(port=8080, debug=True)
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/packages/tqdm_sp/tqdm_sp.py:
--------------------------------------------------------------------------------
1 | import time
2 | from tqdm import tqdm
3 |
4 |
5 | def main():
6 | for _ in tqdm(range(70)):
7 | time.sleep(0.05)
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/utils/010-stack-overflow/app.py:
--------------------------------------------------------------------------------
1 | def foo():
2 | bar()
3 |
4 | def bar():
5 | foo()
6 |
7 | def main():
8 | print('foo...')
9 | foo()
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/builtin_packages/re_sp/readme.md:
--------------------------------------------------------------------------------
1 | ## refs
2 |
3 | - [6.2. re — Regular expression operations — Python 3.7.0 documentation](https://docs.python.org/3/library/re.html)
4 | - [Online regex tester and debugger](https://regex101.com/)
5 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/sample_app/my_app/app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from flask import Flask
3 | from flask_mako import MakoTemplates
4 | app = Flask(__name__)
5 | mako = MakoTemplates(app)
6 |
7 | import my_app.views.hello
8 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/sample_app/run_app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from my_app.app import app
3 |
4 |
5 | def main():
6 | app.run(port=8080, debug=True)
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/packages/config_snippets/read_config.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from config import CSConfig
3 |
4 |
5 | def main():
6 | print("ENV is: {}".format(CSConfig.ENV))
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/packages/string_io_snippets/readme.md:
--------------------------------------------------------------------------------
1 | see also:
2 |
3 | - Binary I/O (BytesIO)
4 |
5 | ## refs
6 |
7 | - [16.2. io — Core tools for working with streams — Python 3.6.4 documentation](https://docs.python.org/3/library/io.html)
8 |
--------------------------------------------------------------------------------
/packages/xml_parse_sp/demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | elements
4 | more elements
5 |
6 |
7 | element as well
8 |
9 |
--------------------------------------------------------------------------------
/basic/bytes_sp/char_and_int.py:
--------------------------------------------------------------------------------
1 |
2 | def main():
3 | print('\x41')
4 | print(chr(65))
5 | print(hex(65))
6 | print(ord('A'))
7 | print(ord('我'))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/basic/dict_sp/traverse_dict.py:
--------------------------------------------------------------------------------
1 |
2 | def main():
3 | d = {
4 | 'foo': 'fooo',
5 | 'bar': 'baar',
6 | }
7 | for k in d:
8 | d[]
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/basic/max_sp/max_sp.py:
--------------------------------------------------------------------------------
1 |
2 | def main():
3 | print(max([1, 3, 2]))
4 | print(max(['a', 'c', 'b']))
5 | print(max(['2222-03-20', '', '2018-03-20 18:44:43']))
6 |
7 |
8 | if __name__ == '__main__':
9 | main()
10 |
--------------------------------------------------------------------------------
/packages/requests_sp/get.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def main():
5 | r = requests.get('http://httpbin.org/get', params={'foo': 'bar'})
6 | print(r.json())
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/sdks/qiniu/config/sample_config.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | class Config(object):
5 |
6 | ACCESS_KEY = 'xxx'
7 | SECRET_KEY = 'xxx'
8 | BUCKET = 'xxx'
9 | HOST = 'http://xxx'
10 | PREFIX ='uuimgs/'
11 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/isoformat_sp.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 |
4 | def main():
5 | dt = datetime.datetime.utcnow()
6 | print(dt)
7 | print(dt.isoformat())
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/builtin_packages/re_sp/re_replace.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 |
4 | def main():
5 | s = 'abc x123 def'
6 | s2 = re.sub('x', 'y', s)
7 | print(s)
8 | print(s2)
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/blueprint_app/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 | from bp import simple_page
3 |
4 | app = Flask(__name__)
5 | app.register_blueprint(simple_page)
6 |
7 |
8 | if __name__ == '__main__':
9 | app.run(debug=True)
--------------------------------------------------------------------------------
/builtin_packages/csv_sp/some.csv:
--------------------------------------------------------------------------------
1 | a,b,c
2 | 100,10000,","
3 | 101,10201,","
4 | 102,10404,","
5 | 103,10609,","
6 | 104,10816,","
7 | 110,12100,","
8 | 111,12321,","
9 | 112,12544,","
10 | 113,12769,","
11 | 114,12996,","
12 |
--------------------------------------------------------------------------------
/packages/package_snippets/__init__.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import foo
3 | print('__name__ of __init__.py: {}'.format(__name__))
4 |
5 |
6 | def main():
7 | print('Hello')
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/basic/bool_sp.py:
--------------------------------------------------------------------------------
1 |
2 | def main():
3 | print(bool(-1))
4 | print(bool(0))
5 | print(bool(0.1))
6 | print(bool(1))
7 | print(bool(1.1))
8 | print(bool(2))
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/basic/decorator_sp/hello_deco.py:
--------------------------------------------------------------------------------
1 | def identity(f):
2 | return f
3 |
4 |
5 | @identity
6 | def foo():
7 | return 'bar'
8 |
9 |
10 | def main():
11 | print(foo())
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/packages/bytes_io_sp/hello_bytes_io.py:
--------------------------------------------------------------------------------
1 | import io
2 |
3 |
4 | def main():
5 | f = io.BytesIO(b"some initial binary data: \x00\x01")
6 | print(f.read())
7 | f.close()
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/packages/fire_sp/fire_multi_func.py:
--------------------------------------------------------------------------------
1 | import fire
2 |
3 |
4 | def add(a: int, b: int):
5 | print(a + b)
6 |
7 |
8 | def multiply(a: int, b: int):
9 | print(a * b)
10 |
11 |
12 | if __name__ == '__main__':
13 | fire.Fire()
14 |
--------------------------------------------------------------------------------
/basic/str_sp/upper_sp.py:
--------------------------------------------------------------------------------
1 | def main():
2 | s = 'hello world'
3 | print(s.upper())
4 | print(s.capitalize())
5 | print(s.capitalize().swapcase())
6 | print(s.title())
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/builtin_packages/urllib_parse_sp/parse_qsl_sp.py:
--------------------------------------------------------------------------------
1 | from urllib.parse import parse_qsl
2 |
3 |
4 | def main():
5 | qs = 'lang=en&tag=python&tag=python3'
6 | print(parse_qsl(qs))
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/packages/coroutine_sp/readme.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | - [Python “黑魔法” 之 Generator Coroutines - Python - 伯乐在线](http://python.jobbole.com/85117/)
4 | - [18.5.3. Tasks and coroutines — Python 3.6.4 documentation](https://docs.python.org/3/library/asyncio-task.html)
5 |
--------------------------------------------------------------------------------
/packages/tempfile_sp/hello_tempfile.py:
--------------------------------------------------------------------------------
1 | import os
2 | import tempfile
3 |
4 |
5 | def main():
6 | tempdir = tempfile.gettempdir()
7 | print(os.path.join(tempdir, 'xxxx.txt'))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/sample_app/my_app/views/hello.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from my_app.app import app
3 | from flask_mako import render_template
4 |
5 |
6 | @app.route("/")
7 | def index():
8 | return render_template('index.html', name='Big WM')
9 |
--------------------------------------------------------------------------------
/packages/fake_snippets/hello_fake.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from faker import Factory
3 |
4 |
5 | def main():
6 | fake = Factory.create()
7 | print("Hello, {}!".format(fake.name()))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/strftime_sp.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 |
4 | def main():
5 | now = datetime.datetime.now()
6 | print(now)
7 | print(now.strftime('%Y-%m-%d %H:%M:%S.%f'))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/packages/retrying_sp/readme.md:
--------------------------------------------------------------------------------
1 | ## refs
2 |
3 | - [rholder/retrying: Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything.](https://github.com/rholder/retrying)
4 |
--------------------------------------------------------------------------------
/basic/i__dict__.py:
--------------------------------------------------------------------------------
1 |
2 | class X(object):
3 | pass
4 |
5 |
6 | def main():
7 | print(X.__dict__)
8 | x = X()
9 | print(x.__dict__)
10 | x.xx = 'xxx'
11 | print(x.__dict__)
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/sample_app/my_app/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Hello ${name}
6 |
7 |
8 | Hello ${name}!
9 |
10 |
11 |
--------------------------------------------------------------------------------
/prjs/023-socket-http-client/readme.md:
--------------------------------------------------------------------------------
1 | # socket http client
2 |
3 | - [python socket GET - Stack Overflow](https://stackoverflow.com/questions/34192093/python-socket-get)
4 | - https://www.geeks3d.com/hacklab/20190110/python-3-simple-http-request-with-the-socket-module/
--------------------------------------------------------------------------------
/utils/005-string-generator/app.py:
--------------------------------------------------------------------------------
1 | def main():
2 | start = 1
3 | end = 100
4 | step = 1
5 |
6 | for i in range(start, end + step, step):
7 | print("{:02d}".format(i)) # 01
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/basic/dict_sp/dict_data.py:
--------------------------------------------------------------------------------
1 | def main():
2 | d = {'foo': 'bar', 'baz': 'qux'}
3 | print(d.items())
4 | print(d.values())
5 | for value in d.values():
6 | print(value)
7 | print(d.keys())
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/builtin_packages/math_sp/ceil_and_floor.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 |
4 | def main():
5 | print(math.ceil(1.1))
6 | print(math.floor(1.1))
7 | print(math.ceil(-1.1))
8 | print(math.floor(-1.1))
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/packages/filter_snippets/hello_filter.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | a = ['a', None, 0, '', 'b']
6 | print(a)
7 | print(type(filter(None, a)))
8 | print(filter(None, a))
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/packages/xerox_sp/xerox_sp.py:
--------------------------------------------------------------------------------
1 | import random
2 |
3 | import xerox
4 |
5 |
6 | def main():
7 | i = random.randint(0, 100)
8 | xerox.copy(str(i))
9 | print(i)
10 | print(xerox.paste())
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/packages/mako_sp/render_file.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from mako.template import Template
3 |
4 |
5 | def main():
6 | mytemplate = Template(filename='mytmpl.txt')
7 | print(mytemplate.render(name='Jack'))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/frameworks/pytest_sp/samples/test_exception.py:
--------------------------------------------------------------------------------
1 | import pytest
2 |
3 |
4 | def f():
5 | raise SystemExit(1)
6 |
7 |
8 | def test_exception():
9 | with pytest.raises(SystemExit):
10 | # 如果不抛异常会报错 Failed: DID NOT RAISE
11 | f()
12 |
--------------------------------------------------------------------------------
/packages/pil_sp/hello_pil.py:
--------------------------------------------------------------------------------
1 | from PIL import Image
2 |
3 |
4 | def main():
5 | im = Image.open("../_common_img/in.jpg")
6 | print(im.format, im.size, im.mode)
7 | print(type(im.mode))
8 | im.show()
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/packages/xml_parse_sp/readme.md:
--------------------------------------------------------------------------------
1 | - [XML parsing — The Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/scenarios/xml/)
2 | - [martinblech/xmltodict: Python module that makes working with XML feel like you are working with JSON](https://github.com/martinblech/xmltodict)
--------------------------------------------------------------------------------
/builtin_packages/os_sp/hello_os.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 | import os
4 | import platform
5 |
6 |
7 | def main():
8 | print(os.name)
9 | print(platform.system())
10 | print(platform.release())
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/builtin_packages/statistics_sp/mean_sp.py:
--------------------------------------------------------------------------------
1 | from statistics import mean
2 |
3 |
4 | def main():
5 | print(mean([3, 3, 4]))
6 | # print(mean([])) # statistics.StatisticsError: mean requires at least one data point
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
--------------------------------------------------------------------------------
/packages/aiohttp/readme.md:
--------------------------------------------------------------------------------
1 |
2 | ## refs
3 |
4 |
5 | - [Welcome to AIOHTTP — aiohttp 3.0.2- documentation](https://aiohttp.readthedocs.io/en/stable/)
6 | - [Making 1 million requests with python-aiohttp](https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html)
7 |
--------------------------------------------------------------------------------
/packages/peewee_sp/mysql_conn.py:
--------------------------------------------------------------------------------
1 | import pymysql
2 |
3 |
4 | MYSQL_USER = "root"
5 | MYSQL_PASSWORD = ""
6 | MYSQL_HOST = "localhost"
7 | MYSQL_PORT = 3306
8 | MYSQL_DB_NAME = "test_db2"
9 |
10 | conn = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD)
11 |
--------------------------------------------------------------------------------
/builtin_packages/argparse_sp/note.md:
--------------------------------------------------------------------------------
1 | - [16.4. argparse — Parser for command-line options, arguments and sub-commands — Python 3.6.4 documentation](https://docs.python.org/3/library/argparse.html)
2 | - [Argparse Tutorial — Python 2.7.14 documentation](https://docs.python.org/2/howto/argparse.html)
--------------------------------------------------------------------------------
/builtin_packages/urllib_parse_sp/parse_qs_sp.py:
--------------------------------------------------------------------------------
1 | from urllib.parse import parse_qs
2 |
3 |
4 | def main():
5 | qs = 'lang=en&tag=python&tag=python3'
6 | print(parse_qs(qs))
7 | qs = ''
8 | print(parse_qs(qs))
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/frameworks/pytest_sp/samples/test_tmpdir.py:
--------------------------------------------------------------------------------
1 | # Builtin fixtures/function arguments
2 | def test_needsfiles(tmpdir):
3 | print(tmpdir) # tmpdir = local('/private/var/folders/78/cczt63ls3t1dh7kxy8d7m2z40000gn/T/pytest-of-clip/pytest-2/test_needsfiles0')
4 | # assert 0
5 | assert 1
6 |
--------------------------------------------------------------------------------
/packages/requests_sp/hello_requests.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import requests
3 |
4 | def main():
5 | r = requests.get('http://httpbin.org/get')
6 | print(r.status_code)
7 | print(r.headers)
8 | print(r.text)
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/packages/tuple_snippets/hello_tuple.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def fun1():
5 | return 1, 2
6 |
7 |
8 | def fun2():
9 | return (3, 4)
10 |
11 |
12 | def main():
13 | print(fun1())
14 | print(fun2())
15 |
16 |
17 | if __name__ == '__main__':
18 | main()
19 |
--------------------------------------------------------------------------------
/basic/range_sp/datetime_range.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 |
4 | def main():
5 | dts = range(datetime.datetime(2018, 3, 28), datetime.datetime(2018, 3, 29), datetime.timedelta(hours=1))
6 | print(dts)
7 | print('Hello')
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/builtin_packages/subprocess_sp/get_std_out.py:
--------------------------------------------------------------------------------
1 | import subprocess
2 |
3 |
4 | def main():
5 | ret = subprocess.run(["echo", "hello"], stdout=subprocess.PIPE)
6 | print(ret)
7 | print(ret.stdout.strip().decode('utf-8'))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/hello_flask/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 | app = Flask(__name__)
3 |
4 |
5 | @app.route("/")
6 | def hello():
7 | return "Hello World!"
8 |
9 |
10 | def main():
11 | app.run(debug=True)
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/default_value.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseModel
2 |
3 |
4 | class Foo(BaseModel):
5 | bar: str = None
6 | baz: int = 10
7 |
8 |
9 | def main():
10 | foo = Foo.parse_obj({})
11 | print(foo)
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/basic/exception_sp/catch_sp.py:
--------------------------------------------------------------------------------
1 |
2 |
3 | def int_it(s):
4 | try:
5 | return int(s)
6 | except (ValueError, TypeError) as e:
7 | print(e)
8 |
9 |
10 | def main():
11 | int_it(None)
12 | int_it('hello')
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/packages/dict_snippets/dict_exception.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | d = {"a": "aa"}
6 | try:
7 | d['b'] # KeyError: 'b'
8 | except Exception as e:
9 | print(e)
10 | raise e
11 |
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/packages/dict_snippets/iter_dict.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | d = {
6 | 'foo': 'bar'
7 | }
8 | print(type(d.items()))
9 | for k, v in d.items():
10 | print('<{}: {}>'.format(k, v))
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/packages/fake_snippets/fake_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from faker import Factory
3 |
4 |
5 | def main():
6 | fake = Factory.create()
7 | print("name: {}".format(fake.name()))
8 | print("text: {}".format(fake.text()))
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/packages/requests_sp/request_cookies.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def main():
5 | cookies = {
6 | "t": "ttt",
7 | }
8 | r = requests.get('https://httpbin.org/cookies', cookies=cookies)
9 | print(r.json())
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/packages/tqdm_sp/manual.py:
--------------------------------------------------------------------------------
1 | import time
2 |
3 | from tqdm import tqdm
4 |
5 |
6 | def main():
7 | pbar = tqdm(total=100)
8 | for i in range(10):
9 | pbar.update(10)
10 | time.sleep(0.5)
11 | pbar.close()
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/builtin_packages/os_sp/check_and_make_dir.py:
--------------------------------------------------------------------------------
1 | import os
2 |
3 |
4 | def make_dir_if_not_exists(dir):
5 | if not os.path.exists(dir):
6 | os.makedirs(dir)
7 |
8 |
9 | def main():
10 | make_dir_if_not_exists('hello/world')
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/builtin_packages/inspect_sp/member.py:
--------------------------------------------------------------------------------
1 | import inspect
2 |
3 |
4 | class Foo:
5 | '''Just a foo class'''
6 | a = 'A' # the a
7 |
8 |
9 | def main():
10 | print(inspect.getmembers(Foo))
11 | print(inspect.getmembers(Foo()))
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/packages/pypinyin_sp/pypinyin_sp.py:
--------------------------------------------------------------------------------
1 | from pypinyin import lazy_pinyin
2 |
3 |
4 | def main():
5 | print(lazy_pinyin('中国'))
6 | print(''.join(lazy_pinyin('大西瓜')))
7 | print(lazy_pinyin('T恤'))
8 | print(lazy_pinyin('big西瓜373a'))
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/basic/run_file.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # coding: utf-8
3 |
4 |
5 | def main():
6 | print('Hello')
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
12 | #!/path/to/interpreter
13 | # chmod +x my_python_script.py
14 | # https://www.pythoncentral.io/execute-python-script-file-shell/
15 |
--------------------------------------------------------------------------------
/builtin_packages/subprocess_sp/readme.md:
--------------------------------------------------------------------------------
1 | [shell - Calling an external command in Python - Stack Overflow](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python)
2 | [17.5. subprocess — Subprocess management — Python 3.6.5rc1 documentation](https://docs.python.org/3/library/subprocess.html)
3 |
--------------------------------------------------------------------------------
/frameworks/pytest_sp/samples/test_class.py:
--------------------------------------------------------------------------------
1 | # Group multiple tests in a class
2 | class TestClass(object):
3 | def test_one(self):
4 | x = "this"
5 | assert 'h' in x
6 |
7 | def test_two(self):
8 | x = "hello"
9 | # assert hasattr(x, 'check')
10 | assert 'h' in x
11 |
--------------------------------------------------------------------------------
/packages/tqdm_sp/description.py:
--------------------------------------------------------------------------------
1 | import time
2 |
3 | from tqdm import tqdm
4 |
5 |
6 | def main():
7 | pbar = tqdm(["a", "b", "c", "d"])
8 | for char in pbar:
9 | pbar.set_description("Processing %s" % char)
10 | time.sleep(0.5)
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/builtin_packages/enum_sp/readme.md:
--------------------------------------------------------------------------------
1 | ## refs
2 |
3 | - [8.13. enum — Support for enumerations — Python 3.6.4 documentation](https://docs.python.org/3/library/enum.html)
4 | - [使用枚举类 - 廖雪峰的官方网站](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143191235886950998592cd3e426e91687cdae696e64b000)
5 |
--------------------------------------------------------------------------------
/packages/fileinput_sp/hello_fileinput.py:
--------------------------------------------------------------------------------
1 | import fileinput
2 |
3 |
4 | def main():
5 | lines = []
6 | for line in fileinput.input():
7 | lines.append(line)
8 | print('OK')
9 | print('=== end ===')
10 | print(''.join(lines))
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/builtin_packages/inspect_sp/defines.py:
--------------------------------------------------------------------------------
1 | # class Foo comment
2 | class Foo:
3 | '''Just a foo class'''
4 | # a comment
5 | a = 'A' # the a
6 |
7 | # bar comment
8 | def bar(self):
9 | '''print hello bar'''
10 | print('hello bar')
11 |
12 | # abc
13 | foo = Foo() # a foo object
14 |
--------------------------------------------------------------------------------
/packages/dataclasses_sp/readme.md:
--------------------------------------------------------------------------------
1 | - [PEP 557 -- Data Classes | Python.org](https://www.python.org/dev/peps/pep-0557/)
2 | - [ericvsmith/dataclasses](https://github.com/ericvsmith/dataclasses)
3 | - [A brief tour of Python 3.7 data classes – Hacker Noon](https://hackernoon.com/a-brief-tour-of-python-3-7-data-classes-22ee5e046517)
4 |
--------------------------------------------------------------------------------
/utils/006-text-mosaic/app.py:
--------------------------------------------------------------------------------
1 | def text_mosaic(text):
2 | if len(text) < 2:
3 | return text
4 | else:
5 | return text[0] * len(text)
6 |
7 |
8 | def main():
9 | print(text_mosaic('8d175a93'))
10 | print(text_mosaic('08e235d012aa'))
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/builtin_packages/re_sp/re_find_all.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 |
4 | def main():
5 | s = "a 12:22,b 32:00xc,34:0sdf"
6 | print(re.findall(r'\d\d:\d\d', s))
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
12 | # https://stackoverflow.com/questions/4697882/how-can-i-find-all-matches-to-a-regular-expression-in-python
--------------------------------------------------------------------------------
/packages/base64_snippets/hello_base64.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from base64 import urlsafe_b64encode, urlsafe_b64decode
3 |
4 |
5 | def main():
6 | print(urlsafe_b64encode('abc/=%?&'))
7 | print(urlsafe_b64encode('100'))
8 | print(urlsafe_b64decode('MTAw'))
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
--------------------------------------------------------------------------------
/packages/function_snippets/func_args.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def print_kwargs(**kwargs):
5 | print("=== print_kwargs ===")
6 | print(kwargs)
7 |
8 |
9 | def main():
10 | print_kwargs(a="aa", b="bb")
11 | print_kwargs(**{"c": "cc", "d": "dd"})
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/packages/redis_sp/hello_redis.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import redis
3 |
4 |
5 | def main():
6 | r = redis.StrictRedis(host='localhost', port=6379, db=0)
7 | r.set('Hello', 'World')
8 | r.delete("Hello")
9 | print('Hello, {}!'.format(r.get('Hello')))
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/packages/redis_sp/play_with_key.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import redis
3 |
4 |
5 | def main():
6 | r = redis.StrictRedis(host='localhost', port=6379, db=0)
7 | r.set('Hello', 'World')
8 | r.delete("Hello")
9 | print('Hello, {}!'.format(r.get('Hello')))
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/basic/dict_sp/filter_dict.py:
--------------------------------------------------------------------------------
1 |
2 | def filter_dict_none(d):
3 | return {k: v for k, v in d.items() if v is not None}
4 |
5 |
6 | def main():
7 | d = {
8 | 'foo': 'bar',
9 | 'baz': None,
10 | }
11 | d = filter_dict_none(d)
12 | print(d)
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/builtin_packages/urllib_parse_sp/readme.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | ## refs
4 |
5 | - [Add params to given URL in Python - Stack Overflow](https://stackoverflow.com/questions/2506379/add-params-to-given-url-in-python)
6 | - [21.8. urllib.parse — Parse URLs into components — Python 3.6.4 documentation](https://docs.python.org/3/library/urllib.parse.html)
7 |
--------------------------------------------------------------------------------
/packages/requests_sp/post_json.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def main():
5 | url = 'https://httpbin.org/post'
6 | data = {
7 | 'foo': 'bar',
8 | }
9 | print(requests.post(url, json=data).json())
10 | print(requests.post(url, data=data).json())
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/packages/typing_snippets/any_sp.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from typing import Any
3 |
4 |
5 | def foo(item: Any) -> Any:
6 | return item
7 |
8 |
9 | def bar(s: str) -> None:
10 | print(s)
11 |
12 |
13 | def main():
14 | bar(foo('hello'))
15 | bar(foo(12))
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/utils/014-mean/app.py:
--------------------------------------------------------------------------------
1 | def mean(l):
2 | return sum(l) / (len(l) or 1)
3 |
4 |
5 | def main():
6 | print(mean([3, 3, 4]))
7 | print(mean([]))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
13 |
14 | # https://stackoverflow.com/questions/7716331/calculating-arithmetic-mean-one-type-of-average-in-python
15 |
--------------------------------------------------------------------------------
/basic/context_manager_sp/context_manager_generator_approach.py:
--------------------------------------------------------------------------------
1 | from contextlib import contextmanager
2 |
3 |
4 | @contextmanager
5 | def custom_open(filename):
6 | f = open(filename)
7 | try:
8 | yield f
9 | finally:
10 | f.close()
11 |
12 |
13 | with custom_open('file') as f:
14 | contents = f.read()
15 |
--------------------------------------------------------------------------------
/basic/return_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def f_a():
5 | return
6 |
7 |
8 | def f_b():
9 | return 1, 2
10 |
11 |
12 | def main():
13 | # foo, bar = f_a() # TypeError: 'NoneType' object is not iterable
14 | foo, bar = f_b()
15 | print(foo, bar)
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/packages/marshmallow_sp/readme.md:
--------------------------------------------------------------------------------
1 |
2 | ## refs
3 |
4 | - [marshmallow: simplified object serialization — marshmallow 3.0.0b7 documentation](https://marshmallow.readthedocs.io/en/latest/)
5 | - [Bachmann1234/marshmallow-polyfield: An extension to marshmallow to allow for polymorphic fields](https://github.com/Bachmann1234/marshmallow-polyfield)
6 |
--------------------------------------------------------------------------------
/builtin_packages/collections_snippets/hello_counter.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from collections import Counter
3 |
4 |
5 | def main():
6 | cnt = Counter()
7 | for n in [1, 2, 3, 2, 1, 4, 7, 6, 2]:
8 | cnt[n] += 1
9 | print(cnt.items())
10 | print(cnt.most_common(10))
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/packages/mimetypes_sp/hello_mimetypes.py:
--------------------------------------------------------------------------------
1 | import mimetypes
2 |
3 |
4 | def main():
5 | print(mimetypes.guess_type('xxx.png'))
6 | print(mimetypes.guess_type('xxx.txt'))
7 | print(mimetypes.guess_type('png'))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
13 |
14 | # https://docs.python.org/3/library/mimetypes.html
15 |
--------------------------------------------------------------------------------
/packages/misaka_snippets/misaka_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import misaka as m
3 | from misaka import Markdown, HtmlRenderer
4 |
5 |
6 | def main():
7 | rndr = HtmlRenderer()
8 | md = Markdown(rndr)
9 | print md('some text')
10 |
11 | print m.html('some other text')
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/inherit.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseModel
2 |
3 |
4 | class A(BaseModel):
5 | a: str = 'aaa'
6 |
7 |
8 | class B(BaseModel):
9 | b: str = 'bbb'
10 |
11 |
12 | class AB(A, B):
13 | ab: str = 'aaabbb'
14 |
15 |
16 | def main():
17 | print(AB())
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/packages/requests_sp/post_file.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def main():
5 |
6 | url = 'https://httpbin.org/post'
7 | with open('../_common_img/xxx.png', 'rb') as f:
8 | r = requests.post(url, files={'image': ('xxx.png', f, 'image/png')})
9 | print(r.json())
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/builtin_packages/csv_sp/writer_sp.py:
--------------------------------------------------------------------------------
1 | import csv
2 |
3 |
4 | def rows(n):
5 | for i in range(n):
6 | yield (i, i * i, ',')
7 |
8 |
9 | def main():
10 | with open('some.csv', 'w', newline='') as f:
11 | writer = csv.writer(f)
12 | writer.writerows(rows(10))
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/builtin_packages/urllib_parse_sp/urljoin_py2.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from urlparse import urljoin
3 |
4 |
5 | def test_join_url(url1, url2):
6 | print(urljoin(url1, url2))
7 |
8 |
9 | def main():
10 | test_join_url(
11 | 'http://example.com',
12 | 'foo',
13 | )
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/packages/filesystem/mk_dir.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 |
5 | def main():
6 | dir_path = 'temp_dir'
7 | print('dir <{}> exists: {}'.format(dir_path, os.path.exists(dir_path)))
8 | try:
9 | os.mkdir(dir_path)
10 | except OSError as e:
11 | print(e)
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/utils/001-read-ids-from-file/write_ids_to_file.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def write_ids_to_file(ids_file, ids):
5 | with open(ids_file, 'w') as f:
6 | f.write('\n'.join(map(str, ids)))
7 |
8 |
9 | def main():
10 | ids = [3, 5, 7]
11 | write_ids_to_file('ids2.txt', ids)
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/builtin_packages/os_sp/read_write_os_environment_variables.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 |
5 | def main():
6 | print(os.environ["HOME"])
7 | print(os.environ['PATH'])
8 | print(os.environ.get('NOT_EXIST'))
9 |
10 | os.environ['FOO'] = 'BAR'
11 | print(os.environ['FOO'])
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/route_sp/catch_all_url.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 |
3 | app = Flask(__name__)
4 |
5 |
6 | @app.route('/', defaults={'path': ''})
7 | @app.route('/')
8 | def catch_all(path):
9 | return 'You want path: %s' % path
10 |
11 |
12 | if __name__ == '__main__':
13 | app.run()
14 |
15 | # http://flask.pocoo.org/snippets/57/
16 |
--------------------------------------------------------------------------------
/packages/redis_sp/redis_set.py:
--------------------------------------------------------------------------------
1 | import redis
2 |
3 | r = redis.StrictRedis(host='localhost', port=6379, db=0)
4 |
5 |
6 | def main():
7 | key = 'foo_set'
8 | r.sadd(key, 'a')
9 | r.sadd(key, 1)
10 | print(r.sismember(key, '1'))
11 | print(r.scard(key))
12 | print(r.smembers(key))
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/packages/string_io_snippets/with_string_io_as_f.py:
--------------------------------------------------------------------------------
1 | import io
2 |
3 |
4 | def main():
5 | text = 'hello world!'
6 | with io.StringIO() as f:
7 | f.write(text)
8 | f.seek(0)
9 | f.name = 'hello.txt'
10 | print(f)
11 | print(f.name)
12 | print(f.read())
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/basic/yield_sp/chunks_with_yield.py:
--------------------------------------------------------------------------------
1 | def chunks(l, n):
2 | """ Yield successive n-sized chunks from l. """
3 | for i in range(0, len(l), n):
4 | yield l[i:i+n]
5 |
6 |
7 | def main():
8 | l = range(0, 10)
9 | print(list(l))
10 | for cl in chunks(l, 4):
11 | print(list(cl))
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/builtin_packages/subprocess_sp/error_code_of_call.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from subprocess import call
3 |
4 |
5 | def main():
6 | retcode1 = call(["ls", "./"])
7 | print('retcode1: {}'.format(retcode1))
8 | retcode2 = call(["ls", "./not/existed/path"])
9 | print('retcode2: {}'.format(retcode2))
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/packages/fire_sp/readme.md:
--------------------------------------------------------------------------------
1 | # Python fire snippets
2 |
3 | ## refs
4 |
5 | - [google/python-fire: Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.](https://github.com/google/python-fire)
6 | - [python-fire/guide.md at master · google/python-fire](https://github.com/google/python-fire/blob/master/docs/guide.md)
7 |
--------------------------------------------------------------------------------
/basic/any_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def c1():
5 | print('c1')
6 | return True
7 |
8 |
9 | def c2():
10 | print('c2')
11 | return False
12 |
13 |
14 | def main():
15 | print(any([c1(), c2()]))
16 | if c1() or c2():
17 | print(True)
18 | else:
19 | print(False)
20 |
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/builtin_packages/subprocess_sp/hello_shell.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from subprocess import run
3 |
4 |
5 | def main():
6 | run(["pwd"])
7 | run(["echo", "hello", "world"])
8 | run(["sleep", "1"])
9 | print(1)
10 | run(["sleep", "1"])
11 | print(2)
12 | run(["sleep", "1"])
13 | print(3)
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/packages/package_snippets/print_all.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import test_all
3 |
4 |
5 | def main():
6 | print('test_all.a: {}'.format(test_all.a))
7 | print('test_all.b: {}'.format(test_all.b))
8 | print('test_all.c: {}'.format(test_all.c))
9 | print('test_all.all_a_a: {}'.format(test_all.all_a_a))
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/packages/packaging_sp/packaging_sp.py:
--------------------------------------------------------------------------------
1 | from packaging import version
2 |
3 |
4 | def main():
5 | print(version.parse('1.1.2') < (version.parse('1.1.11')))
6 | print(version.parse('') < (version.parse('1.1.11')))
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
12 | # https://stackoverflow.com/questions/11887762/how-do-i-compare-version-numbers-in-python
13 |
--------------------------------------------------------------------------------
/prjs/023-socket-http-client/get.py:
--------------------------------------------------------------------------------
1 | import socket
2 |
3 |
4 | def main():
5 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6 |
7 | s.connect(("httpbin.org", 80))
8 | s.sendall(b"GET /get HTTP/1.1\r\nHost: httpbin.org\r\n\r\n")
9 | data = s.recv(4096)
10 | print(data)
11 | s.close()
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/basic/dict_sp/update_dict.py:
--------------------------------------------------------------------------------
1 |
2 | def main():
3 | d1 = {
4 | 'foo': 'a',
5 | 'bar': 'b',
6 | }
7 | print(d1)
8 | d1['foo'] = 'aa'
9 | print(d1)
10 | d2 = {
11 | 'bar': 'bbb',
12 | }
13 | d1.update(d2)
14 | print(d1)
15 | print(d1.pop('foo'))
16 | print(d1)
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/hello_datetime.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import datetime
3 |
4 |
5 | def main():
6 | time_delta = datetime.datetime(2017, 7, 17) - datetime.datetime(2017, 7, 20)
7 | time_delta2 = datetime.timedelta(days=1)
8 | print(time_delta)
9 | print(abs(time_delta))
10 | print(time_delta2)
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/builtin_packages/inspect_sp/inspect_sp.py:
--------------------------------------------------------------------------------
1 | import inspect
2 |
3 |
4 | def f(a, b=1, *pos, **named):
5 | print('... in f')
6 | print(inspect.getcallargs(f, 2, 2, 2, 2))
7 |
8 |
9 | def main():
10 | print(inspect.getcallargs(f, 1, 2, 3, 4))
11 | f('x')
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 | # getmembers
17 | # signature
18 | #
19 |
20 |
--------------------------------------------------------------------------------
/builtin_packages/subprocess_sp/hello_shell_old.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from subprocess import call
3 |
4 |
5 | def main():
6 | call(["pwd"])
7 | call(["echo", "hello", "world"])
8 | call(["sleep", "1"])
9 | print(1)
10 | call(["sleep", "1"])
11 | print(2)
12 | call(["sleep", "1"])
13 | print(3)
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/blueprint_app/bp.py:
--------------------------------------------------------------------------------
1 | from flask import Blueprint, abort
2 | from jinja2 import TemplateNotFound
3 |
4 | simple_page = Blueprint('simple_page', __name__)
5 |
6 | @simple_page.route('/', defaults={'name': 'foo'})
7 | @simple_page.route('/')
8 | def show(name):
9 | try:
10 | return name
11 | except TemplateNotFound:
12 | abort(404)
13 |
--------------------------------------------------------------------------------
/packages/pil_sp/write_text.py:
--------------------------------------------------------------------------------
1 | from PIL import Image, ImageFont, ImageDraw
2 |
3 |
4 | def main():
5 | im = Image.new("RGB", (600, 600), "#000")
6 | draw = ImageDraw.Draw(im)
7 | font = ImageFont.truetype("SourceHanSansCN-Normal.otf", 33)
8 | draw.text((10, 25), "大西瓜", font=font, fill="#F78")
9 | im.show()
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/packages/requests_sp/request_header.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def main():
5 | headers = {
6 | "User-Agent": "Mozilla/5.0 (iPhone; CPU iOS 11.4 like Mac OS X)",
7 | "Cookie": "foo=bar; baz=qux;"
8 | }
9 | r = requests.get('https://httpbin.org/headers', headers=headers)
10 | print(r.json())
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/builtin_packages/enum_sp/tuple_enum_sp.py:
--------------------------------------------------------------------------------
1 | from enum import Enum
2 |
3 |
4 | class ErrorCode(tuple, Enum):
5 | common = (1000, '请求错误')
6 | user_not_exist = (1001, '用户不存在')
7 |
8 |
9 | def main():
10 | e1 = ErrorCode.common
11 | print(e1)
12 | print(e1.name)
13 | print(e1.value)
14 | print(e1[0], e1[1])
15 |
16 |
17 | if __name__ == '__main__':
18 | main()
19 |
--------------------------------------------------------------------------------
/builtin_packages/inspect_sp/readme.md:
--------------------------------------------------------------------------------
1 | # Python inspect
2 |
3 |
4 | ## refs
5 |
6 | - [30.13. inspect — Inspect live objects — Python 3.7.0 documentation](https://docs.python.org/3/library/inspect.html)
7 | - [关于inspect研究 - 小明明](http://www.dongwm.com/old/archives/guanyuinspectyanjiu/)
8 | - [inspect.getcomments Python Example](https://www.programcreek.com/python/example/7133/inspect.getcomments)
9 |
--------------------------------------------------------------------------------
/prjs/010-add-bom-to-file/app.py:
--------------------------------------------------------------------------------
1 | import fire
2 |
3 |
4 | def main(file):
5 | with open(file, 'r') as f:
6 | data = f.read()
7 |
8 | with open(f'out-{file}', 'wb') as f:
9 | f.write(b'\xef\xbb\xbf')
10 | f.write(data.encode('utf-8'))
11 |
12 |
13 | if __name__ == '__main__':
14 | fire.Fire(main)
15 |
16 | # https://segmentfault.com/a/1190000004321605
17 |
--------------------------------------------------------------------------------
/builtin_packages/uuid_sp/hello_uuid.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import uuid
3 |
4 |
5 | def main():
6 | print('uuid: {}'.format(str(uuid.uuid4())))
7 | print('uuid: {}'.format(str(uuid.uuid4()).split('-')[0]))
8 | print(str(uuid.uuid4()).replace('-', '')[:24])
9 | print(len(str(uuid.uuid4()).replace('-', '')[:24]))
10 |
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
--------------------------------------------------------------------------------
/packages/threading_snippets/var_in_thread.py:
--------------------------------------------------------------------------------
1 |
2 |
3 | # http://python.jobbole.com/86150/
4 | # https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832845200f6513494f0c64bd882f25818a0281e80000
5 | # https://stackoverflow.com/questions/1408171/thread-local-storage-in-python
6 | # http://wiki.jikexueyuan.com/project/explore-python/Process-Thread-Coroutine/threadlocal.html
7 |
--------------------------------------------------------------------------------
/prjs/024-how-long-the-course/app.py:
--------------------------------------------------------------------------------
1 | import sys
2 | import re
3 |
4 |
5 | def main():
6 | text = sys.stdin.read()
7 | time_strs = re.findall(r'\d\d:\d\d', text)
8 | seconds = 0
9 | for time_str in time_strs:
10 | ms, ss = time_str.split(":")
11 | seconds += int(ms) * 60 + int(ss)
12 | print(seconds // 60)
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/packages/marshmallow_sp/fields_sp.py:
--------------------------------------------------------------------------------
1 | import datetime
2 | from marshmallow import Schema, fields
3 |
4 |
5 | class UserSchema(Schema):
6 | id = fields.Int(missing=None)
7 | name = fields.Str()
8 | created_at = fields.DateTime(missing=lambda: str(datetime.datetime.now()))
9 |
10 |
11 | def main():
12 | print(UserSchema().load({}))
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/prjs/017-url-encode/app.py:
--------------------------------------------------------------------------------
1 | import urllib.parse
2 |
3 |
4 | def main():
5 | txt = """
6 | 西
7 | 瓜
8 | """
9 | strings = txt.strip().splitlines()
10 | for s in strings:
11 | print(f"{s} {urllib.parse.quote_plus(s)}")
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
17 | # https://stackoverflow.com/questions/5607551/how-to-urlencode-a-querystring-in-python/9345102#9345102
18 |
--------------------------------------------------------------------------------
/builtin_packages/inspect_sp/signature.py:
--------------------------------------------------------------------------------
1 | import inspect
2 |
3 |
4 | def foo(a, b: int) -> int:
5 | return int(a) + b
6 |
7 |
8 | def main():
9 | foo(1, 10)
10 | sig = inspect.signature(foo)
11 | for name, parameter in sig.parameters.items():
12 | print(name, parameter, parameter.annotation)
13 | print(sig.return_annotation)
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/builtin_packages/math_sp/math_log.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 |
4 | def main():
5 | print(math.log10(4416))
6 | print(math.log10(1000))
7 | print(math.log10(900))
8 | print(math.log10(100))
9 | print(math.log10(40))
10 | print(math.log10(6))
11 |
12 | print(math.log2(100))
13 | print(math.log2(40))
14 | print(math.log2(6))
15 |
16 |
17 | if __name__ == '__main__':
18 | main()
19 |
--------------------------------------------------------------------------------
/packages/package_snippets/print_all_2.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from test_all import *
3 |
4 |
5 | def main():
6 | print('a: {}'.format(a))
7 | print('b: {}'.format(b))
8 | # print('c: {}'.format(c)) # NameError: global name 'c' is not defined
9 | # print('all_a_a: {}'.format(all_a_a)) # NameError: global name 'all_a_a' is not defined
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/default_multable_value_pitfall.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseModel
2 | from typing import List
3 |
4 |
5 | class Foo(BaseModel):
6 | bar: List[str] = []
7 |
8 |
9 | def main():
10 | foo1 = Foo()
11 | foo1.bar.append('a')
12 | print(foo1)
13 |
14 | foo2 = Foo()
15 | foo2.bar.append('b')
16 | print(foo2)
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/builtin_packages/uuid_sp/part_or_uuid.py:
--------------------------------------------------------------------------------
1 | import uuid
2 |
3 |
4 | def get_a_part_or_uuid():
5 | return str(uuid.uuid4())[:16]
6 |
7 |
8 | def main():
9 | pus = []
10 | for i in range(10000000):
11 | pus.append(get_a_part_or_uuid())
12 | print(len(pus)) # 10000000
13 | s_pus = set(pus)
14 | print(len(s_pus)) # 10000000
15 |
16 |
17 | if __name__ == '__main__':
18 | main()
19 |
--------------------------------------------------------------------------------
/packages/statsd_snippets/hello_statsd.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import statsd
3 |
4 |
5 | def main():
6 | c = statsd.StatsClient('localhost', 8125)
7 | c.incr('foo') # Increment the 'foo' counter.
8 | c.timing('stats.timed', 320) # Record a 320ms 'stats.timed'.
9 | c.timing('stats.timed,service_name={},status_code={}'.format('bar', 200), 120)
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/builtin_packages/base64_sp/base64_decode.py:
--------------------------------------------------------------------------------
1 | import base64
2 |
3 |
4 | def print_b64decode(bs):
5 | print(base64.b64decode(bs))
6 |
7 |
8 | def main():
9 | print_b64decode(b'////')
10 | print_b64decode(b'YQ==')
11 | print_b64decode(b'YWE=')
12 | print_b64decode(b'YWFh')
13 | print_b64decode(b'YWFhYQ==')
14 | print_b64decode(b'BkzPUg==')
15 |
16 |
17 | if __name__ == '__main__':
18 | main()
19 |
--------------------------------------------------------------------------------
/packages/typing_snippets/self_sp.py:
--------------------------------------------------------------------------------
1 | from typing import TypeVar, Type
2 |
3 | T = TypeVar('T', bound='C')
4 | class C:
5 | def __init__(self, a: str, b: int):
6 | self.a = a
7 | self.b = b
8 |
9 | @classmethod
10 | def factory(cls: Type[T]) -> T:
11 | return C('hello', 123)
12 |
13 |
14 | c = C.factory()
15 | print(c.a, c.b)
16 |
17 |
18 | # https://github.com/python/mypy/issues/1212
19 |
--------------------------------------------------------------------------------
/basic/range_sp/range.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | print(range(0))
6 | print(range(1))
7 | print(range(0, 1))
8 | print(range(0, 1, 1))
9 | print(range(10, 15, 4))
10 | print(range(-2))
11 | print(range(0, -2, -1))
12 |
13 | print(list(range(10)))
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
19 | # https://docs.python.org/3/library/functions.html#func-range
20 |
--------------------------------------------------------------------------------
/packages/typing_snippets/callable_sp.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from typing import Callable
3 |
4 |
5 | def feeder(get_next_item: Callable[[], str]) -> None:
6 | pass
7 |
8 |
9 | def async_query(on_success: Callable[[int], None],
10 | on_error: Callable[[int, Exception], None]) -> None:
11 | pass
12 |
13 |
14 | def main():
15 | print('Hello')
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/builtin_packages/csv_sp/reader_for_tsv_sp.py:
--------------------------------------------------------------------------------
1 | import csv
2 |
3 |
4 | def main():
5 | with open('some.tsv', 'r', newline='') as f:
6 | reader = csv.reader(f, delimiter='\t')
7 | for row in reader:
8 | print(row)
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
14 |
15 | # https://stackoverflow.com/questions/13992971/reading-and-parsing-a-tsv-file-then-manipulating-it-for-saving-as-csv-efficie
16 |
--------------------------------------------------------------------------------
/packages/dict_snippets/dict_update.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | d = {}
6 | print(d)
7 | d['a'] = 'aa'
8 | d.update(b='bb')
9 | print(d)
10 | d.pop('a')
11 | print(d)
12 | print(d.popitem())
13 | print(d)
14 | d = {
15 | 'c': 'cc',
16 | 'd': 'dd',
17 | }
18 | print(d)
19 | d.clear()
20 | print(d)
21 |
22 |
23 | if __name__ == '__main__':
24 | main()
25 |
--------------------------------------------------------------------------------
/packages/heapq_sp/heapq_sp.py:
--------------------------------------------------------------------------------
1 | from heapq import *
2 |
3 |
4 | def main():
5 | h = [] # 毕竟 heap 是可以用 list 实现的,所以这个 heapq 直接用 list 操作也没毛病
6 | # 不确定有没有初始化的过程,以及中间强行破坏后会怎么样
7 | heappush(h, (5, 'write code'))
8 | heappush(h, (7, 'release product'))
9 | heappush(h, (1, 'write spec'))
10 | heappush(h, (3, 'create tests'))
11 | print(heappop(h))
12 | print(h)
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/packages/socket_sp/socket_client_sp.py:
--------------------------------------------------------------------------------
1 | import socket
2 |
3 |
4 | def main():
5 | HOST = '127.0.0.1' # 服务器的主机名或者 IP 地址
6 | PORT = 65432 # 服务器使用的端口
7 |
8 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 | s.connect((HOST, PORT))
10 | s.sendall(b'Hello, world')
11 | data = s.recv(1024)
12 |
13 | print('Received', repr(data))
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/builtin_packages/csv_sp/reader_sp.py:
--------------------------------------------------------------------------------
1 | import csv
2 |
3 |
4 | def main():
5 | with open('some.csv', 'r', newline='') as f:
6 | reader = csv.reader(f)
7 | next(reader) # pass the first line
8 | for row in reader:
9 | print(row)
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
15 | # https://stackoverflow.com/questions/11349333/when-processing-csv-data-how-do-i-ignore-the-first-line-of-data
16 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/sample_app/readme.md:
--------------------------------------------------------------------------------
1 |
2 | ```
3 | export PYTHONPATH=/frameworks/flask_snippets/sample_app:$PYTHONPATH
4 | ```
5 |
6 |
7 | ref:
8 |
9 | - [flask/examples/flaskr at master · pallets/flask](https://github.com/pallets/flask/tree/master/examples/flaskr)
10 | - [flask/examples/patterns/largerapp at master · pallets/flask](https://github.com/pallets/flask/tree/master/examples/patterns/largerapp)
11 |
--------------------------------------------------------------------------------
/packages/filesystem/files_in_path.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 |
5 | def main():
6 | dir = './'
7 | print(os.listdir(dir))
8 | file_paths = []
9 | for file_name in os.listdir(dir):
10 | file_paths.append(os.path.join(dir, file_name))
11 | print(file_paths)
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
17 | # https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
18 |
--------------------------------------------------------------------------------
/packages/typing_snippets/int_it.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | print(int('1'))
6 | try:
7 | print(int('x1'))
8 | except ValueError as e:
9 | print(e)
10 | try:
11 | print(int('xx'))
12 | except ValueError as e:
13 | print(e)
14 | try:
15 | print(int(None))
16 | except TypeError as e:
17 | print(e)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/timedelta_sp.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import datetime
3 | import time
4 |
5 |
6 | def main():
7 | now1 = datetime.datetime.now()
8 | time.sleep(1.1)
9 | now2 = datetime.datetime.now()
10 | print(now2 - now1)
11 | past = now2 - datetime.timedelta(days=1, seconds=1, microseconds=1, milliseconds=1, minutes=1, hours=1, weeks=1)
12 | print(past)
13 |
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/packages/dict_snippets/dict_compare.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | d1 = {"a": 1}
6 | d1_1 = {"a": 1}
7 | d2 = {"l": [1, 2, 3]}
8 | d2_1 = {"l": [1, 2, 3]}
9 | d3 = {"d": {"i": 1}}
10 | d3_1 = {"d": {"i": 1}}
11 | d3_2 = {"d": {"i": 1}, "i": 2}
12 | print(d1 == d1_1)
13 | print(d2 == d2_1)
14 | print(d3 == d3_1)
15 | print(d3 == d3_2)
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/packages/gevent_snippets/hello_gevent.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from gevent import monkey; monkey.patch_all()
3 | import gevent
4 | import urllib2
5 |
6 |
7 | def main():
8 | urls = ['www.google.com', 'www.example.com', 'www.python.org']
9 | jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls]
10 | gevent.joinall(jobs, timeout=2)
11 | print([job.value for job in jobs])
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
--------------------------------------------------------------------------------
/packages/redis_sp/batch_read_performance.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import redis
3 | from profilehooks import profile
4 |
5 |
6 | @profile
7 | def read_from_redis(r, n, d):
8 | for i in xrange(n):
9 | d[str(n - 1 - i)] = r.get(str(i))
10 |
11 |
12 | def main():
13 | d = {}
14 | r = redis.StrictRedis(host='localhost', port=6379, db=0)
15 |
16 | read_from_redis(r, 10000, d)
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/packages/requests_sp/sessions.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def main():
5 | s = requests.Session()
6 | s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
7 | r = s.get("http://httpbin.org/cookies")
8 | print(r.text)
9 | r = requests.get("http://httpbin.org/cookies")
10 | print(r.text)
11 |
12 |
13 | if __name__ == '__main__':
14 | main()
15 |
16 | # http://docs.python-requests.org/en/latest/user/advanced/
17 |
--------------------------------------------------------------------------------
/packages/schematics_snippets/schematics_types.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import datetime
3 | from schematics.models import Model
4 | from schematics.types import StringType, IntType, DateTimeType, ModelType
5 |
6 |
7 | class MyModel(Model):
8 |
9 | s = StringType()
10 | i = IntType()
11 |
12 |
13 | def main():
14 | mm = MyModel({"s": "ss", "i": "1"})
15 | print(mm.to_primitive())
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/before_after_request_sp/bp.py:
--------------------------------------------------------------------------------
1 | from flask import Blueprint
2 |
3 | bp = Blueprint('bp', __name__)
4 |
5 | @bp.before_request
6 | def before_request():
7 | print('>>> bp before_request')
8 |
9 |
10 | @bp.after_request
11 | def after_request(resp):
12 | print('>>> bp after_request')
13 | print(resp)
14 | return resp
15 |
16 |
17 | @bp.route('/', methods=['GET', 'POST'])
18 | def index():
19 | return 'bp index'
20 |
--------------------------------------------------------------------------------
/packages/dataclasses_sp/default_sp.py:
--------------------------------------------------------------------------------
1 | from dataclasses import dataclass
2 |
3 |
4 | @dataclass
5 | class C:
6 | x: int
7 | y: int
8 | z: int = 10
9 | # t: int # TypeError: non-default argument 't' follows default argument
10 |
11 |
12 | def main():
13 | c = C(1, 2)
14 | print(c)
15 | # c = C(1) # TypeError: __init__() missing 1 required positional argument: 'y'
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/errorhandler_app/readme.md:
--------------------------------------------------------------------------------
1 | ## refs
2 |
3 | - [python - Global error handler for any exception - Stack Overflow](https://stackoverflow.com/questions/29332056/global-error-handler-for-any-exception)
4 | - [blueprint error handler not working?? · Issue #1935 · pallets/flask](https://github.com/pallets/flask/issues/1935)
5 | - [Allow blueprint-local routing for errorhandlers · Issue #503 · pallets/flask](https://github.com/pallets/flask/issues/503)
6 |
--------------------------------------------------------------------------------
/prjs/006-make-1-million-requests-with-python-aiohttp/s1_aiohttp.py:
--------------------------------------------------------------------------------
1 | import asyncio
2 | from aiohttp import ClientSession
3 |
4 |
5 | async def hello(url):
6 | async with ClientSession() as session:
7 | async with session.get(url) as response:
8 | response = await response.read()
9 | print(response.decode('utf-8'))
10 |
11 |
12 | loop = asyncio.get_event_loop()
13 | loop.run_until_complete(hello("http://httpbin.org/headers"))
14 |
--------------------------------------------------------------------------------
/packages/filesystem/rm_dir.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 | import shutil
4 |
5 |
6 | def main():
7 | dir_path = 'temp_dir'
8 | print('dir <{}> exists: {}'.format(dir_path, os.path.exists(dir_path)))
9 | try:
10 | os.rmdir(dir_path)
11 | except OSError as e:
12 | print(e)
13 | try:
14 | shutil.rmtree(dir_path)
15 | except OSError as e:
16 | print(e)
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/packages/log_snippets/logger_sp.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import logging
3 |
4 |
5 | def main():
6 | logging.basicConfig(
7 | level=logging.INFO,
8 | format='%(asctime)s %(name)s %(levelname)s %(module)s %(funcName)s: %(message)s',
9 | datefmt='%Y-%m-%d %H:%M:%S'
10 | )
11 | logger = logging.getLogger()
12 | logger.info('hello world i')
13 | logger.warning('hello world w')
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/packages/mako_sp/hello_mako.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from mako.template import Template
3 |
4 |
5 | def simple_render():
6 | mytemplate = Template("hello world!")
7 | print(mytemplate.render())
8 |
9 |
10 | def render_arg():
11 | mytemplate = Template("hello, ${name}!")
12 | print(mytemplate.render(name="jack"))
13 |
14 |
15 | def main():
16 | simple_render()
17 | render_arg()
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
23 |
--------------------------------------------------------------------------------
/utils/002-show-progress/app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import sys
3 | import time
4 |
5 |
6 | def show_progress(p, n):
7 | sys.stdout.write('\r{}/{} ({:.2%})'.format(p, n, p / n))
8 |
9 |
10 | def main():
11 | print('=== start ===')
12 | n = 15
13 | l = range(n)
14 | for i, _ in enumerate(l):
15 | show_progress(i + 1, n)
16 | time.sleep(0.1)
17 | print('\n=== end ===')
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/basic/decorator_sp/register_deco.py:
--------------------------------------------------------------------------------
1 | _functions = {}
2 |
3 |
4 | def register(f):
5 | global _functions
6 | _functions[f.__name__] = f
7 | return f
8 |
9 |
10 | @register
11 | def foo():
12 | return 'bar'
13 |
14 |
15 | @register
16 | def baz():
17 | return 'qux'
18 |
19 |
20 | def main():
21 | print(_functions)
22 | print(_functions['foo']())
23 | print(_functions['baz']())
24 |
25 |
26 | if __name__ == '__main__':
27 | main()
28 |
--------------------------------------------------------------------------------
/basic/int_sp/int_str.py:
--------------------------------------------------------------------------------
1 | def main():
2 | i = 105555555
3 | s = str(i)
4 | print(s)
5 | print(int(s))
6 |
7 | hex_str1 = hex(i)
8 | print(hex_str1)
9 | print(type(hex_str1))
10 | print(int(hex_str1, 16))
11 |
12 | hex_str2 = '{:x}'.format(i)
13 | print(hex_str2)
14 | print(int(hex_str2, 16))
15 |
16 | print('{:04x}'.format(1))
17 | print('{:04x}'.format(100000))
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/basic/str_sp/str_num.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def str_isdigit():
5 | print("=== str_isdigit ===")
6 | print('"123".isdigit(): {}'.format('123'.isdigit()))
7 | print('"0x1234".isdigit(): {}'.format('0x1234'.isdigit()))
8 | print('"0123".isdigit(): {}'.format('0123'.isdigit()))
9 | print('"xc123".isdigit(): {}'.format('xc123'.isdigit()))
10 |
11 |
12 | def main():
13 | str_isdigit()
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/datetime_operation.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 |
4 | def main():
5 | dt1 = datetime.datetime(2018, 6, 27)
6 | dt2 = datetime.datetime(2018, 6, 28)
7 | print('max: {}'.format(max(dt1, dt2)))
8 | print('minus: {}'.format(dt1 - dt2))
9 | # print('plus: {}'.format(dt1 + dt2)) # TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.datetime'
10 |
11 |
12 | if __name__ == '__main__':
13 | main()
14 |
--------------------------------------------------------------------------------
/packages/asyncio_sp/readme.md:
--------------------------------------------------------------------------------
1 | - async 理解成标记,标记这个函数是可以异步的,从而可以被加入 event 中
2 | - await 理解成后面的内容可能是阻塞的,需要异步返回,这个点可以用来切换到其他的 event 去执行
3 |
4 |
5 | ## refs
6 |
7 | - [asyncio - 廖雪峰的官方网站](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432090954004980bd351f2cd4cc18c9e6c06d855c498000)
8 | - [async/await - 廖雪峰的官方网站](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00144661533005329786387b5684be385062a121e834ac7000)
9 |
--------------------------------------------------------------------------------
/packages/mypy_sp/typed_dict.py:
--------------------------------------------------------------------------------
1 | from mypy_extensions import TypedDict
2 |
3 |
4 | class MovieA(TypedDict):
5 | name: str
6 | year: int
7 |
8 |
9 | MovieB = TypedDict('Movie', {'name': str, 'year': int})
10 |
11 |
12 | def main():
13 | m1 = MovieA(name='foo', year=2028)
14 | m2 = MovieB(name='bar', year=2029)
15 | print(m1)
16 | print(m2)
17 | print(type(m1))
18 | print(type(m2))
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/basic/exception_sp/exception_to_str.py:
--------------------------------------------------------------------------------
1 | def main():
2 | print('Hello')
3 | try:
4 | # raise Exception('hello')
5 | raise Exception('foo', 'bar')
6 | except Exception as e:
7 | print(e)
8 | print(repr(e))
9 |
10 |
11 | if __name__ == '__main__':
12 | main()
13 |
14 |
15 | # str -> https://github.com/python/cpython/blob/master/Objects/exceptions.c#L104
16 | # repr -> https://github.com/python/cpython/blob/master/Objects/exceptions.c#L117
--------------------------------------------------------------------------------
/basic/type_sp/type_sp.py:
--------------------------------------------------------------------------------
1 | class Foo:
2 | """hello foo"""
3 | pass
4 |
5 |
6 | def main():
7 | print(type(1))
8 | print(type(3.3))
9 | print(type('s'))
10 | print(type(object()))
11 | print(type(object))
12 | print(type(int))
13 | print(type(Foo()))
14 | print(type(Foo))
15 | print('===')
16 | print(Foo.__name__)
17 | print(Foo.__class__)
18 | print(Foo.__doc__)
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/builtin_packages/argparse_sp/argparse_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import argparse
3 |
4 |
5 | def main():
6 | parser = argparse.ArgumentParser()
7 | parser.add_argument("-d", "--debug", action="store_true", help='debug mode on')
8 | parser.add_argument("your_name", help="your name")
9 |
10 | args = parser.parse_args()
11 |
12 | print(args)
13 | print(args.debug)
14 | print(args.your_name)
15 |
16 |
17 | if __name__ == '__main__':
18 | main()
19 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/datetime_util.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import datetime
3 |
4 |
5 | def _datetime(d):
6 | return datetime.datetime.strptime(d, '%Y-%m-%d %H:%M:%S')
7 |
8 |
9 | def _strftime(d):
10 | return d.strftime('%Y-%m-%d %H:%M:%S')
11 |
12 |
13 | def main():
14 | now = datetime.datetime.now()
15 | print(_strftime(now))
16 | t = _datetime("2017-09-03 17:37:48")
17 | print(t)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/frameworks/pytest_sp/samples/test_fixtures.py:
--------------------------------------------------------------------------------
1 | import pytest
2 |
3 |
4 | @pytest.fixture
5 | def smtp():
6 | import smtplib
7 | return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
8 |
9 |
10 | def test_ehlo(smtp):
11 | response, msg = smtp.ehlo()
12 | assert response == 250
13 | assert 0 # for demo purposes
14 |
15 |
16 | # [pytest fixtures: explicit, modular, scalable — pytest documentation](https://docs.pytest.org/en/latest/fixture.html#fixtures)
17 |
--------------------------------------------------------------------------------
/packages/typing_snippets/new_type_sp.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from typing import NewType
3 |
4 | UserId = NewType('UserId', int)
5 |
6 |
7 | def get_user_name(user_id: UserId) -> str:
8 | return {
9 | 1: "foo",
10 | 2: "bar"
11 | }.get(user_id)
12 |
13 |
14 | def main():
15 | some_id = UserId(1)
16 | print(some_id)
17 | print(get_user_name(some_id))
18 | print(get_user_name(1))
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/fields.py:
--------------------------------------------------------------------------------
1 | import datetime
2 | from pydantic import BaseModel
3 |
4 |
5 | class MyModel(BaseModel):
6 | dt: datetime.datetime
7 |
8 |
9 | def main():
10 | model = MyModel.parse_obj({
11 | "dt": "2018-04-13 17:42:42"
12 | })
13 | print(model.dt)
14 |
15 | model = MyModel.parse_obj({
16 | "dt": "2018-04-13 16:06:11.844265+08:00"
17 | })
18 | print(model.dt)
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/builtin_packages/urllib_parse_sp/urlencode_sp.py:
--------------------------------------------------------------------------------
1 | from urllib.parse import urlencode, unquote, quote
2 |
3 |
4 | def main():
5 | params = {'lang': 'en', 'tag': 'python'}
6 | print(urlencode(params))
7 |
8 | params = {'foo': 'Az0 +-_./\\'}
9 | print(urlencode(params))
10 |
11 | params = {'bwm': '大西瓜'}
12 | print(urlencode(params))
13 |
14 | print(quote('大西瓜'))
15 | print(unquote(quote('大西瓜')))
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/packages/log_snippets/readme.md:
--------------------------------------------------------------------------------
1 | - [日志(Logging) — The Hitchhiker's Guide to Python](http://pythonguidecn.readthedocs.io/zh/latest/writing/logging.html)
2 | - [Logging HOWTO — Python 3.6.4 documentation](https://docs.python.org/3.6/howto/logging.html)
3 | - [16.6. logging — Logging facility for Python — Python 3.6.4 documentation](https://docs.python.org/3.6/library/logging.html)
4 | - https://stackoverflow.com/questions/14058453/making-python-loggers-output-all-messages-to-stdout-in-addition-to-log
5 |
--------------------------------------------------------------------------------
/packages/stdout_snippets/print_to_stderr.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from __future__ import print_function
3 | import sys
4 |
5 |
6 | def eprint(*args, **kwargs):
7 | print(*args, file=sys.stderr, **kwargs)
8 |
9 |
10 | def main():
11 | sys.stderr.write('spam\n')
12 | print('spam', file=sys.stderr)
13 | eprint('spam')
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
19 |
20 | # https://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python
21 |
--------------------------------------------------------------------------------
/builtin_packages/collections_snippets/hello_deque.py:
--------------------------------------------------------------------------------
1 | from collections import deque
2 |
3 |
4 | def main():
5 | d = deque('ab')
6 | print(d)
7 | for elem in d:
8 | print(elem)
9 | d.append('c')
10 | print(d)
11 | print(d.pop())
12 | print(d.popleft())
13 | print(d)
14 | d.extend('def')
15 | d.extendleft('ghi')
16 | print(d)
17 | d = deque('jklmnop', 3)
18 | print(d)
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
24 |
--------------------------------------------------------------------------------
/builtin_packages/csv_sp/append_writer.py:
--------------------------------------------------------------------------------
1 | import csv
2 |
3 |
4 | def rows(start, stop):
5 | for i in range(start, stop):
6 | yield (i, i * i, ',')
7 |
8 |
9 | def write_rows(start, stop):
10 | with open('some.csv', 'a', newline='') as f:
11 | writer = csv.writer(f)
12 | writer.writerows(rows(start, stop))
13 |
14 |
15 | def main():
16 | write_rows(100, 105)
17 | write_rows(110, 115)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
23 |
--------------------------------------------------------------------------------
/sdks/qiniu/config/__init__.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 | __all__ = ['QiniuConfig']
5 |
6 | app_config_file_name = os.environ.get('QINIU_CONFIG', '')
7 | if not app_config_file_name:
8 | raise Exception('No QINIU_CONFIG in os environ, should be something like: xxx_config')
9 | app_config_module = __import__('config.{}'.format(app_config_file_name), fromlist=['Config'])
10 |
11 | QiniuConfig = app_config_module.Config
12 |
13 | # should set env: export QINIU_CONFIG=xxx_config
14 |
--------------------------------------------------------------------------------
/basic/func_with_docstring.py:
--------------------------------------------------------------------------------
1 |
2 | def hello(name, times=1):
3 | """say hello to x, y times
4 | :param name: name to say greeting
5 | :param times: say hello how many times
6 | :type name: str
7 | :type times: int
8 | :return: the whole greeting str
9 | :rtype: str
10 | """
11 | hs = 'hello ' * times
12 | return f'{hs}{name}!'
13 |
14 |
15 | def main():
16 | s = hello('foo', 3)
17 | print(s)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/packages/jieba_sp/recipe.py:
--------------------------------------------------------------------------------
1 | import jieba
2 |
3 |
4 | def cut_txt(txt: str):
5 | seg_list = jieba.cut(txt, cut_all=False)
6 | print(f"{txt} -> " + " | ".join(seg_list)) # 精确模式
7 |
8 |
9 |
10 | def main():
11 | jieba.load_userdict('recipe.txt')
12 |
13 | titles = [
14 | '冻干水果牛轧糖',
15 | '老北京烫饭',
16 | '芋头蛋卷',
17 | '葱油芋头',
18 | ]
19 | for title in titles:
20 | cut_txt(title)
21 |
22 |
23 | if __name__ == '__main__':
24 | main()
25 |
--------------------------------------------------------------------------------
/packages/redis_sp/batch_insert_performance.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import time
3 | import redis
4 | from profilehooks import profile
5 |
6 |
7 | @profile
8 | def insert_to_redis(r, n, start):
9 | for i in xrange(n):
10 | r.set(str(i) + str(start), 'x')
11 |
12 |
13 | def main():
14 | start = int(time.time())
15 | r = redis.StrictRedis(host='localhost', port=6379, db=0)
16 |
17 | insert_to_redis(r, 10000, start)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/utils/013-this-week/app.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 |
4 | def this_monday_and_related_sunday():
5 | now = datetime.datetime.now()
6 | today = datetime.datetime(now.year, now.month, now.day)
7 | this_monday = today - datetime.timedelta(now.weekday())
8 | related_sunday = this_monday + datetime.timedelta(6)
9 | return this_monday, related_sunday
10 |
11 |
12 | def main():
13 | print(this_monday_and_related_sunday())
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/builtin_packages/inspect_sp/source_code.py:
--------------------------------------------------------------------------------
1 | import inspect
2 |
3 | from defines import Foo, foo
4 |
5 |
6 | def main():
7 | print(inspect.getdoc(Foo))
8 | print(inspect.getsource(Foo))
9 | print(inspect.getsourcefile(Foo))
10 | print(inspect.getsourcelines(Foo))
11 | print(inspect.getcomments(Foo))
12 | print(inspect.getcomments(Foo.a))
13 | print(inspect.getcomments(Foo.bar))
14 | print(inspect.getcomments(foo))
15 |
16 |
17 | if __name__ == '__main__':
18 | main()
19 |
--------------------------------------------------------------------------------
/packages/peewee_sp/models.py:
--------------------------------------------------------------------------------
1 | import datetime
2 | from peewee import *
3 |
4 | from .mysql_db import db
5 |
6 |
7 | class Person(Model):
8 |
9 | name = CharField()
10 | birthday = DateField()
11 | is_relative = BooleanField()
12 |
13 | class Meta:
14 | database = db
15 | table_name = 'person'
16 |
17 |
18 | class Ppp(Model):
19 | name = CharField()
20 | create_time = DateTimeField(default=datetime.datetime.now)
21 |
22 | class Meta:
23 | database = db
24 |
--------------------------------------------------------------------------------
/packages/socket_sp/readme.md:
--------------------------------------------------------------------------------
1 | # readme
2 |
3 | - [套接字编程指南 — Python 3.8.0 文档](https://docs.python.org/zh-cn/3/howto/sockets.html)
4 | - [Socket Programming in Python (Guide) – Real Python](https://realpython.com/python-sockets/)
5 | - [[译]Python 中的 Socket 编程(指南)](https://keelii.com/2018/09/24/socket-programming-in-python/)
6 | - [Python 网络编程 | 菜鸟教程](https://www.runoob.com/python/python-socket.html)
7 | - [socket — Low-level networking interface — Python 3.8.0 documentation](https://docs.python.org/3/library/socket.html)
--------------------------------------------------------------------------------
/basic/class_sp/class_demo.py:
--------------------------------------------------------------------------------
1 | class A:
2 |
3 | def __init__(self):
4 | pass
5 |
6 | def hello(self):
7 | print('A: Hello')
8 |
9 |
10 | class A2(A):
11 |
12 | def __init__(self):
13 | super(A2, self).__init__()
14 |
15 | def hello(self):
16 | super(A2, self).hello()
17 | print('A2: Hello')
18 |
19 |
20 | def main():
21 | a = A()
22 | a.hello()
23 | a2 = A2()
24 | a2.hello()
25 |
26 |
27 | if __name__ == '__main__':
28 | main()
29 |
--------------------------------------------------------------------------------
/packages/peewee_sp/mysql_db.py:
--------------------------------------------------------------------------------
1 | from playhouse.db_url import connect
2 |
3 |
4 | MYSQL_USER = "root"
5 | MYSQL_PASSWORD = ""
6 | MYSQL_HOST = "localhost"
7 | MYSQL_PORT = 3306
8 | MYSQL_DB_NAME = "test_db"
9 | MYSQL_CHARSET = 'utf8mb4'
10 | MYSQL_DB_URL = "mysql://{user}:{password}@{host}:{port}/{db_name}?charset=utf8mb4".format(
11 | user=MYSQL_USER,
12 | password=MYSQL_PASSWORD,
13 | host=MYSQL_HOST,
14 | port=MYSQL_PORT,
15 | db_name=MYSQL_DB_NAME,
16 | )
17 | db = connect(MYSQL_DB_URL)
18 |
--------------------------------------------------------------------------------
/packages/requests_sp/request_through_proxy.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def main():
5 | proxies = {'http': 'http://localhost:8886', 'https': 'http://localhost:8886'}
6 | r = requests.get('https://httpbin.org/get', proxies=proxies, verify=False)
7 | print(r.json())
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
13 |
14 | # http://www.cnblogs.com/clip/p/python-requests-proxy-and-ssl-config.html
15 | # https://stackoverflow.com/questions/10667960/python-requests-throwing-sslerror
16 |
--------------------------------------------------------------------------------
/packages/gevent_snippets/patch_stdout.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from gevent import monkey; monkey.patch_sys()
3 | import gevent
4 | # import time
5 |
6 |
7 | def f(n):
8 | # time.sleep(0.01)
9 | for i in range(n):
10 | print gevent.getcurrent(), i
11 | # 是失败的
12 |
13 |
14 | def main():
15 | g1 = gevent.spawn(f, 5)
16 | g2 = gevent.spawn(f, 5)
17 | g3 = gevent.spawn(f, 5)
18 | gevent.joinall([g1, g2, g3])
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/timezone_sp.py:
--------------------------------------------------------------------------------
1 | import time
2 | import datetime
3 |
4 |
5 | def main():
6 | print(time.timezone)
7 | print(time.tzname)
8 | print(time.localtime())
9 | print(time.localtime().tm_isdst)
10 | print(time.altzone)
11 | utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone
12 | utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
13 | print(datetime.timezone(offset=utc_offset))
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/builtin_packages/subprocess_sp/run_apple_script.py:
--------------------------------------------------------------------------------
1 | import subprocess
2 |
3 |
4 | def main():
5 | ret = subprocess.run(['osascript', '-e', r'''set input_text to text returned of (display dialog "Please input Text here:" default answer "hello world" with title "exchange to python")'''], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
6 | print(ret)
7 | print(ret.stdout.strip().decode('utf-8'))
8 |
9 |
10 | if __name__ == '__main__':
11 | main()
12 |
13 |
14 | # https://stackoverflow.com/a/24446693/3936457
15 |
--------------------------------------------------------------------------------
/packages/mako_sp/python_block.py:
--------------------------------------------------------------------------------
1 | from mako.template import Template
2 |
3 |
4 | tpl = """
5 | a, b = ${a}, ${b}
6 | <%
7 | show_max = not (a < 0 and b < 0)
8 | %>
9 | % if show_max:
10 | max(a, b) = ${max(a, b)}
11 | % else:
12 | min(a, b) = ${min(a, b)}
13 | % endif
14 | """
15 |
16 |
17 | def main():
18 | mytemplate = Template(tpl)
19 | print(mytemplate.render(a=1, b=2))
20 | print(mytemplate.render(a=-1, b=2))
21 | print(mytemplate.render(a=-1, b=-2))
22 |
23 |
24 | if __name__ == '__main__':
25 | main()
26 |
--------------------------------------------------------------------------------
/prjs/008-marshmallow-schematics/app.py:
--------------------------------------------------------------------------------
1 | from schemas import FooSchema
2 | from dtos import FooDTO
3 | from daos import FooDAO
4 |
5 |
6 | def main():
7 | dao = FooDAO('baz_dao')
8 | d = {"bar": "baz_d"}
9 |
10 | dto = FooDTO(d)
11 | print(dto.serialize())
12 |
13 | dto, errors = FooSchema().dump(dao)
14 | print(dto.serialize())
15 |
16 | # print(FooSchema().load(dao))
17 | # print(FooSchema().load(d))
18 | # print(FooSchema().dump(dao))
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/basic/context_manager_sp/context_manager_class_approach.py:
--------------------------------------------------------------------------------
1 | class CustomOpen(object):
2 | def __init__(self, filename):
3 | self.file = open(filename)
4 |
5 | def __enter__(self):
6 | return self.file
7 |
8 | def __exit__(self, ctx_type, ctx_value, ctx_traceback):
9 | self.file.close()
10 |
11 |
12 | def main():
13 | with CustomOpen('test_file') as f:
14 | # a = 1 / 0
15 | contents = f.read(1000)
16 | print(contents)
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/basic/yield_sp/yield_.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def my_range(n):
5 | for i in range(0, n):
6 | yield i
7 |
8 |
9 | def main():
10 | print('type: {}, {}'.format(type(my_range(2)), my_range(2)))
11 | print('type: {}, {}'.format(type(list(my_range(2))), list(my_range(2))))
12 | # print(len(my_range(2))) # TypeError: object of type 'generator' has no len()
13 | # print(my_range(2)[0]) # TypeError: 'generator' object has no attribute '__getitem__'
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/builtin_packages/sys_sp/sys_stdin.py:
--------------------------------------------------------------------------------
1 | import sys
2 |
3 |
4 | def main():
5 | t = sys.stdin.read() # will block if no std input
6 | print(t)
7 |
8 |
9 | if __name__ == '__main__':
10 | main()
11 |
12 | # https://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin/1450398#1450398
13 | # sys.stdin is a file-like object on which you can call functions read or readlines if you want to read everything or
14 | # you want to read everything and split it by newline automatically. (You need to import sys for this to work.)
15 |
--------------------------------------------------------------------------------
/basic/range_sp/range_py2.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def print_range(r):
5 | print(list(r))
6 |
7 |
8 | def main():
9 | print(range(0))
10 | print_range(range(1))
11 | print_range(range(0, 1))
12 | print_range(range(0, 1, 1))
13 | print_range(range(10, 15, 4))
14 | print_range(range(-2))
15 | print_range(range(0, -2, -1))
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
21 |
22 | # https://docs.python.org/2/library/functions.html#range
23 | # https://docs.python.org/2/library/functions.html#xrange
24 |
--------------------------------------------------------------------------------
/packages/aiohttp/aiohttp_client.py:
--------------------------------------------------------------------------------
1 | import aiohttp
2 | import asyncio
3 | import async_timeout
4 |
5 |
6 | async def fetch(session, url):
7 | async with async_timeout.timeout(10):
8 | async with session.get(url) as response:
9 | return await response.text()
10 |
11 |
12 | async def main():
13 | async with aiohttp.ClientSession() as session:
14 | html = await fetch(session, 'http://httpbin.org/delay/5.1')
15 | print(html)
16 |
17 |
18 | loop = asyncio.get_event_loop()
19 | loop.run_until_complete(main())
20 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/field_alias_config.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseModel
2 |
3 |
4 | class Foo(BaseModel):
5 | bar: str
6 | baz: str
7 |
8 | class Config:
9 | fields = {
10 | 'bar': {
11 | 'alias': 'Bar',
12 | },
13 | }
14 |
15 |
16 | def main():
17 | foo_data = {
18 | "Bar": "barrrrr",
19 | "baz": "bazzzzz",
20 | }
21 | foo = Foo.parse_obj(foo_data)
22 | print(foo)
23 | print(foo.dict())
24 |
25 |
26 | if __name__ == '__main__':
27 | main()
28 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/model_init.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseModel
2 |
3 |
4 | class Foo(BaseModel):
5 | bar: int = 0
6 | baz: str = ''
7 |
8 |
9 | def main():
10 | # use default
11 | foo = Foo()
12 | print(foo)
13 | # set values
14 | foo.bar = 3
15 | foo.baz = 'x'
16 | print(foo)
17 | # init with kwargs
18 | foo = Foo(bar=1, baz='2')
19 | print(foo)
20 | # init with a dict
21 | foo = Foo.parse_obj({"bar": 1, "baz": '2'})
22 | print(foo)
23 |
24 |
25 | if __name__ == '__main__':
26 | main()
27 |
--------------------------------------------------------------------------------
/basic/bytes_sp/bytes_int.py:
--------------------------------------------------------------------------------
1 | def int_to_bytes(x):
2 | return x.to_bytes((x.bit_length() + 7) // 8, 'big')
3 |
4 |
5 | def int_from_bytes(xbytes):
6 | return int.from_bytes(xbytes, 'big')
7 |
8 |
9 | def main():
10 | print(bytes([255]))
11 | # print(bytes([1024])) # ValueError: bytes must be in range(0, 256)
12 |
13 | b = int_to_bytes(1024)
14 | print(b)
15 | i = int_from_bytes(b)
16 | print(i)
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
22 |
23 |
24 | # https://stackoverflow.com/a/30375198/3936457
25 |
--------------------------------------------------------------------------------
/basic/str_sp/str_unicode.py:
--------------------------------------------------------------------------------
1 | def to_utf8(s):
2 | if isinstance(s, str):
3 | return s.encode('utf-8')
4 | return s
5 |
6 |
7 | def to_unicode(s):
8 | if isinstance(s, bytes):
9 | return s.decode('utf-8')
10 | return s
11 |
12 |
13 | def main():
14 | print(to_utf8('哈哈哈'), to_unicode(b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88'))
15 |
16 | # print('哈哈哈'.encode('ascii')) # UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/builtin_packages/base64_sp/urlsafe_encode_decode.py:
--------------------------------------------------------------------------------
1 | import base64
2 |
3 |
4 | # urlsafe_b64encode doc: The alphabet uses '-' instead of '+' and '_' instead of '/'.
5 |
6 |
7 | def urlsafe_encode_and_decode(s):
8 | print('=== urlsafe_encode_and_decode ===')
9 | print(s)
10 | s_encoded = base64.urlsafe_b64encode(s.encode('utf-8'))
11 | print(s_encoded)
12 | print(base64.urlsafe_b64decode(s_encoded).decode('utf-8'))
13 |
14 |
15 | def main():
16 | urlsafe_encode_and_decode('hello')
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/utils/003-upload-temp-file-with-string-io/app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import requests
3 | from StringIO import StringIO
4 |
5 |
6 | def main():
7 | text = 'hello world!'
8 | temp = StringIO()
9 | temp.write(text)
10 | temp.seek(0)
11 | temp.name = 'hello.txt'
12 | files = {
13 | 'file_x': temp
14 | }
15 | ret = requests.post('https://httpbin.org/post', files=files)
16 | print ret.text
17 | temp.close()
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
23 | # https://segmentfault.com/a/1190000005999222
24 |
--------------------------------------------------------------------------------
/packages/package_snippets/print_package.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | import foo
6 | print('__name__: {}'.format(__name__))
7 | print('foo: {}'.format(foo))
8 | print('foo.print_foo: {}'.format(foo.print_foo))
9 | # print('foo.foo_a: {}'.format(foo.foo_a)) # AttributeError: 'module' object has no attribute 'foo_a'
10 |
11 |
12 | def main2():
13 | import foo.foo_a
14 | print('foo: {}'.format(foo))
15 | print('foo.foo_a: {}'.format(foo.foo_a))
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 | main2()
21 |
--------------------------------------------------------------------------------
/packages/pendulum_sp/timezone.py:
--------------------------------------------------------------------------------
1 | import pendulum
2 |
3 |
4 | def main():
5 | now = pendulum.now()
6 | print(now.tz, now.timezone, now.timezone_name, now.tzinfo)
7 | print(now)
8 |
9 | tz_shanghai = pendulum.timezone('Asia/Shanghai')
10 | print(now.in_tz(tz_shanghai))
11 |
12 | tz_utc = pendulum.timezone('UTC')
13 | print(now.in_tz(tz_utc))
14 |
15 | dts = "2018-04-13T10:13:37.225416+00:00"
16 | dt = pendulum.parse(dts)
17 | dt = dt.in_tz(tz_shanghai)
18 | print(dt)
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/packages/typing_snippets/union_sp.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from typing import Union, Optional
3 |
4 |
5 | def foo(x: Union[str, int]) -> None:
6 | print(x)
7 |
8 |
9 | def foo2(x: int) -> Union[str, None]:
10 | if x < 0:
11 | return
12 | return str(x)
13 |
14 |
15 | def foo3(x: int) -> Optional[str]:
16 | return foo2(x)
17 |
18 |
19 | def main():
20 | foo(1)
21 | foo('a')
22 | # foo([1, 2])
23 |
24 | foo2(10)
25 | foo2(-10)
26 | foo3(10)
27 | foo3(-10)
28 |
29 |
30 | if __name__ == '__main__':
31 | main()
32 |
--------------------------------------------------------------------------------
/packages/jieba_sp/jieba_sp.py:
--------------------------------------------------------------------------------
1 | import jieba
2 |
3 |
4 | def main():
5 | seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
6 | print("Full Mode: " + ",".join(seg_list)) # 全模式
7 |
8 | seg_list = jieba.cut("我来到北京清华大学", cut_all=False)
9 | print("Default Mode: " + ",".join(seg_list)) # 精确模式
10 |
11 | seg_list = jieba.cut("他来到了网易杭研大厦") # 默认是精确模式
12 | print(", ".join(seg_list))
13 |
14 | seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造") # 搜索引擎模式
15 | print(", ".join(seg_list))
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/packages/peewee_sp/index_sp.py:
--------------------------------------------------------------------------------
1 | from peewee import *
2 | from packages.peewee_sp.mysql_db import db
3 |
4 |
5 | class BaseModel(Model):
6 | class Meta:
7 | database = db
8 |
9 |
10 | class BigIndex(BaseModel):
11 | my_integer = IntegerField(index=True)
12 | my_char = CharField(unique=True)
13 |
14 | class Meta:
15 | indexes = (
16 | (('my_integer', 'my_char', ), False),
17 | )
18 |
19 |
20 | def main():
21 | db.connect()
22 | db.create_tables([BigIndex])
23 |
24 |
25 | if __name__ == '__main__':
26 | main()
27 |
--------------------------------------------------------------------------------
/packages/stdout_snippets/get_shell_width_height.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 |
5 | def get_shell_width_height():
6 | print('=== get_shell_width_height ===')
7 | print os.popen('stty size', 'r').read().split()
8 | rows, columns = os.popen('stty size', 'r').read().split()
9 | print('width: {}, height: {}'.format(columns, rows))
10 |
11 |
12 | def main():
13 | get_shell_width_height()
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
19 | # https://stackoverflow.com/questions/566746/how-to-get-linux-console-window-width-in-python
20 |
--------------------------------------------------------------------------------
/builtin_packages/re_sp/hello_re.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 |
4 | def search_hello(s):
5 | m = re.search('hello', s)
6 | if m:
7 | print(m.group())
8 | else:
9 | print('not found')
10 |
11 |
12 | def search_hello2(s):
13 | prog = re.compile('hello')
14 | m = prog.search(s)
15 | if m:
16 | print(m.group())
17 | else:
18 | print('not found')
19 |
20 |
21 | def main():
22 | search_hello('hello world')
23 | search_hello('hi world')
24 | search_hello2('hello world')
25 |
26 | if __name__ == '__main__':
27 | main()
28 |
--------------------------------------------------------------------------------
/packages/log_snippets/log_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import logging
3 |
4 |
5 | def hello_func():
6 | logging.info('hello %s', 'world')
7 |
8 |
9 | def main():
10 | # config logging
11 | logging.basicConfig(
12 | filename='hello.log', # to `pwd`
13 | level=logging.INFO,
14 | format='%(asctime)s %(name)s %(levelname)s %(module)s %(funcName)s: %(message)s',
15 | datefmt='%Y-%m-%d %H:%M:%S'
16 | )
17 | # log it
18 | logging.info('in the main')
19 | hello_func()
20 |
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/packages/log_snippets/log_to_stdout.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import logging
3 | import sys
4 |
5 |
6 | def hello_func():
7 | logging.info('hello %s', 'world')
8 |
9 |
10 | def main():
11 | # config logging
12 | logging.basicConfig(
13 | stream=sys.stdout,
14 | level=logging.INFO,
15 | format='%(asctime)s %(name)s %(levelname)s %(module)s %(funcName)s: %(message)s',
16 | datefmt='%Y-%m-%d %H:%M:%S'
17 | )
18 | # log it
19 | logging.info('in the main')
20 | hello_func()
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/packages/marshmallow_sp/nest_field.py:
--------------------------------------------------------------------------------
1 | from marshmallow import Schema, fields
2 |
3 |
4 | class ButtonSchema(Schema):
5 | name = fields.Str()
6 |
7 |
8 | class MenuSchema(Schema):
9 | buttons = fields.Nested(ButtonSchema(many=True))
10 |
11 |
12 | def main():
13 | result = MenuSchema().load({
14 | 'buttons': [
15 | {
16 | 'name': 'foo'
17 | },
18 | {
19 | 'name': 'bar'
20 | }
21 | ]
22 | })
23 | print(result)
24 |
25 |
26 | if __name__ == '__main__':
27 | main()
28 |
--------------------------------------------------------------------------------
/builtin_packages/base64_sp/base64_encode.py:
--------------------------------------------------------------------------------
1 | import base64
2 |
3 |
4 | def int_to_bytes(x):
5 | return x.to_bytes((x.bit_length() + 7) // 8, 'big')
6 |
7 |
8 | def print_b64encode(bs):
9 | print(base64.b64encode(bs))
10 |
11 |
12 | def main():
13 | print_b64encode(b'\xff\xff\xff')
14 | print_b64encode('a'.encode('utf-8'))
15 | print_b64encode('aa'.encode('utf-8'))
16 | print_b64encode('aaa'.encode('utf-8'))
17 | print_b64encode('aaaa'.encode('utf-8'))
18 | print_b64encode(int_to_bytes(105697106))
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/packages/filesystem/join_path.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 |
5 | def get_file_dir_path():
6 | return os.path.dirname(os.path.realpath(__file__))
7 |
8 |
9 | def join_path(path_a, path_b):
10 | return os.path.join(path_a, path_b)
11 |
12 |
13 | def main():
14 | path_a = get_file_dir_path()
15 | path_b = "../common_text/foo.txt"
16 | path_ab = join_path(path_a, path_b)
17 | print('"{}" + "{}" = "{}"'.format(path_a, path_b, path_ab))
18 | with open(path_ab) as f:
19 | print(f.read())
20 |
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/packages/schematics_snippets/schematics_list_type.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from schematics.models import Model
3 | from schematics.types import ListType, StringType, ModelType
4 |
5 |
6 | # list of string
7 | # list of model
8 |
9 |
10 | class A(Model):
11 | s = StringType()
12 |
13 |
14 | class B(Model):
15 | sl = ListType(StringType)
16 | ml = ListType(ModelType(A))
17 |
18 |
19 | def main():
20 | b = B()
21 | b.sl = ["ee", "ff"]
22 | b.ml = [A({"s": "ss"}), A({"s": "sss"})]
23 | print(b.serialize())
24 |
25 |
26 | if __name__ == '__main__':
27 | main()
28 |
--------------------------------------------------------------------------------
/basic/class_sp/inner_meta_class.py:
--------------------------------------------------------------------------------
1 | class Base:
2 | class Meta:
3 | foo = 'f'
4 | bar = 'b'
5 |
6 |
7 | class Fancy(Base):
8 | class Meta:
9 | bar = 'ar'
10 | baz = 'baz'
11 |
12 |
13 | def main():
14 | print(Base.Meta.foo, Base.Meta.bar)
15 | # print(Fancy.Meta.foo, Fancy.Meta.bar, Fancy.Meta.baz) # AttributeError: type object 'Meta' has no attribute 'foo'
16 | print(Fancy.Meta.bar, Fancy.Meta.baz)
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
22 | # https://stackoverflow.com/questions/10344197/how-does-djangos-meta-class-work
23 |
--------------------------------------------------------------------------------
/basic/i__str____repr__.py:
--------------------------------------------------------------------------------
1 | class Foo(object):
2 |
3 | def __init__(self, id, name):
4 | self.id = id
5 | self.name = name
6 |
7 | def __repr__(self):
8 | print("=== __repr__ ===")
9 | return f'Foo(id={self.id}, name={self.name})'
10 |
11 | def __str__(self):
12 | print("=== __str__ ===")
13 | return f'Foo(id={self.id}, name={self.name})'
14 |
15 |
16 | def main():
17 | foo = Foo(1, 'fooo')
18 | print(foo)
19 | print(str(foo))
20 | print([foo])
21 | print(str([foo]))
22 |
23 |
24 | if __name__ == '__main__':
25 | main()
26 |
--------------------------------------------------------------------------------
/packages/config_snippets/config/__init__.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 | __all__ = ['CSConfig']
5 |
6 | app_config_file_name = os.environ.get('CS_CONFIG', '')
7 | if not app_config_file_name:
8 | raise Exception('No CS_CONFIG in os environ, should be something like: xxx_config')
9 | app_config_module = __import__('config.{}'.format(app_config_file_name), fromlist=['Config'])
10 |
11 | CSConfig = app_config_module.Config
12 |
13 | # should set env: export CS_CONFIG=xxx_config
14 | # in gitignore:
15 | # #/models/config_snippets/config/*_config.py
16 | # !/models/config_snippets/config/sample_config.py
17 |
--------------------------------------------------------------------------------
/packages/typing_snippets/type_hint_sp.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from typing import List
3 |
4 |
5 | def greeting(name: str) -> str:
6 | if not name:
7 | return 123 # PyCharm 会有提示,但是运行不会有异常
8 | return 'Hello ' + name
9 |
10 |
11 | def greeting_many(names: List[str]) -> List[str]:
12 | return [greeting(name) for name in names]
13 |
14 |
15 | def main():
16 | print(greeting("clip"))
17 | # print(greeting(123)) # TypeError: must be str, not int
18 | print(greeting(""))
19 | print(greeting_many(['clip', 'cliip', 'cliiip']))
20 |
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/hello_pydantic.py:
--------------------------------------------------------------------------------
1 | from datetime import datetime
2 | from typing import List
3 | from pydantic import BaseModel
4 |
5 |
6 | class User(BaseModel):
7 | id: int
8 | name = 'John Doe'
9 | signup_ts: datetime = None
10 | friends: List[int] = []
11 |
12 |
13 | def main():
14 | external_data = {'id': '123', 'signup_ts': '2017-06-01 12:22', 'friends': [1, '2', b'3']}
15 | user = User(**external_data)
16 | print(user)
17 | print(user.id)
18 | # print(user.get('id')) # AttributeError: 'User' object has no attribute 'get'
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/packages/filesystem/rm_file.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 |
5 | def main():
6 | file_path = 'temp.txt'
7 | print('file <{}> exists: {}'.format(file_path, os.path.exists(file_path)))
8 | try:
9 | os.remove(file_path)
10 | except OSError as e:
11 | print(e)
12 |
13 |
14 | if __name__ == '__main__':
15 | main()
16 |
17 | # ref Python Delete/Remove a File If Exists On Disk – nixCraft https://www.cyberciti.biz/faq/python-delete-remove-file-if-exists-on-disk/
18 | # python - How to delete a file or folder? - Stack Overflow https://stackoverflow.com/questions/6996603/how-to-delete-a-file-or-folder
--------------------------------------------------------------------------------
/sdks/qiniu/app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import qiniu
3 | from config import QiniuConfig
4 | from qiniu_client import QiniuClient
5 |
6 |
7 | def main():
8 | qiniu_client = QiniuClient(QiniuConfig.ACCESS_KEY, QiniuConfig.SECRET_KEY,
9 | QiniuConfig.BUCKET, QiniuConfig.HOST, QiniuConfig.PREFIX)
10 | file_name = '../../models/_common_img/google.jpg.png'
11 | with open(file_name) as f:
12 | url = qiniu_client.upload_img(f.read())
13 | print(url)
14 | url2 = qiniu_client.upload_img_file(file_name)
15 | print(url2)
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/readme.md:
--------------------------------------------------------------------------------
1 | ## samples
2 |
3 | - [init model](model_init.py)
4 | - [validation error](validation_error.py)
5 | - [use Unit to implement poly field](poly_field_with_union.py)
6 | - [recursive models](recursive_models.py)
7 | - [PEP 484 types](pep_484_types.py)
8 | - [serialization](serialization.py)
9 | - [field alias config](field_alias_config.py)
10 | - exotic types
11 | - helper functions
12 | - model config
13 | - settings
14 | - dynamic model creation
15 | - usage with mypy
16 | - Faux Immutability
17 | - Copy and Values
18 | - Abstract Base Classes
19 |
20 |
21 | ## refs
22 |
23 | - https://pydantic-docs.helpmanual.io/
24 |
--------------------------------------------------------------------------------
/packages/asyncio_sp/hello_asyncio.py:
--------------------------------------------------------------------------------
1 | import threading
2 | import asyncio
3 |
4 |
5 | @asyncio.coroutine
6 | def hello():
7 | print(f'Hello World! ({threading.current_thread()})')
8 | # 异步调用 asyncio.sleep(1):
9 | yield from asyncio.sleep(1)
10 | print(f'Hello again! ({threading.current_thread()})')
11 |
12 |
13 | def main():
14 | # 获取 EventLoop
15 | loop = asyncio.get_event_loop()
16 | # 执行 coroutine
17 | loop.run_until_complete(hello())
18 |
19 | tasks = [hello(), hello(), hello()]
20 | loop.run_until_complete(asyncio.wait(tasks))
21 | loop.close()
22 |
23 |
24 | if __name__ == '__main__':
25 | main()
26 |
--------------------------------------------------------------------------------
/packages/schematics_snippets/schematics_dict_type.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from schematics.models import Model
3 | from schematics.types import DictType, StringType, ModelType
4 |
5 |
6 | # dict of string
7 | # dict of model
8 |
9 |
10 | class A(Model):
11 | s = StringType()
12 |
13 |
14 | class B(Model):
15 | sd = DictType(StringType)
16 | md = DictType(ModelType(A))
17 |
18 |
19 | def main():
20 | b = B()
21 | b.sd = {
22 | "e": "ee",
23 | "f": "ff"
24 | }
25 | b.md = {
26 | "g": A({"s": "ss"})
27 | }
28 | print(b.serialize())
29 |
30 |
31 | if __name__ == '__main__':
32 | main()
33 |
--------------------------------------------------------------------------------
/prjs/001-covert-img-to-rgb/app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from PIL import Image, ImageCms
3 |
4 |
5 | def covert_a():
6 | img = Image.open('./img/in.jpg')
7 | img = img.convert('RGB')
8 | img.save('./img/out_a.jpg')
9 |
10 |
11 | def covert_b():
12 | img = Image.open("./img/in.jpg")
13 | img = ImageCms.profileToProfile(img, './icc/JapanColor2001Coated.icc', './icc/sRGB Color Space Profile.icm',
14 | renderingIntent=0, outputMode='RGB')
15 | img.save('./img/out_b.jpg', quality=100)
16 |
17 |
18 | def main():
19 | covert_a()
20 | covert_b()
21 |
22 |
23 | if __name__ == '__main__':
24 | main()
25 |
--------------------------------------------------------------------------------
/basic/str_sp/str_escape.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def main():
5 | u = u'2017年'
6 | print(u)
7 | print(u.encode('unicode-escape'))
8 | print(repr(u.encode('unicode-escape')))
9 | print(u.encode('unicode-escape').encode('string-escape'))
10 | print(repr(u.encode('unicode-escape').encode('string-escape')))
11 | print(u.encode('unicode-escape').encode('string-escape').encode('string-escape'))
12 | print(repr(u.encode('unicode-escape').encode('string-escape').encode('string-escape')))
13 | print(u.encode('unicode-escape').encode('unicode-escape').encode('unicode-escape'))
14 |
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/flask_request/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, request, jsonify
2 | app = Flask(__name__)
3 |
4 |
5 |
6 | @app.route("/")
7 | def get():
8 | return "Hello World!"
9 |
10 |
11 |
12 | @app.route("/get", methods=['GET'])
13 | def app_get():
14 | print(request.headers)
15 | return jsonify({
16 | "args": request.args,
17 | })
18 |
19 |
20 | @app.route("/post", methods=['POST'])
21 | def app_post():
22 | return jsonify({
23 | "json": request.json,
24 | "form": request.form,
25 | })
26 |
27 |
28 | def main():
29 | app.run(debug=True)
30 |
31 |
32 | if __name__ == '__main__':
33 | main()
34 |
--------------------------------------------------------------------------------
/packages/peewee_sp/databases.py:
--------------------------------------------------------------------------------
1 | from mysql_conn import MYSQL_DB_NAME, conn
2 |
3 |
4 | def create_database():
5 | conn.cursor().execute(
6 | f'CREATE DATABASE IF NOT EXISTS {MYSQL_DB_NAME} '
7 | 'DEFAULT CHARACTER SET utf8mb4 '
8 | 'DEFAULT COLLATE utf8mb4_unicode_ci;',
9 | )
10 | print('=== create database done ===')
11 |
12 |
13 | def drop_database():
14 | conn.cursor().execute(f'DROP DATABASE IF EXISTS {MYSQL_DB_NAME};')
15 | print('=== drop database done ===')
16 |
17 |
18 | def main():
19 | create_database()
20 | drop_database()
21 | conn.close()
22 |
23 |
24 | if __name__ == '__main__':
25 | main()
26 |
--------------------------------------------------------------------------------
/packages/pendulum_sp/with_datetime.py:
--------------------------------------------------------------------------------
1 | import datetime
2 | import pendulum
3 |
4 |
5 | def main():
6 | dt = datetime.datetime(2016, 8, 20, 1, 2, 3)
7 | dt = pendulum.instance(dt)
8 | print(dt)
9 |
10 | dt = datetime.datetime(2016, 8, 20, 1, 2, 3)
11 | dt = pendulum.instance(dt, 'Asia/Shanghai')
12 | print(dt)
13 |
14 | dt = datetime.datetime(
15 | year=dt.year,
16 | month=dt.month,
17 | day=dt.day,
18 | hour=dt.hour,
19 | minute=dt.minute,
20 | second=dt.second,
21 | microsecond=dt.microsecond,
22 | )
23 | print(dt)
24 |
25 |
26 | if __name__ == '__main__':
27 | main()
28 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/custom_data_type.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseModel
2 |
3 |
4 | class MyData:
5 |
6 | def __init__(self, s):
7 | self.s = s
8 |
9 |
10 | def __str__(self):
11 | return f''
12 |
13 | @classmethod
14 | def get_validators(cls):
15 | yield cls.myvalidator
16 |
17 | @classmethod
18 | def myvalidator(cls, v):
19 | return cls(v)
20 |
21 |
22 | class MyModel(BaseModel):
23 | md: MyData
24 |
25 |
26 | def main():
27 | model = MyModel.parse_obj({
28 | "md": "hello"
29 | })
30 | print(model.md)
31 |
32 |
33 | if __name__ == '__main__':
34 | main()
35 |
--------------------------------------------------------------------------------
/builtin_packages/collections_snippets/use_ordered_dict_remove_duplicate.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from collections import OrderedDict
3 |
4 |
5 | def remove_duplicate1():
6 | print("=== remove_duplicate1 ===")
7 | ids = [1, 3, 4, 1, 2, 3]
8 | print(list(OrderedDict.fromkeys(ids)))
9 |
10 |
11 | def remove_duplicate2():
12 | print("=== remove_duplicate2 ===")
13 |
14 | od = OrderedDict([(1, 'a'), (2, 'b'), (1, 'a')])
15 | print([v for _, v in od.items()])
16 | # return list(OrderedDict.fromkeys(items))
17 |
18 |
19 | def main():
20 | remove_duplicate1()
21 | remove_duplicate2()
22 |
23 |
24 | if __name__ == '__main__':
25 | main()
26 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # code-snippets-python
2 |
3 | ## 为什么要收集这些代码
4 |
5 | 下次用起来方便。
6 |
7 | ## 环境配置
8 |
9 | ### 安装 pipenv
10 |
11 | ```shell
12 | $ pip install pipenv
13 | # or
14 | $ brew install pipenv
15 | ```
16 |
17 | ### 配置(可选)
18 |
19 | ```shell
20 | # 自动补全
21 | $ eval "$(pipenv --completion)"
22 |
23 | # macOS 可能要在 profile 里导出 local 变量
24 | $ export LC_ALL=en_US.UTF-8
25 | $ export LANG=en_US.UTF-8
26 | ```
27 |
28 | ### 创建和启动虚拟环境
29 |
30 | ```shell
31 | $ git clone git@github.com:binderclip/code-snippets-python.git
32 | $ cd code-snippets-python
33 | $ pipenv install
34 | $ pipenv shell
35 | ```
36 |
37 | ### 之后更新虚拟环境
38 |
39 | ```shell
40 | $ pipenv install xxx
41 | ```
42 |
--------------------------------------------------------------------------------
/prjs/023-socket-http-client/post.py:
--------------------------------------------------------------------------------
1 | import socket
2 |
3 |
4 | def main():
5 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6 |
7 | s.connect(("httpbin.org", 80))
8 | print("== after connect")
9 | s.sendall(b'POST /post HTTP/1.1\r\nHost: httpbin.org\r\n\r\n{"foo": "bar"}')
10 | # b'HTTP/1.1 503 Service Unavailable.\r\nContent-length:0\r\n\r\n'
11 | # 如果不设置 Content-length 的话似乎 post 就不能携带内容了,server 可能会认为没有 body,但是又来了 body 就会出错
12 | print("== after sendall")
13 | s.shutdown(1)
14 | print("== after shutdown")
15 | data = s.recv(4096)
16 | print(data)
17 | s.close()
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/packages/pyjwt_sp/pyjwt_sp.py:
--------------------------------------------------------------------------------
1 | import jwt
2 | from jwt import DecodeError
3 |
4 |
5 | def main():
6 | encoded_jwt = jwt.encode({'some': 'word'}, 'secret', algorithm='HS256')
7 | print(encoded_jwt)
8 |
9 | decoded_jwt = jwt.decode(encoded_jwt, 'secret', algorithms=['HS256'])
10 | print(decoded_jwt)
11 |
12 | decoded_jwt_not_verify = jwt.decode(encoded_jwt, verify=False)
13 | print(decoded_jwt_not_verify)
14 |
15 | try:
16 | decoded_jwt = jwt.decode('xxxxxxxxxx', 'secret', algorithms=['HS256'])
17 | print(decoded_jwt)
18 | except DecodeError as e:
19 | print(type(e), e)
20 |
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/builtin_packages/re_sp/group_search.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import re
3 |
4 |
5 | def group_search():
6 | print('===== group_search =====')
7 | s = '"Foo" and "bar" are on the way.'
8 | m = re.search(r'"(.*)" and "(.*)" are on the way\.', s)
9 | if m:
10 | print('{} ||| {} {}'.format(m.group(0), m.group(1), m.group(2)))
11 | else:
12 | print('not found')
13 | s = '"张三"和"李四"在路上。'
14 | m = re.search(r'"(.*)"和"(.*)"在路上。', s)
15 | if m:
16 | print('{} {}'.format(m.group(1), m.group(2)))
17 | else:
18 | print('not found')
19 |
20 |
21 | def main():
22 | group_search()
23 |
24 | if __name__ == '__main__':
25 | main()
26 |
--------------------------------------------------------------------------------
/packages/marshmallow_sp/marshmallow_demo.py:
--------------------------------------------------------------------------------
1 | import datetime
2 | from marshmallow import Schema, fields
3 |
4 |
5 | class ArtistSchema(Schema):
6 | name = fields.Str()
7 |
8 |
9 | class AlbumSchema(Schema):
10 | title = fields.Str()
11 | release_date = fields.Date()
12 | artist = fields.Nested(ArtistSchema())
13 |
14 |
15 | def main():
16 | bowie = dict(name='David Bowie')
17 | album = dict(artist=bowie, title='Hunky Dory', release_date=datetime.date(1971, 12, 17))
18 |
19 | schema = AlbumSchema()
20 | result = schema.dump(album)
21 | print(result)
22 | # pprint(result.data, indent=2)
23 |
24 |
25 | if __name__ == '__main__':
26 | main()
27 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/setting_sp.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseSettings
2 |
3 |
4 | class Settings(BaseSettings):
5 | redis_host = 'localhost' # set os env APP_REDIS_HOST to overwrite
6 | redis_port = 6379
7 | redis_database = 0
8 | debug = False # 'true' and '1' are True, 'false' and '0' are False
9 |
10 | class Config:
11 | env_prefix = 'APP_'
12 |
13 |
14 | def main():
15 | settings = Settings()
16 | print(settings.redis_host)
17 | print(settings.redis_port, type(settings.redis_port))
18 | print(settings.debug)
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
24 |
25 | # https://pydantic-docs.helpmanual.io/#settings
26 |
--------------------------------------------------------------------------------
/packages/requests_sp/resp.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def main():
5 | r = requests.get('https://httpbin.org/get')
6 | print(r.content) # """Content of the response, in bytes."""
7 | print(r.text) # """Content of the response, in unicode...."""
8 | print(r.json()) # """Returns the json-encoded content of a response, if any....
9 | # raises ValueError: If the response body does not contain valid json."""
10 |
11 | r = requests.get('https://httpbin.org/')
12 | try:
13 | print(r.json())
14 | except ValueError as e:
15 | print(type(e))
16 | print(e)
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/builtin_packages/enum_sp/hello_enum.py:
--------------------------------------------------------------------------------
1 | from enum import Enum, IntEnum
2 |
3 |
4 | class Color(Enum):
5 | RED = 1
6 | GREEN = 2
7 | BLUE = 3
8 |
9 |
10 | class Color2(IntEnum):
11 | RED = 1
12 | GREEN = 2
13 | BLUE = 3
14 |
15 |
16 | def main():
17 | c_r = Color.RED
18 | print(c_r)
19 | print(c_r.name)
20 | print(c_r.value)
21 | # print(int(c_r)) # TypeError: int() argument must be a string, a bytes-like object or a number, not 'Color'
22 |
23 | c_r = Color2.RED
24 | print(c_r)
25 | print(c_r.name)
26 | print(c_r.value)
27 | print(int(c_r))
28 | print(c_r.__dict__)
29 |
30 |
31 | if __name__ == '__main__':
32 | main()
33 |
--------------------------------------------------------------------------------
/builtin_packages/collections_snippets/hello_namedtuple.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from collections import namedtuple
3 | User = namedtuple('User', ['name', 'sex', 'age'])
4 |
5 |
6 | def main():
7 | u1 = User(name='foo', sex='male', age=21)
8 | u2 = User._make(['bar', 'female', 22])
9 | print(u1)
10 | print(u1.name, u1.sex, u1.age)
11 | for t in u1:
12 | print(t)
13 | print(u2)
14 | u1._replace(age=22)
15 | print(u1)
16 | print(u1._asdict())
17 | u3 = User(**{
18 | 'name': 'baz',
19 | 'sex': 'male',
20 | 'age': 23,
21 | })
22 | print(u3)
23 | print(dict(u3._asdict()))
24 |
25 |
26 | if __name__ == '__main__':
27 | main()
28 |
--------------------------------------------------------------------------------
/packages/socket_sp/socket_server_sp.py:
--------------------------------------------------------------------------------
1 | import socket
2 |
3 |
4 | def main():
5 | HOST = '127.0.0.1' # 标准的回环地址 (localhost)
6 | PORT = 65432 # 监听的端口 (非系统级的端口: 大于 1023)
7 |
8 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 | s.bind((HOST, PORT))
10 | s.listen()
11 | conn, addr = s.accept()
12 | with conn:
13 | # 用了 context manager 之后不用再手动关闭
14 | print('Connected by', addr)
15 | while True:
16 | data = conn.recv(1024)
17 | if not data:
18 | break # 返回空对象时结束
19 | conn.sendall(data)
20 |
21 |
22 | if __name__ == '__main__':
23 | main()
24 |
--------------------------------------------------------------------------------
/packages/typing_snippets/type_var_sp.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from typing import TypeVar, Sequence
3 |
4 |
5 | T = TypeVar('T')
6 | A = TypeVar('A', str, bytes)
7 |
8 |
9 | def repeat(x: T, n: int) -> Sequence[T]:
10 | """Return a list containing n references to x."""
11 | return [x]*n
12 |
13 |
14 | def longest(x: A, y: A) -> A:
15 | """Return the longest of two strings."""
16 | print(len(x), len(y))
17 | return x if len(x) >= len(y) else y
18 |
19 |
20 | def main():
21 | print(repeat('ha', 20))
22 | print(longest('big watermelon', '大西瓜🍉'))
23 | print(longest('big watermelon'.encode('utf-8'), '大西瓜🍉'.encode('utf-8')))
24 |
25 | if __name__ == '__main__':
26 | main()
27 |
--------------------------------------------------------------------------------
/basic/class_sp/enum_class_demo.py:
--------------------------------------------------------------------------------
1 | class MyType(object):
2 | '''我的类别
3 | x: x 类型
4 | y: y 类型
5 | '''
6 | x = 1
7 | y = 2
8 |
9 | type_text_map = {
10 | x: "x_type",
11 | y: "y_type"
12 | }
13 |
14 | text_type_map = {
15 | "x_type": x,
16 | "y_type": y
17 | }
18 |
19 | @classmethod
20 | def get_text(cls, type_):
21 | return cls.type_text_map[type_]
22 |
23 | @classmethod
24 | def get_type(cls, text):
25 | return cls.text_type_map[text]
26 |
27 |
28 | def main():
29 | x = 'x_type'
30 | print(MyType.get_type(x))
31 | print(MyType.get_text(MyType.x))
32 |
33 |
34 | if __name__ == '__main__':
35 | main()
36 |
--------------------------------------------------------------------------------
/packages/requests_sp/download_and_upload_img.py:
--------------------------------------------------------------------------------
1 | import shutil
2 | import requests
3 | import io
4 |
5 |
6 | def main():
7 | url_down = 'https://qn.cdn.cliiip.com/imgs/u/4c3f70fd-f1c4-4232-a6ff-9228ce164fc5.png'
8 | url_up = 'https://httpbin.org/post'
9 |
10 | r = requests.get(url_down, stream=True)
11 | if r.status_code == 200:
12 | with io.BytesIO() as f:
13 | r.raw.decode_content = True # decode based on content-encoding
14 | print(type(r.raw))
15 | shutil.copyfileobj(r.raw, f)
16 | f.seek(0)
17 | r = requests.post(url_up, files=[('image', f)])
18 | print(r.json())
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/packages/threading_snippets/thread_and_queue.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from Queue import Queue
3 | from threading import Thread
4 |
5 | worker_size = 10
6 | queue_size = 10
7 |
8 | q = Queue(maxsize=queue_size)
9 |
10 |
11 | def worker():
12 | while True:
13 | item = q.get()
14 | print(item)
15 | q.task_done()
16 |
17 |
18 | def main():
19 |
20 | for i in range(worker_size):
21 | t = Thread(target=worker)
22 | t.daemon = True
23 | t.start()
24 |
25 | print('=== start work ===')
26 | for i in xrange(100):
27 | q.put(i)
28 | q.join() # block until all tasks are done
29 | print('=== end ===')
30 |
31 |
32 | if __name__ == '__main__':
33 | main()
34 |
--------------------------------------------------------------------------------
/packages/geetest_sp/geetest_sp.py:
--------------------------------------------------------------------------------
1 | from geetest import GeetestLib
2 |
3 |
4 | GEETEST_ID = 'e285deeeeeeeeeeeeeeeeeeeeeeeeeee'
5 | GEETEST_KEY = '16f7317eeeeeeeeeeeeeeeeeeeeeeeee'
6 |
7 |
8 | def get_status_and_response_str(gt, user_id):
9 | return gt.pre_process(user_id), gt.get_response_str()
10 | # {"success": 1, "gt": "e285deeeeeeeeeeeeeeeeeeeeeeeeeee", "challenge": "2cd858e8243972a277a715dd7a45e82a"}
11 | # {"success": 0, "gt": "e285deeeeeeeeeeeeeeeeeeeeeeeeeee", "challenge": "70efdf2ec9b086079795c442636b55fb1f"}
12 |
13 |
14 | def main():
15 | gt = GeetestLib(GEETEST_ID, GEETEST_KEY)
16 | user_id = 1
17 | get_status_and_response_str(gt, user_id)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
--------------------------------------------------------------------------------
/prjs/022-http-chunked-exp-with-flask/app.py:
--------------------------------------------------------------------------------
1 | import time
2 | from flask import Flask, escape, request, Response
3 |
4 | app = Flask(__name__)
5 |
6 |
7 | @app.route('/')
8 | def hello():
9 | name = request.args.get("name", "World")
10 | return f'Hello, {escape(name)}!'
11 |
12 |
13 | @app.route('/stream.csv')
14 | def generate_large_csv():
15 | def generate():
16 | print("== generate start ==")
17 | for i in range(10000):
18 | yield f'{i}, foo\n'
19 | time.sleep(0.0005)
20 | print("== generate done ==")
21 | return Response(generate(), mimetype='text/csv')
22 |
23 |
24 | def main():
25 | app.run(port=8889)
26 |
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
--------------------------------------------------------------------------------
/utils/012-yes-or-no/app.py:
--------------------------------------------------------------------------------
1 | def yes_or_no(prompt, true_value='y', false_value='n', default=True):
2 | default_value = true_value if default else false_value
3 | prompt = '%s? %s/%s [%s]: ' % (prompt, true_value, false_value, default_value)
4 | i = input(prompt)
5 | if not i:
6 | return default
7 | while True:
8 | if i == true_value:
9 | return True
10 | elif i == false_value:
11 | return False
12 | prompt = 'Please input %s or %s: ' % (true_value, false_value)
13 | i = input(prompt)
14 | # replace input with raw_input for Python 2
15 |
16 |
17 | def main():
18 | print(yes_or_no('Are you OK'))
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/builtin_packages/collections_snippets/hello_defaultdict.py:
--------------------------------------------------------------------------------
1 | from collections import defaultdict
2 |
3 |
4 | def main():
5 | print(int(), int())
6 | dd = defaultdict(int)
7 | print(dd['a'], dd['b'])
8 |
9 | print(str(), str())
10 | dd = defaultdict(str, {'a': 'aa'})
11 | print(dd['a'], dd['b'])
12 |
13 | print(bool(), bool())
14 | dd = defaultdict(bool)
15 | print(dd['a'], dd['b'])
16 |
17 | dd = defaultdict(lambda: 3)
18 | print(dd['a'], dd['b'])
19 |
20 | dd = defaultdict(lambda: [])
21 | print(dd['a'], dd['b'])
22 |
23 | dd = defaultdict(lambda: defaultdict(list))
24 | print(dd['a'], dd['b'], dd['a']['b'], dd['c']['d'])
25 |
26 |
27 | if __name__ == '__main__':
28 | main()
29 |
--------------------------------------------------------------------------------
/packages/string_io_snippets/hello_string_io.py:
--------------------------------------------------------------------------------
1 | import io
2 |
3 |
4 | def use_as_file():
5 | print('=== use_as_file ===')
6 | text = 'hello world!'
7 | f = io.StringIO()
8 | f.write(text)
9 | f.seek(0)
10 | print(f)
11 | # print(f.name) # AttributeError: '_io.StringIO' object has no attribute 'name'
12 | f.name = 'hello.txt'
13 | print(f)
14 | print(f.name)
15 | print(f.read())
16 | f.close()
17 |
18 |
19 | def use_as_stream():
20 | print('=== use_as_stream ===')
21 | f = io.StringIO("some initial text data")
22 | print(f.read())
23 | f.close()
24 |
25 |
26 | def main():
27 | use_as_file()
28 | use_as_stream()
29 |
30 |
31 | if __name__ == '__main__':
32 | main()
33 |
--------------------------------------------------------------------------------
/basic/i__slots__.py:
--------------------------------------------------------------------------------
1 | import sys
2 |
3 |
4 | class Image1(object):
5 | pass
6 |
7 |
8 | class Image2(object):
9 | __slots__ = ['id', 'caption', 'url']
10 |
11 |
12 | def set_obj(obj):
13 | obj.id = 1
14 | obj.caption = 'capppppppppp'
15 | obj.url = 'uuuuuuuuuuuuuuu'
16 |
17 |
18 | def main():
19 | img1 = Image1()
20 | img2 = Image2()
21 |
22 | print(sys.getsizeof(img1))
23 | print(sys.getsizeof(img2))
24 |
25 | set_obj(img1)
26 | set_obj(img2)
27 |
28 | print(img1.__dict__)
29 | # print(img2.__dict__) AttributeError: 'Image2' object has no attribute '__dict__'
30 |
31 | print(sys.getsizeof(img1))
32 | print(sys.getsizeof(img2))
33 |
34 |
35 | if __name__ == '__main__':
36 | main()
37 |
--------------------------------------------------------------------------------
/packages/dataclasses_sp/field_sp.py:
--------------------------------------------------------------------------------
1 | from dataclasses import dataclass, fields, field
2 |
3 |
4 | @dataclass
5 | class C(object):
6 | # basic
7 | field_a: str
8 |
9 |
10 | @dataclass
11 | class C1(object):
12 | # with default
13 | field_a: str = field(default='')
14 |
15 |
16 | @dataclass
17 | class C2(object):
18 | field_a: str
19 | field_b: int
20 |
21 | @classmethod
22 | def get_fields(cls):
23 | return fields(cls)
24 |
25 |
26 | class Foo(object):
27 | def __init__(self, foo):
28 | self.foo = foo
29 |
30 |
31 | def main():
32 | # get all fields
33 | print(fields(C))
34 | print(C2.get_fields())
35 | print(C2.__dict__)
36 |
37 |
38 | if __name__ == '__main__':
39 | main()
40 |
--------------------------------------------------------------------------------
/prjs/006-make-1-million-requests-with-python-aiohttp/s2_requests.py:
--------------------------------------------------------------------------------
1 | import requests
2 | import time
3 |
4 |
5 | def fetch(i, session):
6 | url = f"http://httpbin.org/anything/{i}"
7 | # url = f"https://www.baidu.com/s?wd={i}"
8 | return session.get(url)
9 |
10 |
11 | def main():
12 | print('=== start ===')
13 |
14 | t_start = time.time()
15 |
16 | texts = []
17 | s = requests.Session()
18 | for i in range(30):
19 | texts.append(fetch(i, s).text)
20 |
21 | t_end = time.time()
22 | print('time: {}'.format(t_end - t_start))
23 |
24 | # for text in texts:
25 | # print(text)
26 | print(f'{len(texts)}')
27 | print('=== done ===')
28 |
29 |
30 | if __name__ == '__main__':
31 | main()
32 |
--------------------------------------------------------------------------------
/packages/geetest_sp/readme.md:
--------------------------------------------------------------------------------
1 | # geetest
2 |
3 | ## 验证流程
4 |
5 | - 用 id 和 key 初始化 geetest lib
6 | - 发送 user_id 获得 status(geetest 是否宕机)和 challenge
7 | - status 和 user_id 写入 session,challenge 信息发给客户端
8 | - ...
9 | - 从客户端读取 challenge、validate 和 seccode,从 session 读取 status 和 user_id
10 | - 如果是正常情况用 geetest 的在线测试,geetest 宕机情况则使用普通模式
11 |
12 | ## 实际应用
13 |
14 | ### 做发帖限制
15 |
16 | - 客户端默认的请求不需要验证
17 | - 服务器中记录用户的发帖情况
18 | - 服务器接收请求之后判断是否超过限制,如果超过且没有 geetest 信息则报错
19 | - 客户端接收到错误之后提示验证
20 | - 验证后重新提交请求,验证通过则提交,不通过则返回错误
21 |
22 | ## refs
23 |
24 | - [geetest-start](https://docs.geetest.com/install/overview/start/)
25 | - [geetest-guide](https://docs.geetest.com/install/overview/guide)
26 | - [geetest-flowchart](https://docs.geetest.com/install/overview/flowchart)
27 |
--------------------------------------------------------------------------------
/prjs/006-make-1-million-requests-with-python-aiohttp/s3_requests.py:
--------------------------------------------------------------------------------
1 | import requests
2 | import time
3 |
4 |
5 | def fetch(i, session):
6 | # url = f"http://localhost:8080/{i}"
7 | url = f"http://httpbin.org/anything/{i}"
8 | return session.get(url)
9 |
10 |
11 | def main():
12 | print('=== start ===')
13 | n = 100
14 | t_start = time.time()
15 |
16 | texts = []
17 | s = requests.Session()
18 | for i in range(n):
19 | texts.append(fetch(i, s).text)
20 |
21 | t_end = time.time()
22 | print('time: {}'.format(t_end - t_start))
23 |
24 | # for text in texts:
25 | # print(text)
26 | print(f'{len(texts)}')
27 | print('=== done ===')
28 |
29 |
30 | if __name__ == '__main__':
31 | main()
32 |
--------------------------------------------------------------------------------
/packages/requests_sp/timeout_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import requests
3 | from requests.exceptions import Timeout
4 |
5 |
6 | def main():
7 | try:
8 | r = requests.get('http://httpbin.org/delay/1', timeout=(3, 5)) # ConnectTimeout = 3, ReadTimeout = 5
9 | # r = requests.get('http://httpbin.org/delay/1', timeout=5) # ConnectTimeout = ReadTimeout = 5
10 | print(r.status_code)
11 | print int(r.elapsed.total_seconds() * 1000) # ms
12 | except Timeout:
13 | # http://docs.python-requests.org/en/master/_modules/requests/exceptions/
14 | # ConnectTimeout(ConnectionError, Timeout)
15 | # ReadTimeout(Timeout)
16 | print("timeout")
17 |
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/packages/struct_sp/hello_struct.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | from struct import pack, unpack
3 | from base64 import urlsafe_b64encode, urlsafe_b64decode
4 |
5 |
6 | def main():
7 | print(urlsafe_b64encode(pack('L', 123)))
8 | print(urlsafe_b64encode(pack('L', 1234567)))
9 | print(urlsafe_b64encode(pack('L', 1234567890)))
10 | print(urlsafe_b64encode(pack('L', 1234567890123)))
11 | print(urlsafe_b64encode(pack('L', 12345678901234567)))
12 | print(urlsafe_b64encode(pack('L', 130000000000000000)))
13 | print(urlsafe_b64encode(pack('L', 990000000000000000)))
14 | print(unpack('L', urlsafe_b64decode('AADNrE_azQE='))[0])
15 | print(unpack('L', urlsafe_b64decode('AACjN8EvvQ0='))[0])
16 |
17 |
18 | if __name__ == '__main__':
19 | main()
20 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/before_after_request_sp/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, request
2 |
3 | from frameworks.flask_snippets.before_after_request_sp.bp import bp
4 |
5 | app = Flask(__name__)
6 | app.register_blueprint(bp, url_prefix='/bp')
7 |
8 |
9 | @app.before_request
10 | def before_request():
11 | print('>>> before_request')
12 | print(request.method)
13 | print(type(request.method))
14 |
15 |
16 | @app.after_request
17 | def after_request(resp):
18 | print('>>> after_request')
19 | print(resp)
20 | return resp
21 |
22 |
23 | @app.route("/", methods=['GET', 'POST'])
24 | def index():
25 | return "Hello World!"
26 |
27 |
28 | def main():
29 | app.run(debug=True)
30 |
31 |
32 | if __name__ == '__main__':
33 | main()
34 |
--------------------------------------------------------------------------------
/packages/dataclasses_sp/poly_field_sp.py:
--------------------------------------------------------------------------------
1 | from dataclasses import dataclass, asdict
2 |
3 |
4 | @dataclass
5 | class SimpleDataObject(object):
6 | field_x: str
7 |
8 |
9 | @dataclass
10 | class SimpleDataObjectA(SimpleDataObject):
11 | field_a: int
12 |
13 |
14 | @dataclass
15 | class SimpleDataObjectB(SimpleDataObject):
16 | field_b: str
17 |
18 |
19 | @dataclass
20 | class DataObject(object):
21 | field_c: int
22 | s_obj: SimpleDataObject
23 |
24 |
25 | def main():
26 | d2 = {'field_c': 100, 's_obj': SimpleDataObject(**{'field_a': 1, 'field_b': 'aaa'})}
27 | d2 = {'field_c': 100, 's_obj': {'field_a': 1, 'field_b': 'aaa'}}
28 | obj2 = DataObject(**d2)
29 | print(obj2)
30 |
31 |
32 | if __name__ == '__main__':
33 | main()
34 |
--------------------------------------------------------------------------------
/packages/peewee_sp/model_and_dict.py:
--------------------------------------------------------------------------------
1 | from playhouse.shortcuts import model_to_dict, dict_to_model, update_model_from_dict
2 |
3 | from queries import Foo
4 |
5 |
6 | def main():
7 | foo = Foo.get(id=1)
8 | print(foo)
9 | print(model_to_dict(foo))
10 | print(model_to_dict(foo, exclude=[Foo.type]))
11 | print(model_to_dict(foo, extra_attrs=['upper_name']))
12 |
13 | d = {
14 | 'id': 1,
15 | 'name': 'n1',
16 | 'type': 1,
17 | }
18 | foo = dict_to_model(Foo, d)
19 | print(foo)
20 |
21 | d['type'] = 2
22 | update_model_from_dict(foo, d)
23 | print(foo)
24 |
25 | d['id'] = 2
26 | update_model_from_dict(foo, d) # id 同样会改掉
27 | print(foo)
28 |
29 |
30 | if __name__ == '__main__':
31 | main()
32 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/recursive_models.py:
--------------------------------------------------------------------------------
1 | from typing import List
2 | from pydantic import BaseModel
3 |
4 |
5 | class Foo(BaseModel):
6 | count: int = ...
7 | size: float = None
8 |
9 |
10 | class Bar(BaseModel):
11 | apple = 'x'
12 | banana = 'y'
13 |
14 |
15 | class Spam(BaseModel):
16 | foo: Foo = ...
17 | bars: List[Bar] = ...
18 |
19 |
20 | def main():
21 | m = Spam(foo={'count': 4}, bars=[{'apple': 'x1'}, {'apple': 'x2'}])
22 | print(m)
23 | # > Spam foo= bars=[, ]
24 | print(m.dict())
25 | # {'foo': {'count': 4, 'size': None}, 'bars': [{'apple': 'x1', 'banana': 'y'}, {'apple': 'x2', 'banana': 'y'}]}
26 |
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
--------------------------------------------------------------------------------
/prjs/026-json2csv/app.py:
--------------------------------------------------------------------------------
1 | import json
2 | import csv
3 |
4 |
5 | def load_json(name):
6 | with open(name) as f:
7 | s = json.load(f)
8 | return s
9 |
10 |
11 | def ds_2_rows(d1, d2, d3):
12 | rows = [(k, v, d2.get(k, ""), d3.get(k, "")) for (k, v) in d1.items()]
13 | return [('key', 'zh', 'en', 'jp')] + rows
14 |
15 |
16 | def dump_csv(rows):
17 | with open('out.csv', 'w', newline='') as f:
18 | writer = csv.writer(f, dialect='excel')
19 | writer.writerows(rows)
20 |
21 |
22 | def main():
23 | dz = load_json("zh-CN.json")
24 | de = load_json("en-US.json")
25 | dj = load_json("ja-JP.json")
26 | rows = ds_2_rows(dz, de, dj)
27 | # print(rows)
28 | dump_csv(rows)
29 |
30 |
31 | if __name__ == '__main__':
32 | main()
33 |
--------------------------------------------------------------------------------
/Pipfile:
--------------------------------------------------------------------------------
1 | [[source]]
2 | url = "https://pypi.python.org/simple"
3 | verify_ssl = true
4 | name = "pypi"
5 |
6 | [packages]
7 | flask = "*"
8 | xmltodict = "*"
9 | python-telegram-bot = "*"
10 | "beautifulsoup4" = "*"
11 | mako = "*"
12 | flask-mako = "*"
13 | wtforms = "*"
14 | aiohttp = "*"
15 | peewee = "*"
16 | pymysql = "*"
17 | requests = "*"
18 | ipython = "*"
19 | fire = "*"
20 | dataclasses = "*"
21 | schematics = "*"
22 | pydantic = "*"
23 | redis = "*"
24 | pendulum = "*"
25 | mypy = "*"
26 | mypy-extensions = "*"
27 | pillow = "*"
28 | pyjwt = "*"
29 | geetest = "*"
30 | profilehooks = "*"
31 | retrying = "*"
32 | jieba = "*"
33 | tqdm = "*"
34 | packaging = "*"
35 | pypinyin = "*"
36 | xerox = "*"
37 |
38 | [dev-packages]
39 | pytest = "*"
40 |
41 | [requires]
42 | python_version = "3.7"
43 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/datetime_and_timestamp.py:
--------------------------------------------------------------------------------
1 | import time
2 | import datetime
3 |
4 |
5 | def dt2ts():
6 | print("=== dt2ts ===")
7 | dt = datetime.datetime.now()
8 | print(dt.timestamp()) # python 2 中没有这个方法
9 | print(time.time())
10 |
11 | d = datetime.datetime.now().date()
12 | print(d)
13 | print(datetime.datetime.combine(d, datetime.time()).timestamp())
14 | print(time.mktime(d.timetuple()))
15 |
16 |
17 | def ts2dt():
18 | print("=== ts2dt ===")
19 | ts = 1575266499
20 | dt = datetime.datetime.fromtimestamp(ts)
21 | print(dt)
22 |
23 |
24 | def main():
25 | dt2ts()
26 | ts2dt()
27 |
28 |
29 | if __name__ == '__main__':
30 | main()
31 |
32 | # https://www.programiz.com/python-programming/datetime/timestamp-datetime
33 |
--------------------------------------------------------------------------------
/packages/pendulum_sp/parse_sp.py:
--------------------------------------------------------------------------------
1 | import pendulum
2 |
3 |
4 | def main():
5 | dts = '2018-04-13 16:06:11.844265+08:00'
6 | dt = pendulum.parse(dts)
7 | print(dt)
8 |
9 | # without timezone
10 | dts = '2018-04-13 17:59:15'
11 | dt = pendulum.parse(dts)
12 | print(dt)
13 |
14 | # parse with timezone
15 | tz_shanghai = pendulum.timezone('Asia/Shanghai')
16 |
17 | dts = '2018-04-13 17:59:15'
18 | # dt = pendulum.parse(dts, tz='Asia/Shanghai')
19 | dt = pendulum.parse(dts, tz=tz_shanghai)
20 | print(dt)
21 | print(dt.in_tz(tz_shanghai))
22 |
23 |
24 | dts = '2018-04-13 17:59:15+00:00'
25 | dt = pendulum.parse(dts, tz='Asia/Shanghai')
26 | print(dt)
27 | print(dt.in_tz(tz_shanghai))
28 |
29 |
30 | if __name__ == '__main__':
31 | main()
32 |
--------------------------------------------------------------------------------
/basic/attribute_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | class A(object):
5 | def __init__(self, a):
6 | self.a = a
7 |
8 | def f1(self):
9 | print('hello f1')
10 |
11 |
12 | def main():
13 | a1 = A('aaa')
14 | print(A.__dict__)
15 | print(A.__dict__['f1'])
16 | print(hasattr(a1, 'f1'))
17 | print(hasattr(a1, 'f2'))
18 | a1.f1()
19 | # a1.f2() # AttributeError: 'A' object has no attribute 'f2'
20 |
21 | d = {'b': 'bb'}
22 | # print(d.__dict__) # AttributeError: 'dict' object has no attribute '__dict__'
23 | print(hasattr(d, 'b')) # False
24 | print('b' in d) # True
25 |
26 |
27 | if __name__ == '__main__':
28 | main()
29 |
30 |
31 | # https://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python
32 |
--------------------------------------------------------------------------------
/basic/decorator_sp/retry_with_decorator.py:
--------------------------------------------------------------------------------
1 | import requests
2 |
3 |
4 | def retries(times=3):
5 | def wrap(func):
6 | def r(*args, **kw):
7 | t = 0
8 | while True:
9 | try:
10 | return func(*args, **kw)
11 | except requests.exceptions.ConnectTimeout as e:
12 | t += 1
13 | if t < times:
14 | print('retry {}'.format(t))
15 | else:
16 | raise e
17 | return r
18 | return wrap
19 |
20 |
21 | @retries()
22 | def make_a_get():
23 | return requests.get('http://httpbin.org/get', timeout=(0.3, 2))
24 |
25 |
26 | def main():
27 | print(make_a_get())
28 |
29 |
30 | if __name__ == '__main__':
31 | main()
32 |
--------------------------------------------------------------------------------
/packages/coroutine_sp/yield_sample.py:
--------------------------------------------------------------------------------
1 |
2 | def printer():
3 | print('>>> printer start')
4 | counter = 0
5 | while True:
6 | print('>>> in while')
7 | string = yield
8 | print('[{0}] {1}'.format(counter, string))
9 | counter += 1
10 |
11 |
12 | def test_printer():
13 | p = printer()
14 | print(">>> let's next")
15 | next(p)
16 | # 如果不 next 就直接 send 的话 TypeError: can't send non-None value to a just-started generator
17 | print(">>> send hi")
18 | p.send('Hi')
19 | print(">>> send my...")
20 | p.send('My name is hsfzxjy.')
21 | print(">>> send bye")
22 | p.send('Bye')
23 | print(">>> send bye!")
24 | p.send('bye!')
25 |
26 |
27 | def main():
28 | test_printer()
29 |
30 |
31 | if __name__ == '__main__':
32 | main()
33 |
--------------------------------------------------------------------------------
/packages/dataclasses_sp/load_sp.py:
--------------------------------------------------------------------------------
1 | from dataclasses import dataclass, fields
2 |
3 |
4 | @dataclass
5 | class XDataClass(object):
6 | """dataclass can load dict or obj"""
7 |
8 | @classmethod
9 | def load(cls, data):
10 | """load a dict/obj to dataclass
11 |
12 | auto load dataclass fields recursively
13 | auto grab needed obj attrs
14 | """
15 | if isinstance(data, dict):
16 | print('>>> dict')
17 | else:
18 | print('>>> obj')
19 | # cls_fields = fields(cls)
20 | init()
21 |
22 |
23 | # @classmethod
24 | # def _load_inner(cls, ):
25 |
26 |
27 | def main():
28 | print(XDataClass.load({'foo': 'bar'}))
29 | print(XDataClass.load(Foo('bar')))
30 |
31 |
32 | if __name__ == '__main__':
33 | main()
34 |
--------------------------------------------------------------------------------
/packages/filter_snippets/filter_performance.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import time
3 |
4 |
5 | def timeit(method):
6 |
7 | def timed(*args, **kw):
8 | ts = time.time()
9 | result = method(*args, **kw)
10 | te = time.time()
11 |
12 | print '%r (%r, %r) %2.2f sec' % \
13 | (method.__name__, args, kw, te - ts)
14 | return result
15 |
16 | return timed
17 |
18 |
19 | @timeit
20 | def f1():
21 | for i in range(100):
22 | l1 = range(100000)
23 | l2 = filter(lambda x: x % 2 == 0, l1)
24 |
25 |
26 | @timeit
27 | def f2():
28 | for i in range(100):
29 | l1 = range(100000)
30 | l2 = [x for x in l1 if x % 2 == 0]
31 |
32 |
33 | def main():
34 | f1()
35 | f2()
36 |
37 |
38 | if __name__ == '__main__':
39 | main()
40 |
--------------------------------------------------------------------------------
/basic/iter_sp/iter_sp.py:
--------------------------------------------------------------------------------
1 | def func(i: int):
2 | for i in range(i, i + 2):
3 | yield i * i
4 |
5 |
6 | def iter_print(it):
7 | print(f'>>> {it}:')
8 | for i in it:
9 | print(i)
10 |
11 |
12 | def main():
13 | l = ['foo', 'bar']
14 | s = {'a', 'b'}
15 | r = range(3)
16 | f = func(2)
17 | print(l, s, r, f)
18 | print(iter(l), iter(s), iter(r), iter(f))
19 |
20 | for x in (l, s, r, f):
21 | iter_print(x)
22 |
23 | print(iter(iter(l)))
24 | print(iter(iter(iter(l))))
25 |
26 | # raise some exceptions
27 | # iter(1) # TypeError: 'int' object is not iterable
28 | # list(1) # TypeError: 'int' object is not iterable
29 |
30 |
31 | if __name__ == '__main__':
32 | main()
33 |
34 | # https://docs.python.org/3/tutorial/classes.html#iterators
35 |
--------------------------------------------------------------------------------
/packages/peewee_sp/exceptions_sp.py:
--------------------------------------------------------------------------------
1 | from peewee import *
2 | from packages.peewee_sp.mysql_db import db
3 |
4 |
5 | class BaseModel(Model):
6 | class Meta:
7 | database = db
8 |
9 |
10 | class EModel(BaseModel):
11 | my_char = CharField(max_length=10)
12 |
13 |
14 | def create_table():
15 | db.create_tables([EModel])
16 |
17 |
18 | def drop_table():
19 | db.create_tables([EModel])
20 |
21 |
22 | def does_not_exist():
23 | EModel.get_by_id(11111) # DoesNotExist
24 |
25 |
26 | def data_error():
27 | EModel.create(my_char='a' * 11) # peewee.DataError: (1406, "Data too long for column 'my_char' at row 1")
28 |
29 |
30 | def main():
31 | # create_table()
32 | # does_not_exist()
33 | data_error()
34 | # drop_table()
35 |
36 |
37 | if __name__ == '__main__':
38 | main()
39 |
--------------------------------------------------------------------------------
/packages/requests_sp/save_img.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import shutil
3 | import requests
4 |
5 |
6 | def save_img(path, url):
7 | print(path)
8 |
9 | r = requests.get(url, stream=True)
10 | if r.status_code == 200:
11 | with open(path, 'wb') as f:
12 | r.raw.decode_content = True # # decode based on content-encoding
13 | print(type(r.raw))
14 | shutil.copyfileobj(r.raw, f)
15 |
16 |
17 | def main():
18 | img_path = "xxx.jpg"
19 | url = "https://qn.cdn.cliiip.com/imgs/u/4c3f70fd-f1c4-4232-a6ff-9228ce164fc5.png"
20 | save_img(img_path, url)
21 |
22 |
23 | if __name__ == '__main__':
24 | main()
25 |
26 |
27 | # stream=True: Advanced Usage — Requests 2.18.1 documentation http://docs.python-requests.org/en/master/user/advanced/#body-content-workflow
28 |
--------------------------------------------------------------------------------
/prjs/014-count-text/app.py:
--------------------------------------------------------------------------------
1 | from collections import defaultdict
2 |
3 | import csv
4 |
5 |
6 | def read_data():
7 | # f_name = 'foo.txt'
8 | f_name = 'search_log.txt'
9 | d = defaultdict(int)
10 | with open(f_name) as f:
11 | for line in f.readlines():
12 | text = line.strip()
13 | if not text:
14 | continue
15 | d[text] += 1
16 | l = list(d.items())
17 | l.sort(key=lambda x: x[1], reverse=True)
18 | return l
19 |
20 |
21 | def write_date(l):
22 | f_name = 'top_query_until_2018-09-19.csv'
23 | with open(f_name, 'w', newline='') as f:
24 | writer = csv.writer(f)
25 | writer.writerows(l)
26 |
27 |
28 | def main():
29 | l = read_data()
30 | write_date(l)
31 | print(l)
32 |
33 |
34 | if __name__ == '__main__':
35 | main()
36 |
--------------------------------------------------------------------------------
/utils/001-read-ids-from-file/app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import argparse
3 |
4 |
5 | def read_ids_from_file(ids_file):
6 | ids = []
7 | with open(ids_file) as f:
8 | for line in f.readlines():
9 | _line = line.strip()
10 | if not _line:
11 | continue
12 | ids.append(int(_line))
13 | return ids
14 |
15 |
16 | def main():
17 | parser = argparse.ArgumentParser()
18 | parser.add_argument("-i", "--ids_file", help='select ids file', required=True)
19 | args = parser.parse_args()
20 | ids_file = args.ids_file
21 | ids = read_ids_from_file(ids_file)
22 | print('{} ids'.format(len(ids)))
23 | print('{}...'.format(', '.join(map(str, ids[:10]))) if len(ids) > 10 else '{}'.format(', '.join(map(str, ids))))
24 |
25 |
26 | if __name__ == '__main__':
27 | main()
28 |
--------------------------------------------------------------------------------
/prjs/001-covert-img-to-rgb/notes.md:
--------------------------------------------------------------------------------
1 | # 把 CMYK 模式的图片转换为 RGB 模式
2 |
3 | - Finder 和预览打开 CMYK 的图片颜色正常
4 | - Chrome 中显示 CMYK 的图片颜色异常
5 | - Photoshop 转换完的效果是正常的,不过 CMYK 到 RGB 再转换回 CMYK 颜色会发生一点变化
6 | - 未设置 icc 的时候直接用 Pillow 转换图片到 RGB 的效果和浏览器中异常的效果几乎一样
7 | - 设置对应的 icc 后转换出来的是接近正常的
8 |
9 | ref:
10 |
11 | - [python - Conversion from CMYK to RGB with Pillow is different from that of Photoshop - Stack Overflow](https://stackoverflow.com/questions/38855022/conversion-from-cmyk-to-rgb-with-pillow-is-different-from-that-of-photoshop)
12 | - [Pillow — Pillow (PIL Fork) 4.4.0.dev0 documentation](http://pillow.readthedocs.io/en/latest/)
13 | - [ICC profile downloads - Mac](http://www.adobe.com/support/downloads/iccprofiles/iccprofiles_mac.html)
14 | - [sRGB Color Space Profile.ICM](https://github.com/xorgy/graphicsmagick/blob/master/profiles/sRGB%20Color%20Space%20Profile.ICM)
15 |
--------------------------------------------------------------------------------
/packages/json_sp/json_encoder_sp.py:
--------------------------------------------------------------------------------
1 | import datetime
2 | import json
3 |
4 |
5 | class MyEncoder(json.JSONEncoder):
6 | ENCODER_BY_TYPE = {
7 | datetime.datetime: lambda dt: dt.isoformat(),
8 | set: list,
9 | }
10 |
11 | def default(self, o):
12 | try:
13 | encoder = self.ENCODER_BY_TYPE[type(o)]
14 | except KeyError:
15 | return super().default(o)
16 | return encoder(o)
17 |
18 |
19 | def main():
20 | data = {
21 | "my_dt": datetime.datetime.now(),
22 | "my_set": {1, 2, 3},
23 | }
24 | print(data)
25 | print(json.dumps(data, cls=MyEncoder))
26 |
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
31 | # https://github.com/samuelcolvin/pydantic/issues/133
32 | # https://github.com/tutorcruncher/socket-server/blob/master/tcsocket/app/utils.py#L16-L35
33 |
--------------------------------------------------------------------------------
/basic/doctest_sp/doctest_sp.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 |
4 | def factorial(n):
5 | """
6 | Return the factorial of n, an exact integer >= 0.
7 |
8 | >>> factorial(1)
9 | 1
10 | >>> factorial(2)
11 | 2
12 |
13 | :param n:
14 | :return:
15 | """
16 | if not n >= 0:
17 | raise ValueError("n must be >= 0")
18 | if math.floor(n) != n:
19 | raise ValueError("n must be exact integer")
20 | if n+1 == n: # catch a value like 1e300
21 | raise OverflowError("n too large")
22 | result = 1
23 | factor = 2
24 | while factor <= n:
25 | result *= factor
26 | factor += 1
27 | return result
28 |
29 |
30 | def main():
31 | print(factorial(1))
32 | print(factorial(2))
33 |
34 |
35 | if __name__ == '__main__':
36 | main()
37 |
38 |
39 | # https://docs.python.org/2/library/doctest.html
40 |
--------------------------------------------------------------------------------
/packages/filesystem/get_path.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import os
3 |
4 |
5 | def get_file_path():
6 | return os.path.realpath(__file__)
7 |
8 |
9 | def get_file_dir_path():
10 | return os.path.dirname(os.path.realpath(__file__))
11 |
12 |
13 | def get_file_name():
14 | return os.path.basename(get_file_path())
15 |
16 |
17 | def get_cwd():
18 | return os.getcwd()
19 |
20 |
21 | def get_home_path():
22 | home = os.path.expanduser("~")
23 | return home
24 |
25 |
26 | def main():
27 | print('__file__ >>> {}'.format(__file__))
28 | print('file path >>> {}'.format(get_file_path()))
29 | print('dir path >>> {}'.format(get_file_dir_path()))
30 | print('file name >>> {}'.format(get_file_name()))
31 | print('cwd >>> {}'.format(get_cwd()))
32 | print('home >>> {}'.format(get_home_path()))
33 |
34 | if __name__ == '__main__':
35 | main()
36 |
--------------------------------------------------------------------------------
/builtin_packages/re_sp/re_collect.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import re
3 |
4 |
5 | def find_and_print(order, r, s, group=0):
6 | m = re.search(r, s)
7 | print('{}: {} | {}'.format(order, s, m.group(group) if m else None))
8 |
9 |
10 | def main():
11 | s = '- [1.1.1 连接管理与安全性](#1.1.1 连接管理与安全性)' # to find 1.1.1 连接管理与安全性
12 | find_and_print('A', r"\(#(.*)\)", s, 1)
13 | r_url = r"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$"
14 | s = 'xxx http://exp.com' # to find None
15 | find_and_print('B', r_url, s, 0)
16 | s = 'http://exp.com' # to find http://exp.com
17 | find_and_print('B', r_url, s, 0)
18 |
19 |
20 | if __name__ == '__main__':
21 | main()
22 |
23 | # https://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url
24 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/errorhandler_app/app2.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, abort, jsonify
2 | from werkzeug.exceptions import HTTPException, default_exceptions
3 |
4 | app = Flask('test')
5 |
6 |
7 | @app.errorhandler(Exception)
8 | def handle_error(e):
9 | code = 500
10 | if isinstance(e, HTTPException):
11 | code = e.code
12 | return jsonify(error=str(e)), code
13 |
14 |
15 | for ex in default_exceptions:
16 | app.register_error_handler(ex, handle_error)
17 |
18 |
19 | @app.route('/')
20 | def index():
21 | return 'hello world'
22 |
23 |
24 | @app.route("/500")
25 | def error_500():
26 | a = 100 / 0
27 | return f'hello {a}'
28 |
29 |
30 | @app.route("/r500")
31 | def error_r500():
32 | abort(500)
33 | return 'hello'
34 |
35 |
36 | def main():
37 | app.run(debug=True)
38 |
39 |
40 | if __name__ == '__main__':
41 | main()
42 |
--------------------------------------------------------------------------------
/packages/peewee_sp/group_by_and_count.py:
--------------------------------------------------------------------------------
1 | from peewee import *
2 |
3 | from packages.peewee_sp.mysql_db import db
4 |
5 |
6 | class BaseModel(Model):
7 | class Meta:
8 | database = db
9 |
10 |
11 | class Comment(BaseModel):
12 | parent_id = IntegerField()
13 |
14 |
15 | def create():
16 | db.connect()
17 | db.create_tables([Comment])
18 | Comment.create(parent_id=1)
19 | Comment.create(parent_id=1)
20 | Comment.create(parent_id=2)
21 |
22 |
23 | def main():
24 | # create()
25 | query = (Comment.select(Comment.parent_id, fn.COUNT(Comment.id).alias('ct'))
26 | .group_by(Comment.parent_id))
27 | for obj in query:
28 | print(obj.parent_id, obj.ct)
29 |
30 |
31 | if __name__ == '__main__':
32 | main()
33 |
34 |
35 | # https://stackoverflow.com/questions/45760563/returning-a-count-of-grouped-items-in-peewee-orm
36 |
--------------------------------------------------------------------------------
/packages/pyjwt_sp/readme.md:
--------------------------------------------------------------------------------
1 | # pyjwt usage
2 |
3 | ## jwt 里一些字段的含义
4 |
5 | - iat,Issued At,授权的时间
6 | - exp,Expiration Time,失效时间
7 | - nbf,Not Before Time,生效时间
8 | - iss,Issuer,授权者
9 | - sub,Subject,授权目的
10 | - jti,JWT ID,ID
11 |
12 | 上面的时间都是用时间戳
13 |
14 | ## refs
15 |
16 | - [RFC 7519 - JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519)
17 | - [JSON Web Token - Wikipedia](https://en.wikipedia.org/wiki/JSON_Web_Token)
18 | - [Welcome to PyJWT — PyJWT 1.6.1 documentation](https://pyjwt.readthedocs.io/en/latest/)
19 | - [Flask + PyJWT 实现基于Json Web Token的用户认证授权 – 那时那你](https://www.thatyou.cn/flask-pyjwt-%E5%AE%9E%E7%8E%B0%E5%9F%BA%E4%BA%8Ejson-web-token%E7%9A%84%E7%94%A8%E6%88%B7%E8%AE%A4%E8%AF%81%E6%8E%88%E6%9D%83/)
20 | - [IBM Knowledge Center - 用于 OAuth 客户机授权的 JSON Web 令牌 (JWT)](https://www.ibm.com/support/knowledgecenter/zh/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/cwlp_jwttoken.html)
21 |
--------------------------------------------------------------------------------
/packages/asyncio_sp/async_wget.py:
--------------------------------------------------------------------------------
1 | import asyncio
2 |
3 |
4 | async def wget(host):
5 | print('wget %s...' % host)
6 | connect = asyncio.open_connection(host, 80)
7 | reader, writer = await connect
8 | header = f'GET / HTTP/1.0\r\nHost: {host}\r\n\r\n'
9 | writer.write(header.encode('utf-8'))
10 | await writer.drain()
11 | while True:
12 | line = await reader.readline()
13 | if line == b'\r\n':
14 | break
15 | print('{host} header > {line}'.format(host=host, line=line.decode('utf-8').rstrip()))
16 | # ignore the body, close the socket
17 | writer.close()
18 |
19 |
20 | def main():
21 | loop = asyncio.get_event_loop()
22 | tasks = [wget(host) for host in ['httpbin.org', 'requestb.in']]
23 | loop.run_until_complete(asyncio.wait(tasks))
24 | loop.close()
25 |
26 |
27 | if __name__ == '__main__':
28 | main()
29 |
--------------------------------------------------------------------------------
/prjs/019-read-video-duration/app.py:
--------------------------------------------------------------------------------
1 | import subprocess
2 |
3 |
4 | def get_duration(url):
5 | """Get the duration of a video using ffprobe."""
6 | cmd = 'ffprobe -i {} -show_entries format=duration -v quiet -of csv="p=0"'.format(url)
7 | output = subprocess.check_output(
8 | cmd,
9 | shell=True,
10 | stderr=subprocess.STDOUT
11 | )
12 | return float(output)
13 |
14 |
15 | def main():
16 | video_urls = [
17 | "http://i4.chuimg.com/ce94ccea88ae11e89c2402420a000130_720w_720h.mp4",
18 | "http://i4.chuimg.com/0a7e9af88a4711e89c2402420a000130_720w_720h.mp4",
19 | ]
20 | for video_url in video_urls:
21 | print(video_url, get_duration(video_url))
22 |
23 |
24 | if __name__ == '__main__':
25 | main()
26 |
27 | # https://stackoverflow.com/questions/26974921/how-to-get-a-online-videos-duration-without-downloading-the-full-video
28 |
--------------------------------------------------------------------------------
/utils/005-get-image-type/app.py:
--------------------------------------------------------------------------------
1 | import imghdr
2 |
3 |
4 | def get_image_type_of_file(file_name):
5 | type_ = imghdr.what(file_name) or ''
6 | if type_ == 'jpeg':
7 | type_ = 'jpg'
8 | return type_
9 |
10 |
11 | def get_image_type_of_file_data(file_data):
12 | type_ = imghdr.what('', file_data) or ''
13 | if type_ == 'jpeg':
14 | type_ = 'jpg'
15 | return type_
16 |
17 |
18 | def main():
19 | file_name = '../../packages/_common_img/google.jpg'
20 | file_name2 = '../../packages/_common_img/google.jpg.png'
21 | print(get_image_type_of_file(file_name))
22 | print(get_image_type_of_file(file_name2))
23 | with open(file_name, 'rb') as f:
24 | print(get_image_type_of_file_data(f.read()))
25 | with open(file_name2, 'rb') as f2:
26 | print(get_image_type_of_file_data(f2.read()))
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
--------------------------------------------------------------------------------
/packages/coroutine_sp/yield_task_scheduler.py:
--------------------------------------------------------------------------------
1 | from collections import deque
2 |
3 |
4 | def task(name, times):
5 | for i in range(times):
6 | yield i
7 | print(name, i)
8 |
9 |
10 | class Runner(object):
11 |
12 | def __init__(self, tasks):
13 | self.tasks = deque(tasks)
14 |
15 | def next(self):
16 | return self.tasks.pop()
17 |
18 | def run(self):
19 | while len(self.tasks):
20 | task = self.next()
21 | try:
22 | i = next(task)
23 | print(f'>>> i:{i}')
24 | except StopIteration:
25 | pass
26 | else:
27 | self.tasks.appendleft(task)
28 |
29 |
30 | def main():
31 | Runner([
32 | task('hsfzxjy', 5),
33 | task('Jack', 4),
34 | task('Bob', 6)
35 | ]).run()
36 |
37 |
38 | if __name__ == '__main__':
39 | main()
40 |
--------------------------------------------------------------------------------
/packages/filesystem/read_write_file.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def write_and_read():
5 | print("=== write_and_read ===")
6 | with open('temp.txt', 'w') as f:
7 | f.write('hello\nworld')
8 | with open('temp.txt', 'r') as f:
9 | lines = f.readlines()
10 | print('Lines:\n{}'.format(lines))
11 |
12 |
13 | def write_with_line_break_and_read():
14 | print("=== write_with_line_break_and_read ===")
15 | lines = ['hello', 'world']
16 | with open('temp.txt', 'w') as f:
17 | f.writelines(line + '\n' for line in lines)
18 | with open('temp.txt', 'r') as f:
19 | lines = f.readlines()
20 | print('Lines:\n{}'.format(lines))
21 |
22 |
23 | def main():
24 | write_and_read()
25 | write_with_line_break_and_read()
26 |
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
31 | # https://www.guru99.com/reading-and-writing-files-in-python.html
32 |
--------------------------------------------------------------------------------
/packages/peewee_sp/transcation/deco.py:
--------------------------------------------------------------------------------
1 | from peewee import Model, CharField, IntegerField, IntegrityError
2 |
3 | from packages.peewee_sp.mysql_db import db
4 |
5 |
6 | class BaseModel(Model):
7 | class Meta:
8 | database = db
9 |
10 |
11 | class TranFoo(BaseModel):
12 | foo = IntegerField(unique=True)
13 |
14 |
15 | class TranBar(BaseModel):
16 | bar = IntegerField(unique=True)
17 |
18 |
19 | def create_tables():
20 | db.create_tables([TranFoo, TranBar])
21 |
22 |
23 | @db.atomic()
24 | def create_foo_and_bar():
25 | TranFoo.create(foo=2)
26 | TranBar.create(bar=10)
27 |
28 |
29 | def main():
30 | # create_tables()
31 | # TranBar.create(bar=10)
32 | try:
33 | create_foo_and_bar()
34 | except IntegrityError as e:
35 | print(e)
36 | print(TranFoo.select().where(TranFoo.foo==2).count())
37 |
38 |
39 | if __name__ == '__main__':
40 | main()
41 |
--------------------------------------------------------------------------------
/packages/asyncio_sp/asyncio_wget.py:
--------------------------------------------------------------------------------
1 | import asyncio
2 |
3 |
4 | @asyncio.coroutine
5 | def wget(host):
6 | print('wget %s...' % host)
7 | connect = asyncio.open_connection(host, 80)
8 | reader, writer = yield from connect
9 | header = f'GET / HTTP/1.0\r\nHost: {host}\r\n\r\n'
10 | writer.write(header.encode('utf-8'))
11 | yield from writer.drain()
12 | while True:
13 | line = yield from reader.readline()
14 | if line == b'\r\n':
15 | break
16 | print('{host} header > {line}'.format(host=host, line=line.decode('utf-8').rstrip()))
17 | # ignore the body, close the socket
18 | writer.close()
19 |
20 |
21 | def main():
22 | loop = asyncio.get_event_loop()
23 | tasks = [wget(host) for host in ['httpbin.org', 'requestb.in']]
24 | loop.run_until_complete(asyncio.wait(tasks))
25 | loop.close()
26 |
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
--------------------------------------------------------------------------------
/frameworks/flask_snippets/flask_json_encoder/app.py:
--------------------------------------------------------------------------------
1 | import datetime
2 | from flask import Flask, jsonify
3 | from flask.json import JSONEncoder
4 |
5 |
6 | class MyJSONEncoder(JSONEncoder):
7 |
8 | def default(self, obj):
9 | try:
10 | if isinstance(obj, datetime.datetime):
11 | return obj.strftime('%Y-%m-%d %H:%M:%S')
12 | iterable = iter(obj)
13 | except TypeError as e:
14 | pass
15 | else:
16 | return list(iterable)
17 | return JSONEncoder.default(self, obj)
18 |
19 |
20 | app = Flask(__name__)
21 | app.json_encoder = MyJSONEncoder
22 |
23 |
24 | @app.route("/")
25 | def hello():
26 | now = datetime.datetime.now()
27 | return jsonify({"now": now, 'foo': range(10)})
28 |
29 |
30 | def main():
31 | app.run(debug=True)
32 |
33 |
34 | if __name__ == '__main__':
35 | main()
36 |
37 | # http://flask.pocoo.org/snippets/119/
38 |
--------------------------------------------------------------------------------
/packages/aiohttp/simple_aiohttp_server.py:
--------------------------------------------------------------------------------
1 | import asyncio
2 |
3 | from aiohttp import web
4 |
5 |
6 | async def index(request):
7 | await asyncio.sleep(0.5)
8 | return web.Response(body=b'Index
')
9 |
10 |
11 | async def hello(request):
12 | await asyncio.sleep(0.5)
13 | text = 'hello, %s!
' % request.match_info['name']
14 | return web.Response(body=text.encode('utf-8'))
15 |
16 |
17 | async def init(loop):
18 | app = web.Application(loop=loop)
19 | app.router.add_route('GET', '/', index)
20 | app.router.add_route('GET', '/hello/{name}', hello)
21 | srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000)
22 | print('Server started at http://127.0.0.1:8000...')
23 | return srv
24 |
25 |
26 | def main():
27 | loop = asyncio.get_event_loop()
28 | loop.run_until_complete(init(loop))
29 | loop.run_forever()
30 |
31 |
32 | if __name__ == '__main__':
33 | main()
34 |
--------------------------------------------------------------------------------
/packages/dataclasses_sp/hello_dataclasses.py:
--------------------------------------------------------------------------------
1 | from dataclasses import dataclass, asdict
2 |
3 |
4 | @dataclass
5 | class SimpleDataObject(object):
6 | field_a: int
7 | field_b: str
8 |
9 |
10 | @classmethod
11 | class DataObject(object):
12 | field_c: int
13 | s_obj: SimpleDataObject
14 |
15 |
16 | def main():
17 | obj = SimpleDataObject(1, 'aaa')
18 | print(obj)
19 | print(asdict(obj))
20 |
21 | obj2 = SimpleDataObject(100, obj)
22 | print(obj2)
23 | print(asdict(obj2))
24 |
25 | d = {'field_a': 1, 'field_b': 'aaa'}
26 | obj = SimpleDataObject(field_a=1, field_b='aaa')
27 | print(obj)
28 | obj = SimpleDataObject(**d)
29 | print(obj)
30 | print(type(obj))
31 |
32 | # d2 = {'field_a': 100, 'field_b': {'field_a': 1, 'field_b': 'aaa'}}
33 | # obj2 = DataObject()
34 | # obj2 = DataObject(**d2)
35 | # print(obj2)
36 |
37 |
38 | if __name__ == '__main__':
39 | main()
40 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/demo_subclass_and_load_data.py:
--------------------------------------------------------------------------------
1 | from pydantic import BaseModel
2 |
3 |
4 | class Foo(BaseModel):
5 | foo: str
6 |
7 |
8 | class Bar(Foo):
9 | bar: str
10 |
11 |
12 | class FooView(BaseModel):
13 | foo_uc: str # uppercase
14 |
15 | @classmethod
16 | def trans_data(cls, data: dict) -> dict:
17 | data = data.copy()
18 | data['foo_uc'] = data['foo'].upper()
19 | return data
20 |
21 |
22 | class BarView(FooView):
23 | bar_lc: str
24 |
25 | @classmethod
26 | def trans_data(cls, data: dict) -> dict:
27 | data = super().trans_data(data)
28 | data['bar_lc'] = data['bar'].lower()
29 | return data
30 |
31 |
32 | def main():
33 | bar = Bar.parse_obj({'foo': 'f', 'bar': 'B'})
34 | print(bar)
35 | bar_view = BarView.parse_obj(BarView.trans_data(bar.dict()))
36 | print(bar_view)
37 |
38 |
39 | if __name__ == '__main__':
40 | main()
41 |
42 |
--------------------------------------------------------------------------------
/basic/copy_sp/copy_sp.py:
--------------------------------------------------------------------------------
1 | import copy
2 |
3 |
4 | def shallow_copy():
5 | print('=== shallow_copy ===')
6 | d1 = {'a': 1}
7 | print(f'd1: {d1}')
8 | d2 = d1.copy()
9 | print(f'd2: {d2}')
10 | d2['a'] = 2
11 | d2['b'] = 3
12 | print(f'd2: {d2}')
13 | print(f'd1: {d1}')
14 |
15 | d3 = {'d': d1}
16 | print(f'd3: {d3}')
17 | d4 = d3.copy()
18 | print(f'd4: {d4}')
19 | d1['a'] = 2
20 | print(f'd3: {d3}')
21 | print(f'd4: {d4}')
22 |
23 |
24 | def deep_copy():
25 | print('=== deep_copy ===')
26 | d1 = {'a': 1}
27 | d2 = {'b': d1}
28 | d3 = {'c': d2}
29 | d4 = copy.deepcopy(d3)
30 | print(f'd3: {d3}')
31 | print(f'd4: {d4}')
32 | d3['c']['b']['a'] = 2
33 | print(f'd2: {d2}')
34 | print(f'd3: {d3}')
35 | print(f'd4: {d4}')
36 |
37 |
38 | def main():
39 | shallow_copy()
40 | deep_copy()
41 |
42 |
43 | if __name__ == '__main__':
44 | main()
45 |
--------------------------------------------------------------------------------
/utils/009-download-then-upload-image-file/app.py:
--------------------------------------------------------------------------------
1 | import imghdr
2 | import io
3 | import shutil
4 |
5 | import requests
6 |
7 |
8 | def main():
9 | image_url = 'https://qn.cdn.cliiip.com/imgs/u/c2eddd32-754b-4e3b-975b-86741561bda5.png'
10 | r = requests.get(image_url, stream=True)
11 | with io.BytesIO() as f:
12 | r.raw.decode_content = True # ?
13 | shutil.copyfileobj(r.raw, f)
14 | f.seek(0)
15 |
16 | r = requests.post(
17 | 'https://httpbin.org/post',
18 | files={
19 | 'media': f
20 | }
21 | )
22 | print(r.json())
23 |
24 | f.name = 'imagefile'
25 | f.seek(0)
26 | r = requests.post(
27 | 'https://httpbin.org/post',
28 | files={
29 | 'media': (f.name, f),
30 | }
31 | )
32 | print(r.json())
33 |
34 |
35 | if __name__ == '__main__':
36 | main()
37 |
--------------------------------------------------------------------------------
/basic/str_sp/str_demo.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def split_str():
5 | print("===== split_str =====")
6 | url = 'www.google.com.hk'
7 | print(url.split('.'))
8 |
9 |
10 | def split_unicode():
11 | print("===== split_unicode =====")
12 | text = u'正义、秩序、自由'
13 | print(text.split(u'、'))
14 | template = u'城市-职业-昵称'
15 | print(template.split('-'))
16 |
17 |
18 | def sort_str():
19 | print("===== sort_str =====")
20 | str_list = [
21 | "0802aaa",
22 | "1202aa",
23 | "0209aaa",
24 | "0812ccc",
25 | "0802ddd",
26 | ]
27 | str_list.sort()
28 | print(str_list)
29 |
30 |
31 | def get_char_of_str():
32 | print("===== get_char_of_str =====")
33 | s = 'hello'
34 | for c in s:
35 | print(c)
36 |
37 |
38 | def main():
39 | split_str()
40 | split_unicode()
41 | sort_str()
42 | get_char_of_str()
43 |
44 |
45 | if __name__ == '__main__':
46 | main()
47 |
--------------------------------------------------------------------------------
/builtin_packages/datetime_sp/week_day.py:
--------------------------------------------------------------------------------
1 | import datetime
2 |
3 |
4 | def this_monday_and_related_sunday():
5 | now = datetime.datetime.now()
6 | today = datetime.datetime(now.year, now.month, now.day)
7 | this_monday = today - datetime.timedelta(now.weekday())
8 | related_sunday = this_monday + datetime.timedelta(6)
9 | return this_monday, related_sunday
10 |
11 |
12 | def chinese_weekday(dt: datetime.datetime) -> str:
13 | cn_number = ['一', '二', '三', '四', '五', '六', '日']
14 | return f'周{cn_number[dt.weekday()]}'
15 |
16 |
17 | def main():
18 | print('Hello')
19 | dt = datetime.datetime(2019, 3, 25)
20 | print(dt, dt.weekday()) # 周一 0
21 |
22 | dt += datetime.timedelta(days=6)
23 | print(dt, dt.weekday()) # 周日 6
24 |
25 | this_monday, related_sunday = this_monday_and_related_sunday()
26 | print(chinese_weekday(this_monday), chinese_weekday(related_sunday))
27 |
28 |
29 | if __name__ == '__main__':
30 | main()
31 |
--------------------------------------------------------------------------------
/builtin_packages/re_sp/re_tokens.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import re
3 |
4 |
5 | def add_space_after_hash(s):
6 | m = re.search(r'#(\S+)', s)
7 | if m:
8 | return '# {}'.format(m.group(1))
9 | return s
10 |
11 |
12 | def find_str(s):
13 | m = re.search(r'https?://www\.example\.com\?abc_d=(\d+)', s)
14 | if m:
15 | return 'find: {} ||| {}'.format(m.group(0), m.group(1))
16 | return
17 |
18 |
19 | def main():
20 | print(add_space_after_hash('##'))
21 | print(add_space_after_hash('#1'))
22 | print(add_space_after_hash('# 2'))
23 | print(add_space_after_hash('#大西瓜'))
24 | print(add_space_after_hash('#大西瓜#大西瓜'))
25 | print(find_str('https://www.example.com?abc_d=123'))
26 | print(find_str('http://www.example.com?abc_d=12345'))
27 | print(find_str('https://www.example.com?abc_d=1234589sdf'))
28 | print(find_str('https://www.example.com?abc_d=12345)dsf'))
29 |
30 |
31 | if __name__ == '__main__':
32 | main()
33 |
--------------------------------------------------------------------------------
/utils/015-base36/app.py:
--------------------------------------------------------------------------------
1 | def base36encode(number, alphabet='0123456789abcdefghijklmnopqrstuvwxyz'):
2 | """Converts an integer to a base36 string."""
3 | if not isinstance(number, (int)):
4 | raise TypeError('number must be an integer')
5 |
6 | base36 = ''
7 | sign = ''
8 |
9 | if number < 0:
10 | sign = '-'
11 | number = -number
12 |
13 | if 0 <= number < len(alphabet):
14 | return sign + alphabet[number]
15 |
16 | while number != 0:
17 | number, i = divmod(number, len(alphabet))
18 | base36 = alphabet[i] + base36
19 |
20 | return sign + base36
21 |
22 |
23 | def base36decode(number):
24 | return int(number, 36)
25 |
26 |
27 | def main():
28 | print(base36decode('zzzzzz'))
29 | print(base36encode(2176782335))
30 | print(base36encode(2176782336))
31 |
32 |
33 | if __name__ == '__main__':
34 | main()
35 |
36 | # https://stackoverflow.com/questions/1181919/python-base-36-encoding
37 |
--------------------------------------------------------------------------------
/basic/exception_sp/hello_exception.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 |
3 |
4 | def maybe_error(n, raise_it=False):
5 | print('maybe_error == start == {}'.format(n))
6 | a = None
7 | try:
8 | a = 10 / n
9 | except Exception as e:
10 | print('maybe_error == except == {}'.format(n))
11 | print('e: {}'.format(e))
12 | if raise_it:
13 | print('maybe_error == raise == {}'.format(n))
14 | raise e
15 | else:
16 | print('maybe_error == else == {}'.format(n))
17 | finally:
18 | print('maybe_error == finally == {}'.format(n))
19 | print('maybe_error == end == {}'.format(n))
20 |
21 |
22 | def box(n, raise_it):
23 | try:
24 | maybe_error(n, raise_it)
25 | except Exception:
26 | print('box == except == {}'.format(n))
27 |
28 |
29 | def main():
30 | box(1, raise_it=False)
31 | box(0, raise_it=False)
32 | box(0, raise_it=True)
33 |
34 |
35 | if __name__ == '__main__':
36 | main()
37 |
--------------------------------------------------------------------------------
/packages/schematics_snippets/validation.py:
--------------------------------------------------------------------------------
1 | from schematics.models import Model
2 | from schematics.types import StringType, IntType
3 | from schematics.exceptions import DataError
4 |
5 |
6 | class MyModel1(Model):
7 | foo = StringType()
8 |
9 |
10 | class MyModel2(Model):
11 | foo = StringType(required=True)
12 |
13 |
14 | class MyModel3(Model):
15 | foo = IntType()
16 |
17 |
18 | def validate_model(m):
19 | try:
20 | m.validate()
21 | except DataError as e:
22 | print(e)
23 | print(e.messages)
24 |
25 |
26 | def main():
27 | m1 = MyModel1({'foo': 'a'})
28 | m1.validate()
29 |
30 | m2 = MyModel1({})
31 | m2.validate()
32 |
33 | m3 = MyModel2({})
34 | # m3.validate() # schematics.exceptions.DataError: {"foo": ["This field is required."]}
35 | validate_model(m3)
36 |
37 | m4 = MyModel3({'foo': '4'})
38 | print(m4, m4.foo)
39 | validate_model(m4)
40 |
41 |
42 | if __name__ == '__main__':
43 | main()
44 |
--------------------------------------------------------------------------------
/packages/pydantic_sp/pep_484_types.py:
--------------------------------------------------------------------------------
1 | from typing import Dict, List, Optional, Union, Set
2 |
3 | from pydantic import BaseModel
4 |
5 |
6 | class Model(BaseModel):
7 | simple_list: list = None
8 | list_of_ints: List[int] = None
9 |
10 | simple_dict: dict = None
11 | dict_str_float: Dict[str, float] = None
12 |
13 | simple_set: set = None
14 | set_bytes: Set[bytes] = None
15 |
16 | str_or_bytes: Union[str, bytes] = None
17 | none_or_str: Optional[str] = None
18 |
19 | compound: Dict[Union[str, bytes], List[Set[int]]] = None
20 |
21 |
22 | def main():
23 | print(Model(simple_list=['1', '2', '3']).simple_list) # > ['1', '2', '3']
24 | print(Model(list_of_ints=['1', '2', '3']).list_of_ints) # > [1, 2, 3]
25 |
26 | print(Model(simple_dict={'a': 1, b'b': 2}).simple_dict) # > {'a': 1, b'b': 2}
27 | print(Model(dict_str_float={'a': 1, b'b': 2}).dict_str_float) # > {'a': 1.0, 'b': 2.0}
28 |
29 |
30 | if __name__ == '__main__':
31 | main()
32 |
33 |
--------------------------------------------------------------------------------
/packages/redis_sp/redis_list.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import redis
3 | r = redis.StrictRedis(host='localhost', port=6379, db=0)
4 | list_key = 'my_list'
5 | list_key2 = 'my_list2'
6 | list_key3 = 'my_list3'
7 |
8 |
9 | def push():
10 | print('===== push =====')
11 | r.rpush(list_key, 'd')
12 | r.rpush(list_key, 'e', 'f')
13 | r.lpush(list_key, 'c')
14 | r.lpush(list_key, 'b', 'a')
15 | print("r.lrange: {}".format(r.lrange(list_key, 0, -1)))
16 | print("r.llen: {}".format(r.llen(list_key)))
17 | print("r.llen: {}".format(r.llen(list_key3)))
18 | print(r.lrange(list_key, 0, -1))
19 | r.lpush(list_key2, 'a')
20 |
21 |
22 | def pop():
23 | print('===== pop =====')
24 | print(r.lpop(list_key2))
25 | print(r.lpop(list_key2))
26 | print(r.lpop(list_key3))
27 |
28 |
29 | def main():
30 | push()
31 | pop()
32 | r.delete(list_key)
33 | r.delete(list_key2)
34 | r.delete(list_key3)
35 |
36 |
37 | if __name__ == '__main__':
38 | main()
39 |
--------------------------------------------------------------------------------
/utils/004-get-title-from-url/app.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import re
3 | import urllib.request
4 | from bs4 import BeautifulSoup
5 |
6 |
7 | def is_a_valid_url(s):
8 | regex = re.compile(
9 | r'^(?:http|ftp)s?://' # http:// or https://
10 | r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
11 | r'localhost|' # localhost...
12 | r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
13 | r'(?::\d+)?' # optional port
14 | r'(?:/?|[/?]\S+)$', re.IGNORECASE)
15 | return bool(regex.search(s))
16 |
17 |
18 | def get_title_from_url(url):
19 | if not is_a_valid_url(url):
20 | return ''
21 | soup = BeautifulSoup(urllib.request.urlopen(url), 'html.parser')
22 | return soup.title.string
23 |
24 |
25 | def main():
26 | url = 'https://mp.weixin.qq.com/s/uqPPe_YdG7c0JdSHPng_MQ'
27 | print(u"{}\n{}".format(get_title_from_url(url), url))
28 |
29 |
30 | if __name__ == '__main__':
31 | main()
32 |
--------------------------------------------------------------------------------
/basic/inhert_sp/multi_inhert.py:
--------------------------------------------------------------------------------
1 | class A:
2 | def say(self):
3 | print("A Hello:", self)
4 |
5 |
6 | class A2:
7 | pass
8 |
9 |
10 | class A3:
11 | pass
12 |
13 |
14 | # class B(A2, A): # TypeError: Cannot create a consistent method resolution order (MRO) for bases A2, A
15 | class B(A2):
16 | def eat(self):
17 | print("B Eating:", self)
18 |
19 |
20 | class C(A, A2, A3):
21 | def eat(self):
22 | print("C Eating:", self)
23 |
24 |
25 | class D(B, C):
26 | def say(self):
27 | super().say()
28 | print("D Hello:", self)
29 |
30 | def dinner(self):
31 | self.say()
32 | super().say()
33 | self.eat()
34 | super().eat()
35 |
36 |
37 | def main():
38 | d = D()
39 | d.dinner()
40 | print(D.mro()) # D B C A A2 A3
41 | # 因为要保证顺序一致,所以 A2 还是放在了 A 后面
42 |
43 |
44 | if __name__ == '__main__':
45 | main()
46 |
47 | # https://medium.com/technology-nineleaps/python-method-resolution-order-4fd41d2fcc
48 |
--------------------------------------------------------------------------------
/prjs/006-make-1-million-requests-with-python-aiohttp/s3_server.py:
--------------------------------------------------------------------------------
1 | import asyncio
2 | from datetime import datetime
3 | from aiohttp import web
4 | import random
5 |
6 |
7 | async def hello(request):
8 | # name = request.match_info.get("name", "foo")
9 | now = datetime.now().isoformat()
10 | delay = random.uniform(0.01, 0.8)
11 | await asyncio.sleep(delay)
12 | headers = {"content_type": "text/html", "delay": str(delay)}
13 | print(f"{now}: {request.path} delay: {delay}")
14 | body = "0.7777765323550566 0.6820758280410311 0.4466949910540329 0.4301714118515695 0.25021981851292885" \
15 | "0.43939880915949237 0.5380599085488146 0.01086652573727398"
16 | response = web.Response(body=body, headers=headers)
17 | return response
18 |
19 |
20 | def main():
21 | # 用一样的 seed 保证每次运行效果相同
22 | random.seed(1)
23 | app = web.Application()
24 | app.router.add_route("GET", "/{name}", hello)
25 | web.run_app(app)
26 |
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
--------------------------------------------------------------------------------
/packages/threading_snippets/thread_and_log.py:
--------------------------------------------------------------------------------
1 | # coding: utf-8
2 | import logging
3 | import sys
4 | from Queue import Queue
5 | from threading import Thread
6 |
7 | worker_size = 10
8 | queue_size = 10
9 |
10 | q = Queue(maxsize=queue_size)
11 | logging.basicConfig(
12 | stream=sys.stdout, # or filename='xxx.log',
13 | level=logging.INFO,
14 | format='%(asctime)s %(name)s %(levelname)s %(module)s %(funcName)s: %(message)s',
15 | datefmt='%Y-%m-%d %H:%M:%S'
16 | )
17 | # log it
18 |
19 |
20 | def worker():
21 | while True:
22 | item = q.get()
23 | logging.info(item)
24 | q.task_done()
25 |
26 |
27 | def main():
28 |
29 | for i in range(worker_size):
30 | t = Thread(target=worker)
31 | t.daemon = True
32 | t.start()
33 |
34 | print('=== start work ===')
35 | for i in range(100):
36 | q.put(i)
37 | q.join() # block until all tasks are done
38 | print('=== end ===')
39 |
40 |
41 | if __name__ == '__main__':
42 | main()
43 |
--------------------------------------------------------------------------------
/utils/007-add-params-to-url/app.py:
--------------------------------------------------------------------------------
1 | try:
2 | from urlparse import urlparse, urlunparse, parse_qsl
3 | from urllib import urlencode
4 | except: # For Python 3
5 | from urllib.parse import urlencode, urlparse, parse_qsl, urlunparse
6 |
7 |
8 | def add_params_to_url(url, params):
9 | url_parts = list(urlparse(url))
10 | query = dict(parse_qsl(url_parts[4]))
11 | query.update(params)
12 | url_parts[4] = urlencode(query)
13 | return urlunparse(url_parts)
14 |
15 |
16 | def main():
17 | url1 = 'http://example.com/search?q=question&t=tag#class1'
18 | url2 = 'http://example.com/'
19 | url3 = 'exp.io'
20 | params = {'lang': 'en', 'tag': 'python'}
21 | print(urlencode(params))
22 |
23 | print(add_params_to_url(url1, params))
24 | print(add_params_to_url(url2, params))
25 | print(add_params_to_url(url3, params))
26 |
27 |
28 | if __name__ == '__main__':
29 | main()
30 |
31 |
32 | # https://stackoverflow.com/questions/2506379/add-params-to-given-url-in-python
33 |
--------------------------------------------------------------------------------
/packages/pil_sp/read_write_color.py:
--------------------------------------------------------------------------------
1 | from PIL import Image, ImageDraw
2 |
3 |
4 | def print_all_pixel_data(im):
5 | w, h = im.size
6 | for i in range(h):
7 | s = ','.join(map(str, [im.getpixel((j, i)) for j in range(w / 100)]))
8 | print(s)
9 |
10 |
11 | def draw_points(im):
12 | x, y = 10, 20
13 | im.putpixel((x, y), (255, 255, 255))
14 | im.putpixel((x, y - 1), (255, 0, 0))
15 | im.putpixel((x + 1, y), (0, 255, 0))
16 | im.putpixel((x, y + 1), (0, 0, 255))
17 | im.putpixel((x - 1, y), (0, 255, 0))
18 |
19 |
20 | def draw_lines(im):
21 | draw = ImageDraw.Draw(im)
22 | draw.line((0, 0) + im.size, fill=128)
23 | draw.line((0, im.size[1], im.size[0], 0), fill=128, width=3)
24 | del draw
25 |
26 |
27 | def main():
28 | im = Image.open("../_common_img/jump_jump.jpg")
29 | # print_all_pixel_data(im)
30 | # draw_points(im)
31 | draw_lines(im)
32 | im.save("../_common_img/jump_jump2.jpg")
33 |
34 |
35 | if __name__ == '__main__':
36 | main()
37 |
--------------------------------------------------------------------------------
/builtin_packages/enum_sp/enum_for_error_code.py:
--------------------------------------------------------------------------------
1 | from enum import Enum, IntEnum
2 |
3 |
4 | class Color(Enum):
5 | RED = 1
6 | GREEN = 2
7 | BLUE = 3
8 |
9 |
10 | class Color2(IntEnum):
11 | RED = 1
12 | GREEN = 2
13 | BLUE = 3
14 |
15 |
16 | def main():
17 | c_r = Color.RED
18 | print(c_r)
19 | print(c_r.name)
20 | print(c_r.value)
21 | # print(int(c_r)) # TypeError: int() argument must be a string, a bytes-like object or a number, not 'Color'
22 |
23 | c_r = Color2.RED
24 | print(c_r)
25 | print(c_r.name)
26 | print(c_r.value)
27 | print(int(c_r))
28 | print(c_r.__dict__)
29 |
30 | # c_r = Color('1') # ValueError: '1' is not a valid Color
31 | # c_r = Color('RED') # ValueError: 'RED' is not a valid Color
32 |
33 | c_r = Color['RED']
34 | print(c_r)
35 | # c_r = Color['1'] # KeyError: '1'
36 | # c_r = Color[1] # KeyError: 1
37 |
38 | c_r = Color(1)
39 | print(c_r)
40 |
41 |
42 | if __name__ == '__main__':
43 | main()
44 |
--------------------------------------------------------------------------------
/builtin_packages/time_sp/execution_time_b.py:
--------------------------------------------------------------------------------
1 | import time
2 | from profilehooks import profile
3 |
4 | class SampleClass:
5 |
6 | @profile
7 | def silly_fibonacci_example(self, n):
8 | """Return the n-th Fibonacci number.
9 |
10 | This is a method rather rather than a function just to illustrate that
11 | you can use the 'profile' decorator on methods as well as global
12 | functions.
13 |
14 | Needless to say, this is a contrived example.
15 | """
16 | if n < 1:
17 | raise ValueError('n must be >= 1, got %s' % n)
18 | if n in (1, 2):
19 | return 1
20 | else:
21 | return (self.silly_fibonacci_example(n - 1) +
22 | self.silly_fibonacci_example(n - 2))
23 |
24 |
25 | @profile
26 | def f2():
27 | time.sleep(1)
28 |
29 |
30 | if __name__ == '__main__':
31 | fib = SampleClass().silly_fibonacci_example
32 | print(fib(10))
33 | print(f2())
34 |
35 | # ref: http://mg.pov.lt/profilehooks/
36 |
--------------------------------------------------------------------------------
/prjs/012-draw-text-on-img/app.py:
--------------------------------------------------------------------------------
1 | from PIL import Image, ImageFont, ImageDraw
2 |
3 | data = [
4 | {
5 | 'id': '3c1d4d3c-c3b4-4b81-804a-0f82b6f35e29',
6 | 'name': '坂田银八',
7 | },
8 | {
9 | 'id': 'b46c36c6-9d84-49a6-a706-a8e216431184',
10 | 'name': '志村新八',
11 | }
12 | ]
13 |
14 |
15 | def main(bg_img_file_name, configs):
16 | for user in configs:
17 | draw_text_on_image(bg_img_file_name, user)
18 |
19 |
20 | def draw_text_on_image(bg_img_file_name, user):
21 | im = Image.open(bg_img_file_name)
22 | draw = ImageDraw.Draw(im)
23 | font = ImageFont.truetype("SourceHanSansCN-Regular.otf", 33)
24 | draw.text((190, 560), f"{user['name']}同学:", font=font, fill="#000")
25 | im.save(f"out/{user['id']}.png")
26 |
27 |
28 | def _create_bg_image():
29 | im = Image.new("RGB", (750, 1132), "#FFF")
30 | im.save('bg.png')
31 |
32 |
33 | if __name__ == '__main__':
34 | main(
35 | bg_img_file_name='bg.png',
36 | configs=data,
37 | )
38 |
39 |
--------------------------------------------------------------------------------
/prjs/020-random-yuque-page-id/app.py:
--------------------------------------------------------------------------------
1 | import random
2 |
3 |
4 | def base36encode(number, alphabet='0123456789abcdefghijklmnopqrstuvwxyz'):
5 | """Converts an integer to a base36 string."""
6 | if not isinstance(number, (int)):
7 | raise TypeError('number must be an integer')
8 |
9 | base36 = ''
10 | sign = ''
11 |
12 | if number < 0:
13 | sign = '-'
14 | number = -number
15 |
16 | if 0 <= number < len(alphabet):
17 | return sign + alphabet[number]
18 |
19 | while number != 0:
20 | number, i = divmod(number, len(alphabet))
21 | base36 = alphabet[i] + base36
22 |
23 | return sign + base36
24 |
25 |
26 | def base36decode(number):
27 | return int(number, 36)
28 |
29 |
30 | def random_yuque_page_id():
31 | return base36encode(number=random.randint(base36decode('100000'), base36decode('zzzzzz'))) # 让代码简单一点,不要 0 开头的 ID 了,不然还要处理补 0 的逻辑
32 |
33 |
34 | def main():
35 | print(random_yuque_page_id())
36 |
37 |
38 | if __name__ == '__main__':
39 | main()
40 |
--------------------------------------------------------------------------------
/packages/wtforms_sp/hello_wtforms.py:
--------------------------------------------------------------------------------
1 | from wtforms import Form, StringField, validators
2 | from wtforms.validators import ValidationError
3 |
4 |
5 | def without_space(form, field):
6 | if ' ' in field.data:
7 | raise ValidationError('Username must without space')
8 |
9 |
10 | class UsernameForm(Form):
11 | username = StringField('Username', [validators.Length(min=5), without_space], default='test')
12 |
13 |
14 | def main():
15 | form1 = UsernameForm()
16 | print('>>> form1')
17 | print(type(form1['username']))
18 | print(form1.data)
19 | print(form1.username.data)
20 | print(form1.errors)
21 | print(form1.validate())
22 | print(form1.errors)
23 |
24 | print('>>> form2')
25 | form2 = UsernameForm(username='Robert')
26 | print(form2.data)
27 | print(form2.validate())
28 | print(form2.errors)
29 |
30 | print('>>> form3')
31 | form3 = UsernameForm(username='Foo Bar')
32 | form3.validate()
33 | print(form3.errors)
34 |
35 |
36 | if __name__ == '__main__':
37 | main()
38 |
--------------------------------------------------------------------------------