Python useful code Snippet collection stickers

Source: Internet
Author: User
Tags imap pprint
get all subclasses of a class

The code is as follows:

Def itersubclasses (CLS, _seen=none):
"" "Generator-subclasses of a given class in depth first order." "
If not isinstance (CLS, type):
Raise TypeError (_ (' itersubclasses must is called with '
' New-style classes, not%.100r ')% CLS)
_seen = _seen or set ()
Subs = cls.__subclasses__ ()
Except TypeError: # fails only if CLS is type
Subs = cls.__subclasses__ (CLS)
For sub in Subs:
If sub not in _seen:
_seen.add (sub)
Yield Sub
For sub in Itersubclasses (Sub, _seen):
Yield Sub

A simple thread mate

The code is as follows:

Import threading
Is_done = Threading. Event ()
Consumer = Threading. Thread (
Args= (Key, Self.task, Runner.result_queue, Is_done))
Consumer.start ()
Self.duration = (
Name, Kw.get ("context", {}), Kw.get ("args", {}))
Is_done.set ()
Consumer.join () #主线程堵塞 until consumer run is complete

Say a little more, threading. The Event () can also be replaced with threading. Condition (), Condition has notify (), wait (), Notifyall (). The explanations are as follows:

The code is as follows:

The wait () method releases the lock, and then blocks until it was awakened by a notify () or Notifyall () call for the same C Ondition variable in another thread. Once awakened, it re-acquires the lock and returns. It is also possible to specify a timeout.
The Notify () method wakes up one of the threads waiting for the condition variable, if any is waiting. The Notifyall () method wakes up all threads waiting for the condition variable.
Note:the notify () and Notifyall () methods don ' t release the lock; This means the thread or threads awakened won't return from their wait () call immediately D that called notify () or Notifyall () finally relinquishes ownership of the lock.

The code is as follows:

# Consume one item
Cv.acquire ()
While not an_item_is_available ():
Cv.wait ()
Get_an_available_item ()
Cv.release ()
# Produce one item
Cv.acquire ()
Make_an_item_available ()
Cv.notify ()
Cv.release ()

Calculate Run time

The code is as follows:

Class Timer (object):
def __enter__ (self):
Self.error = None
Self.start = Time.time ()
return self
def __exit__ (self, type, value, TB):
Self.finish = Time.time ()
If Type:
Self.error = (type, value, TB)
def duration (self):
Return Self.finish-self.start
with timer () as timer:
Func ()
Return Timer.duration ()

Meta class

The parameters that are received by the __new__ () method are:
The object of the class that is currently ready to be created;
The name of the class;
The collection of parent classes that the class inherits;
A collection of methods for the class;

The code is as follows:

