Python Learning--day5

Source: Internet
Author: User
Tags local time

First, the module

1. module definition, purpose

module is a certain function of the program block, essentially the module is. py format Files , each file is a module. The module can separate complex programs by function and store them in different file names. The goal is to make the program code reusable and make the program easier to maintain.

Python modules are divided into three categories: (1) built-in modules, (2) third-party modules, and (3) custom modules.

2. Import of modules

Modules need to be imported before use, the way of import commonly used are: (1) import + [module name], for example, to import the OS module using the Import OS; (2) from [module name] import [function name].

Some modules have a long name and can be imported, giving it a simple name: import [module name] as other name.

3. References to Packages

Python files have an organization, that is, a few functional modules can be composed of a python package, stored in a directory structure, by entering the path of the package to call the package module variables, functions and so on. Below each package there is an initialization file __init__.py, which can be empty or define related code. There are three ways to refer to packages: (1) Import [package name] can only use initialization file object, (2) Imoport [pack name]. [Module name] can use the object referencing the module, (3) from [Package name] import [function name] and the second way, you can use the object referencing the module.

4. Common built-in modules:

(1) OS module

Gets the absolute path of the current file

1 Import OS 2 base_path=os.path.abspath (__file__)3print(Base_path)

Gets the absolute path of the parent directory of the current file

Import osbase_path=os.path.dirname (Os.path.abspath (__file__))print(base_ Path

(2) The SYS module contains functions related to the Python interpreter and its environment, which you can use to view the methods and member properties in him through dir(SYS).

1 Import SYS 2 Print (DIR (SYS))

Operation Result:

['__displayhook__','__doc__','__excepthook__','__interactivehook__','__loader__','__name__','__package__','__spec__','__stderr__','__stdin__','__stdout__','_clear_type_cache','_current_frames','_debugmallocstats','_getframe','_home','_mercurial','_xoptions','api_version','argv','Base_exec_prefix','Base_prefix','Builtin_module_names','Byteorder','call_tracing','Callstats','Copyright','Displayhook','Dllhandle','Dont_write_bytecode','Exc_info','Excepthook','Exec_prefix','executable','Exit','Flags','Float_info','Float_repr_style','Get_coroutine_wrapper','Getallocatedblocks','Getcheckinterval','getdefaultencoding','getfilesystemencoding','GetProfile','Getrecursionlimit','Getrefcount','getsizeof','Getswitchinterval','Gettrace','getwindowsversion','Hash_info','hexversion','Implementation','Int_info','Intern','is_finalizing','maxsize','Maxunicode','Meta_path','Modules','Path','Path_hooks','Path_importer_cache','Platform','prefix','Set_coroutine_wrapper','Setcheckinterval','Setprofile','Setrecursionlimit','Setswitchinterval','Settrace','stderr','stdin','stdout','Thread_info','version','Version_info','warnoptions','winver']
View Code

Sys.path is a list, by default Python imports the file or module, he will first find the path of the module in the Sys.path. If not,

The program will get an error. So we generally write our own program, it is best to add their own module path to the current module scan path.

1 Import Os,sys 2 base_path=os.path.dirname (Os.path.abspath (__file__))3 sys.path.append (base _path)

(3) Time module

There are three ways to represent time in Python:

(1) Timestamp: The timestamp is the offset that is calculated in seconds from 00:00:00 on January 1, 1970. ;

(2) the formatted time string;

(3) Time tuple: A tuple that contains a moment of various states, including: year, month, day, hour, minute, second, week, day, daylight saving time indicator.

    • Returns the timestamp of the current time
1 Import  Time 2 Print (Time.time ())

Operation Result:

1471843632.2690241
    • Converts a timestamp to a time tuple in the local time zone. localtime ([secs]),secs is the parameter in seconds, and takes local time when no arguments are supplied.
Import  Time Print (Time.localtime ())

Operation Result:

Time.struct_time (tm_year=2016, tm_mon=8, tm_mday=22, tm_hour=13, tm_min=35, tm_sec=5, tm_wday=0, tm_yday=235, TM_ISDST =0)

We can find any amount we need based on the offsets, such as today is the day of the year. The procedure is as follows:

Import  Time Print (Time.localtime () [7])
    • Time.gmtime ([secs]): Similar to the LocalTime () method, the Gmtime () method is the struct_time of converting a timestamp to the UTC time zone (0 o'clock Zone).
Import  Time Print (Time.gmtime ())

Operation Result:

Ime.struct_time (tm_year=2016, tm_mon=8, tm_mday=22, tm_hour=5, tm_min=39, tm_sec=44, tm_wday=0, tm_yday=235, tm_isdst= 0)
    • There are functions that turn timestamps into time tuples, then there is a function time.mktime (t) that transforms the time tuple into a timestamp, andT is a tuple with nine elements
Import Timet= (2016,8,22,13,35,5,0,235, 0)print(Time.mktime (t))

Operation Result:

1471844105.0
    • Time.sleep (secs): Thread delays the specified time run. Unit is seconds.

Second, the regular expression

Python Learning--day5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.