Python module all in one

Python Modules

https://docs.python.org/3/tutorial/modules.html

Fibonacc

# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print() def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a+b
return result
# 导入模块
import fibo # 使用
fibo.fib(1000)
fibo.fib2(100) print(fibo.__name__)
# 'fibo'
# 导入模块
import fibo # 重命名
fib = fibo.fib # 使用
fib(1000)
fib2(100) print(fibo.__name__)
# 'fibo'
# import语句有一个变体,可以将名称从模块直接导入到导入模块的符号表中。
from fibo import fib, fib2 fib(500)
# 不存在 fibo, 没有导入
print(fibo.__name__)
# None
# 这将导入除以下划线(_)开头的名称以外的所有名称。 ️
from fibo import * fib(500)
# 如果模块名称后跟as,则名称后跟as直接绑定到导入的模块。
import fibo as fib # Alias
fib.fib(500) print(fibo.__name__)
# None
from fibo import fib as fibonacci

fibonacci(500)


execute module

# python3 fibo.py <arguments>

$ python3 fibo.py

Module => Script ??? "main"

from fibo import fib as fibonacci
fibonacci(500) # 您可以将文件用作脚本以及可导入模块
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
# 因为解析命令行的代码仅在模块作为“主”文件执行时运行:
$ python fibo.py 50 # 如果导入模块,则代码不会运行
# import fibo

sys.path

https://docs.python.org/3/library/sys.html#sys.path

Python 3 模块导入方式

  1. import Module

  2. import Module as Alias_Module

  3. from Module import Function

# coding: utf8

# import Module
# 导入整个模块
import camelcase 模块名.函数名
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))
# coding: utf8

# import Module as Alias_Module
# 导入整个模块, 并且使用 Alias 模块别名
import camelcase as cc c = cc.CamelCase()
txt = "hello world"
print(c.hump(txt))
# coding: utf8

# from Module import Function
# 导入部分模块
from camelcase import CamelCase 函数名
c = CamelCase()
txt = "hello world"
print(c.hump(txt))

import group modules

python

# import group modules
from spider import url_manager, html_downloader, html_parser, html_outputer
# 导入部分模块
from spider import url_manager
from spider import html_downloader
from spider import html_parser
from spider import html_outputer

create a module

# greeting_module.py

def greeting(name):
print("Hello, " + name)

use a module

Note: When using a function from a module, use the syntax: module_name.function_name.

import greeting_module

greeting_module.greeting("xgqfrms")

# Hello, xgqfrms

import Module as Alias

module alias

human.py

person = {
"name": "xgqfrms",
"age": 23,
"country": "China"
}

import human as man age = man.person["age"]
print(age) # 23

import Module as Alias_Module

# coding: utf8

# 导入整个模块
# import camelcase # 模块名.函数名
# c = camelcase.CamelCase()
# txt = "hello world"
# print(c.hump(txt)) # 导入部分模块
# from camelcase import CamelCase # 函数名
# c = CamelCase()
# txt = "hello world"
# print(c.hump(txt)) # import Module as Alias_Module
# 导入整个模块, 并且使用 Alias 模块别名
import camelcase as cc c = cc.CamelCase()
txt = "hello world"
print(c.hump(txt)) """bug # ModuleNotFoundError: No module named 'Camelcase'
# AttributeError: module 'camelcase' has no attribute 'CamelCase' # This method capitalizes the first letter of each word.
"""

Built-in Modules

import platform

x = platform.system()
print(x)

list function names in a module

list all the function names (or variable names) in a module

Note: The dir() function can be used on all modules, also the ones you create yourself.

#!/usr/bin/env python3

# coding: utf8

__author__ = 'xgqfrms'

__editor__ = 'vscode'

__version__ = '1.0.1'