Class Modelmetaclass (Type):
Def __new__ (CLS, name, bases, Attrs):
If name== ' Model ':
Return type.__new__ (CLS, name, bases, Attrs)
mappings = Dict ()
For K, V in Attrs.iteritems ():
If Isinstance (V, Field):
Print (' Found mapping:%s==>%s '% (k, v))
Mappings[k] = V
For k in Mappings.iterkeys ():
Attrs.pop (k)
attrs[' __table__ ' = name # Assuming the table name and class name match
attrs[' __mappings__ ' = mappings # Save the mappings of properties and columns
Return type.__new__ (CLS, name, bases, Attrs)
Class Model (Dict):
__metaclass__ = Modelmetaclass
def __init__ (self, **kw):
Super (Model, self). __init__ (**KW)
def __getattr__ (self, key):
return Self[key]
Except Keyerror:
Raise Attributeerror (R "Model ' object has no attribute '%s '"% key)
def __setattr__ (self, Key, value):
Self[key] = value
def save (self):
Fields = []
params = []
args = []
For K, V in Self.__mappings__.iteritems ():
Fields.Append (
Params.append ('? ')
Args.append (GetAttr (self, k, None))
sql = ' INSERT into%s (%s) '% (%s) '% (self.__table__, ', '. Join (Fields), ', '. Join (params))
Print (' sql:%s '% sql)
Print (' args:%s '% str (args))
Class Field (object):
def __init__ (self, Name, Column_type): = Name
Self.column_type = Column_type
def __str__ (self):
Return ' <%s:%s> '% (self.__class__.__name__,
Class Stringfield (Field):
def __init__ (self, name):
Super (Stringfield, self). __init__ (name, ' varchar (100) ')
Class Integerfield (Field):
def __init__ (self, name):
Super (Integerfield, self). __init__ (name, ' bigint ')
Class User (Model):
# define the property-to-column mappings for the class:
id = integerfield (' id ')
Name = Stringfield (' username ')
email = stringfield (' email ')
Password = Stringfield (' password ')
# Create an instance:
U = User (id=12345, name= ' Michael ', email= ' ', password= ' my-pwd ')
# Save to Database: ()

The output is as follows:

The code is as follows:

Found Model:user
Found Mapping:email ==>
Found Mapping:password ==>
Found Mapping:id ==>
Found Mapping:name ==>
Sql:insert into User (password,email,username,uid) VALUES (?,?,?,?)
ARGS: [' my-pwd ', ' ', ' Michael ', 12345]

SQLAlchemy Simple to use

The code is as follows:

From SQLAlchemy import Column, String, Create_engine
From Sqlalchemy.orm import Sessionmaker
From sqlalchemy.ext.declarative import declarative_base
# Create the base class for the object:
Base = Declarative_base ()
# define the User object:
Class User (Base):
# Name of the table:
__tablename__ = ' user '
# Structure of the table:
id = Column (String (), primary_key=true)
Name = Column (String (20))
# Initialize Database connection:
Engine = Create_engine (' mysql+mysqlconnector://root:password@localhost:3306/test ') # ' database type + database driver name://user name: password @ machine Address: Port number/database name '
# Create Dbsession Type:
Dbsession = Sessionmaker (bind=engine)
# Create a new User object:
New_user = User (id= ' 5 ', name= ' Bob ')
# Add to session:
Session.add (New_user)
# commit is saved to the database:
Session.commit ()
# Create a query, filter is the Where condition, and the last Call to one () returns a unique row, and all rows are returned if all () is called:
user = Session.query (user). Filter ( ' 5 '). One ()
# Close session:
Session.close ()

Wsgi simple use and simple use of web framework flask

The code is as follows:

From Wsgiref.simple_server import Make_server
def application (environ, start_response):
Start_response (' K OK ', [(' Content-type ', ' text/html ')])
Return '

Hello, web!.

# Create a server, the IP address is empty, the port is 8000, the handler function is application:
httpd = Make_server (", 8000, application)
Print "Serving HTTP on port 8000 ..."
# Start listening for HTTP requests:
Httpd.serve_forever ()

Understanding the WSGI framework, we found that: in fact, a Web App is to write a WSGI processing function that responds to each HTTP request.
But how to handle HTTP requests is not a problem, but the question is how to handle 100 different URLs.
One of the simplest and most earthy ideas is to remove the HTTP request information from the Environ variable and then judge it individually.

The code is as follows:

From flask import Flask
From Flask Import Request
App = Flask (__name__)
@app. Route ('/', methods=[' GET ', ' POST ')
Def home ():
Return '


@app. Route ('/signin ', methods=[' GET ')
Def signin_form ():
Return ""
@app. Route ('/signin ', methods=[' POST ')
def signin ():
# The form content needs to be read from the Request object:
If request.form[' username ']== ' admin ' and request.form[' password ']== ' password ':
Return '

Hello, admin!.

Return '

Bad username or password.

if __name__ = = ' __main__ ': ()

Format Display JSON

The code is as follows:

Print (Json.dumps (data, indent=4))
# or
Import Pprint
Pprint.pprint (data)

Implementing enumerations similar to those in Java or C

The code is as follows:

#!/usr/bin/env python
Import Itertools
Import Sys
Class Immutablemixin (object):
_inited = False
def __init__ (self):
self._inited = True
def __setattr__ (self, Key, value):
If self._inited:
Raise Exception ("Unsupported action")
Super (Immutablemixin, self). __setattr__ (key, value)
Class Enummixin (object):
def __iter__ (self):
For K, V in Itertools.imap (lambda x: (x, GetAttr (self, x)), dir (self):
If not K.startswith ('_'):
Yield V
Class _runnertype (Immutablemixin, enummixin):
Constant_for_duration = "Constant_for_duration"
If __name__== "__main__":
Print _runnertype.constant

Specify permissions when creating a file

The code is as follows:

Import OS
def write_to_file (path, Contents, Umask=none):
"" "Write the given contents to a file
:p Aram Path:destination File
:p Aram contents:desired contents of the file
:p Aram Umask:umask to set when creating the This file ('ll be reset)
If Umask:
Saved_umask = Os.umask (umask)
With open (path, ' W ') as F:
F.write (contents)
If Umask:
Os.umask (Saved_umask)
if __name__ = = ' __main__ ':
Write_to_file ('/home/kong/tmp ', ' Test ', 31)
# then you'll see a file was created with permission 640.
# warning:if The file already exists, its permission won't be changed.
# note:for file, default all permission are 666, and 777 for directory.

Multi-process concurrency execution

The code is as follows:

Import multiprocessing
Import time
Import OS
def run (flag):
Print "flag:%s, sleep 2s in run"% flag
Time.sleep (2)
Print "%s exist"% flag
Return flag
if __name__ = = ' __main__ ':
Pool = multiprocessing. Pool (3)
Iter_result = Pool.imap (Run, xrange (6))
Print "Sleep 5s\n\n"
Time.sleep (5)
For I in range (6):
result = (600)
except multiprocessing. Timeouterror as E:
Print result
Pool.close ()
Pool.join ()

Auto-fill function parameters at run time

The code is as follows:

Import Decorator
def default_from_global (Arg_name, Env_name):
Def default_from_global (F, *args, **kwargs):
Id_arg_index = F.func_code.co_varnames.index (arg_name)
args = List (args)
If Args[id_arg_index] is None:
Args[id_arg_index] = Get_global (env_name)
If not Args[id_arg_index]:
Print ("Missing argument:--% (arg_name) s"% {"Arg_name": Arg_name})
Return (1)
Return f (*args, **kwargs)
Return Decorator.decorator (Default_from_global)
# below is an adorner that can be used on functions that require automatic filling of parameters. Features are:
# If there is no deploy_id parameter for the transfer function, then get it from the environment variable (call the custom Get_global function)
with_default_deploy_id = Default_from_global (' deploy_id ', env_deployment)

Nested adorners

The validator function decorates the FUNC1,FUNC1 using the receive parameter (*arg, **kwargs), and Func1 also decorates func2 (in fact rally function in the scenario), gives Func2 the validators attribute, is a list of functions that receive the parameter config, clients, task. These functions eventually call func1, passing in Parameters (config, clients, task, *args, **kwargs), so func1 is defined when the parameter is (config, clients, task, *arg, **kwargs)
The end result is that FUNC2 has many decorators, each of which receives its own parameters and does some validation work.

The code is as follows:

Def validator (FN):
"" "Decorator that constructs a scenario validator from given function.
Decorated function should return Validationresult on error.
:p Aram Fn:function that performs validation
: returns:rally Scenario Validator
def wrap_given (*args, **kwargs):
"" "Dynamic validation decorator for scenario.
:p Aram args:the arguments of the decorator of the benchmark scenario
Ex. @my_decorator ("Arg1"), then args = (' arg1 ',)
:p Aram kwargs:the keyword arguments of the decorator of the scenario
Ex. @my_decorator (kwarg1= "Kwarg1"), then Kwargs = {"Kwarg1": "Kwarg1"}
def wrap_validator (config, clients, Task):
Return (FN (config, clients, task, *args, **kwargs) or
Validationresult ())
def wrap_scenario (scenario):
Wrap_validator.permission = GetAttr (FN, "permission",
Consts. Endpointpermission.user)
If not hasattr (scenario, "validators"):
Scenario.validators = []
Scenario.validators.append (Wrap_validator)
Return scenario
Return Wrap_scenario
Return Wrap_given

Some common uses of the inspect library

Inspect.getargspec (func) Gets the name and default value of the function parameter, returning a four-tuple (args, varargs, keywords, defaults), Where:
Args is a list of parameter names;
VarArgs and keywords are variable names for * and * * numbers;
Defaults is a list of default values for parameters;

Inspect.getcallargs (func[, *args][, **kwds]) bind function parameters. Returns the argument dictionary for the post-bind function.

Private properties and functions in Python

Python treats two or more variables that begin with an underscore character and that do not have two or an underscore ending as a private variable. A private variable is converted to a long format (public) before the code is generated, and this process is called "Private name mangling", such as the __private identifier in class A will be converted to _a__private, but when the class name is all underlined named, Python will no longer perform a rolling press. And, although called private variables, it is still possible to be accessed or modified (using _classname__membername), so, summarized as follows:

Whether it is a single underscore or a double-underlined member, it is hoped that external program developers do not directly use these member variables and these member functions, but the double underscore syntax can be more directly to avoid the wrong use, but if the _ Class name __ Member name can still be accessed. A single underline may be handy for dynamic debugging, so it might be better to use a single underline as long as the members of the project group are not directly using the member that starts with the underscore.

  • 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: 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.