__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
""" """
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-01-0
*
* @description
* @augments
* @example
* @link
*
*/
""" import platform dir = dir(platform)
# List all the defined names belonging to the `platform` module: print(dir) # ['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_comparable_version', '_component_re', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', '_ver_stages', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_alias', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']

from Module import Function

You can choose to import only parts from a module, by using the from keyword.

test_module.py

# test_module has one function and one dictionary:

def greeting(name):
print("Hello, " + name) person = {
"name": "xgqfrms",
"age": 23,
"country": "China"
}
from test_module import person

print (person["age"])

Note: When importing using the from keyword, do not use the module name when referring to elements in the module.


camelcase bug

https://www.w3schools.com/python/showpython.asp?filename=demo_camelcase

import camelcase

c = camelcase.CamelCase()

txt = "hello world"

print(c.hump(txt))

# Hello World

refs

https://www.w3schools.com/python/python_modules.asp

https://www.runoob.com/python3/python3-module.html



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


Python module all in one的更多相关文章

  1. install python module

    [install python module] 参考:http://docs.python.org/2.7/install/index.html

  2. Nuke Python module的使用

    最近很多脚本工作都需要脱离nuke的gui环境运行,没有了script editor就必须要尝试Nuke Python module功能了.该模式可以执行大部分在GUI环境中的命令,在自动生成或者批量 ...

  3. __import__ 与动态加载 python module

    原文出处: koala bear    Direct use of __import__() is rare, except in cases where you want to import a m ...

  4. Python module中的全局变量

    Python module中的全局变量 我想要实现一个python module,这个module中有一些配置项,这些配置项可以被读取,被修改.一个可行的方案是把这些配置项写到一个叫settings. ...

  5. Python.Module.site

    site " This module is automatically imported during initialization. The automatic import can be ...

  6. import 本地Python module或package

    很基础很重要的一课,虽然很简单,但是防止以后忘了,还是记下来 这个笔记里说的都是import本地的,自己创建的,或者复制粘贴的别人的,总之“不是安装到library”的module or packag ...

  7. python module的结构

    python有很多module,下面是module的结构图: 拿httplib做例子,httlip module有: 4个class( HTTPConnection,HTTPSConnection,H ...

  8. Python : Module

    在Python中,一个.py文件代表一个Module.在Module中可以是任何的符合Python文件格式的Python脚本.了解Module导入机制大有用处. 1 Module 组成 1.1 Mod ...

  9. python module install

    1.issue: How can I bypass kivy module error: ImportError: DLL load failed: The specified module coul ...

随机推荐

  1. 三次握手 四次握手 原因分析 TCP 半连接队列 全连接队列

    小结 1. 三次握手的原因:保证双方收和发消息功能正常: [生活模型] "请问能听见吗""我能听见你的声音,你能听见我的声音吗" [原理]A先对B:你在么?我在 ...

  2. list中map 的value值时间排序

    public static void main(String[] args) { String sys=DateUtil.getTime().substring(0,5); System.out.pr ...

  3. innodb和myisam原理

    MyISAM索引实现 MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.如图:  这里设表一共有三列,假设我们以Col1为主键,则上图是一个MyISAM表的主索引 ...

  4. 云原生项目实践DevOps(GitOps)+K8S+BPF+SRE,从0到1使用Golang开发生产级麻将游戏服务器—第1篇

    项目初探 项目地址: 原项目:https://github.com/lonng/nanoserver 调过的:https://github.com/Kirk-Wang/nanoserver 这将是一个 ...

  5. 一:Spring Boot 的配置文件 application.properties

    Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...

  6. JavaWeb——EL及JSTL学习总结

    什么是EL表达式 为什么需要EL EL的主要作用 EL的语法 EL的开发步骤 EL实例练习 EL中的运算符 EL表达式显示内容的特点 EL的特点 EL隐式对象 EL隐式对象介绍 隐式对象实例练习 什么 ...

  7. Kali-2020 配置Docker

    Kali 2020 安装Docke 为什么在Kali上安装Docker? Kali有很多工具,但是您想运行一个不包含的工具,最干净的方法是通过Docker容器.例如,我正在研究一个名为vulhub的靶 ...

  8. java架构《并发编程框架篇 __Disruptor》

    Disruptor入门   获得Disruptor 可以通过Maven或者下载jar来安装Disruptor.只要把对应的jar放在Java classpath就可以了. 基本的事件生产和消费 我们从 ...

  9. MariaDB数据库---主从复制,galera架构

    主从复制 补充一点:⑤slave端的IO thread 将从master端请求来的二进制日志文件中的内容存储到relay_log(中继日志)中 图片来源:https://www.cnblogs.com ...

  10. (22)zip命令:压缩文件或目录&&unzip命令:解压zip文件

    1.zip 命令基本格式如下: [root@localhost ~]#zip [选项] 压缩包名 源文件或源目录列表 注意,zip 压缩命令需要手工指定压缩之后的压缩包名,注意写清楚扩展名,以便解压缩 ...