date and time
object
timedelta
tzinfo
time
date
datetime
timedelta
>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
minutes=50, seconds=600) # adds up to 365 days
date
>>> from datetime import date
>>> today = date.today()
>>> today == date.fromtimestamp(time.time())
True
>>> my_birthday = date(today.year, 6, 24)
>>> if my_birthday < today:
… my_birthday = my_birthday.replace(year=today.year + 1)
>>> my_birthday
datetime.date(2008, 6, 24)
>>> time_to_birthday = abs(my_birthday - today)
>>> time_to_birthday.days
202
>>> from datetime import date
>>> d = date.fromordinal(730920) # 730920th day after 1. 1. 0001
>>> d
datetime.date(2002, 3, 11)
>>> t = d.timetuple()
>>> for i in t:
… print i
2002 # year
3 # month
11 # day
0
0
0
0 # weekday (0 = Monday)
70 # 70th day in the year
-1
>>> ic = d.isocalendar()
>>> for i in ic:
… print i
2002 # ISO year
11 # ISO week number
1 # ISO day number ( 1 = Monday )
>>> d.isoformat()
‘2002-03-11’
>>> d.strftime(“%d/%m/%y”)
‘11/03/02’
>>> d.strftime(“%A %d. %B %Y”)
‘Monday 11. March 2002’
>>> ‘The {1} is {0:%d}, the {2} is {0:%B}.’.format(d, “day”, “month”)
‘The day is 11, the month is March.
datetime
>>> from datetime import datetime, date, time
>>> # Using datetime.combine()
>>> d = date(2005, 7, 14)
>>> t = time(12, 30)
>>> datetime.combine(d, t)
datetime.datetime(2005, 7, 14, 12, 30)
>>> datetime.now()
datetime.datetime(2007, 12, 6, 16, 29, 43, 79043) # GMT +1
>>> datetime.utcnow()
datetime.datetime(2007, 12, 6, 15, 29, 43, 79060)
datetime.datetime(2006, 11, 21, 16, 30)
>>> # Using datetime.timetuple() to get tuple of all attributes
>>> tt = dt.timetuple()
>>> for it in tt:
… print it
## 2006 # year
11 # month
21 # day
16 # hour
30 # minute
0 # second
1 # weekday (0 = Monday)
325 # number of days since 1st January
-1 # dst - method tzinfo.dst() returned None
datetime.strptime is the main routine for parsing strings into datetimes.
It can handle all sorts of formats, with the format determined by a format string you give it:
>>> from datetime import datetime
>>> datetime_object = datetime.strptime(‘Jun 1 2005 1:33PM’, ‘%b %d %Y %I:%M%p’)
time
>>> from datetime import time, tzinfo, timedelta
>>> class GMT1(tzinfo):
… def utcoffset(self, dt):
… return timedelta(hours=1)
… def dst(self, dt):
… return timedelta(0)
… def tzname(self,dt):
… return “Europe/Prague”
…
>>> t = time(12, 10, 30, tzinfo=GMT1())
>>> t # doctest: +ELLIPSIS
datetime.time(12, 10, 30, tzinfo=)
>>> gmt = GMT1()
>>> t.isoformat()
‘12:10:30+01:00’
>>> t.dst()
datetime.timedelta(0)
>>> t.tzname()
‘Europe/Prague’
>>> t.strftime(“%H:%M:%S %Z”)
‘12:10:30 Europe/Prague’
>>> ‘The {} is {:%H:%M}.’.format(“time”, t)
‘The time is 12:10.’
>> time.sleep( 5 )
time.ctime()
time.ctime([ sec ])
default is using time.time(), equals to asctime(localtime(secs))
>> print “Start : %s” % time.ctime()
Start : Tue Feb 17 10:19:18 2013
timezone conversion
>>> utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)
>>> print(utc_dt)
2015-05-18 09:05:12.377316+00:00
>>> bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))
>>> print(bj_dt)
2015-05-18 17:05:12.377316+08:00
>>> tokyo_dt = utc_dt.astimezone(timezone(timedelta(hours=9)))
>>> print(tokyo_dt)
2015-05-18 18:05:12.377316+09:00
>>> tokyo_dt2 = bj_dt.astimezone(timezone(timedelta(hours=9)))
>>> print(tokyo_dt2)
2015-05-18 18:05:12.377316+09:00
file operation
read lines from a file
sample1
with open(fname) as f:
content = f.readlines()
content = [x.strip() for x in content]
sample2
lines = [line.rstrip(‘\n’) for line in open(‘filename’)]
sample3
with open(“file.txt”, “r”) as ins:
array = []
for line in ins:
array.append(line)
readline
## 通过readline输出,对于比较大的文件,这种占用内存比较小。
#coding:utf-8
f = open(‘poem.txt’,’r’)
result = list()
for line in open(‘poem.txt’):
line = f.readline()
print line
result.append(line)
print result
f.close()
open(‘result-readline.txt’, ‘w’).write(‘%s’ % ‘\n’.join(result))
file exists
import os.path
os.path.isfile(fname)
os.path.exists(file_path)
This returns True for both files and directories but you can instead
use os.path.isfile to test if it’s a file specifically. It follows symlinks.
get power status
import ctypes
from ctypes import wintypes
class SYSTEM_POWER_STATUS(ctypes.Structure):
fields = [
(‘ACLineStatus’, wintypes.BYTE),
(‘BatteryFlag’, wintypes.BYTE),
(‘BatteryLifePercent’, wintypes.BYTE),
(‘Reserved1’, wintypes.BYTE),
(‘BatteryLifeTime’, wintypes.DWORD),
(‘BatteryFullLifeTime’, wintypes.DWORD),
]
SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS)
GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus
GetSystemPowerStatus.argtypes = [SYSTEM_POWER_STATUS_P]
GetSystemPowerStatus.restype = wintypes.BOOL
status = SYSTEM_POWER_STATUS()
if not GetSystemPowerStatus(ctypes.pointer(status)):
raise ctypes.WinError()
print ‘ACLineStatus’, status.ACLineStatus
print ‘BatteryFlag’, status.BatteryFlag
print ‘BatteryLifePercent’, status.BatteryLifePercent
print ‘BatteryLifeTime’, status.BatteryLifeTime
print ‘BatteryFullLifeTime’, status.BatteryFullLifeTime
## On my system that prints this (basically meaning “desktop, plugged in”):
ACLineStatus 1
BatteryFlag -128
BatteryLifePercent -1
BatteryLifeTime 4294967295
BatteryFullLifeTime 4294967295
Exception
try:
with open(module.params[‘result_file’], ‘r’) as f:
result[‘results’] = json.load(f)
except IOError as e:
result[‘msg’] += ‘, and failed to open result file.’
result[‘rc’] = 1
finally:
module.exit_json(**result)
print Unicode string
print u”\u00d7 \u00f7” # multiply & divide mark
The Zen of Python, by Tim Peters
s = “”“Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera’g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar– naq cersrenoyl bayl bar –boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh’er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna evtug abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg’f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn – yrg’f qb zber bs gubfr!”“”
d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c)
print(“”.join([d.get(c, c) for c in s]))
url to filename
import urlparse, os
url = “http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg”
a = urlparse.urlparse(url)
a.path
‘/kyle/09-09-201315-47-571378756077.jpg’
os.path.basename(a.path)
‘09-09-201315-47-571378756077.jpg’
## one line
os.path.basename(urlparse.urlparse(url).path)
lambda
t = lambda x:x*2
print t(3)
>>6
create lambda inside a loop
You either have to capture the variable using default assignments
lambdas_list = [ lambda i=o: i.some_var for o in obj_list ]
or, use closures to capture the variable
lambdas_list = [ (lambda a: lambda: a.some_var)(o) for o in obj_list ]
List Comprehension:
my_list = [x for x in my_list if x.attribute == value]
Or lambda+filter:
my_list = filter(lambda x: x.attribute == value, my_list)
itemgetter
import operator
getseconditem = operator.itemgetter(1)
ls=[‘1’,’2’,’3’]
print getseconditem(ls)
>>2
print operator.itemgetter(1,3,5)(‘abcdefg’)
>>(‘b’, ‘d’, ‘f’)
enumerate
list(enumerate([‘a’, ‘b’, ‘c’, ‘d’], start=1)
>>[(1, ‘a’), (2, ‘b’), (3, ‘c’), (4, ‘d’)]
deeper recursive calls
You can increment the stack depth allowed - with this, deeper recursive calls will be possible, like this:
import sys
sys.setrecursionlimit(10000) # 10000 is an example, try with different values (+ 1 2 3 4)
urlencode/decode
urlencode
>>> from urllib import urlencode
>>> data = {
… ‘a’: ‘test’,
… ‘name’: ‘xxxxxxx’
… }
>>> print urlencode(data)
a=test&name=%C4%A7%CA%DE
>>> from urllib import quote
>>> quote(‘xxxxx’)
‘%C4%A7%CA%DE’
urldecode
>>> from urllib import unquote
>>> unquote(‘%C4%A7%CA%DE’)
‘\xc4\xa7\xca\xde’
>>> print unquote(‘%C4%A7%CA%DE’)
unicode string conversion
1 #将Unicode转换成普通的Python字符串:”编码(encode)”
2 unicodestring = u”Hello world”
3 utf8string = unicodestring.encode(“utf-8”)
4 asciistring = unicodestring.encode(“ascii”)
5 isostring = unicodestring.encode(“ISO-8859-1”)
6 utf16string = unicodestring.encode(“utf-16”)
7
8
9 #将普通的Python字符串转换成Unicode: “解码(decode)”
10 plainstring1 = unicode(utf8string, “utf-8”)
11 plainstring2 = unicode(asciistring, “ascii”)
12 plainstring3 = unicode(isostring, “ISO-8859-1”)
13 plainstring4 = unicode(utf16string, “utf-16”)
check half-width, full-width
unicodedata.east_asian_width(unicode(segments[idx]))
curses
The curses library maintains a finite number of color pairs, containing a foreground (or text) color and a background color. You can get the attribute value corresponding to a color pair with the color_pair() function; this can be bitwise-OR’ed with other attributes such as A_REVERSE, but again, such combinations are not guaranteed to work on all terminals.
An example, which displays a line of text using color pair 1:
stdscr.addstr(“Pretty text”, curses.color_pair(1))
stdscr.refresh()
As I said before, a color pair consists of a foreground and background color. The init_pair(n, f, b) function changes the definition of color pair n, to foreground color f and background color b. Color pair 0 is hard-wired to white on black, and cannot be changed.
Colors are numbered, and start_color() initializes 8 basic colors when it activates color mode. They are: 0:black, 1:red, 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and 7:white. The curses module defines named constants for each of these colors: curses.COLOR_BLACK, curses.COLOR_RED, and so forth.
Let’s put all this together. To change color 1 to red text on a white background, you would call:
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
When you change a color pair, any text already displayed using that color pair will change to the new colors. You can also display new text in this color with:
stdscr.addstr(0,0, “RED ALERT!”, curses.color_pair(1))
Very fancy terminals can change the definitions of the actual colors to a given RGB value. This lets you change color 1, which is usually red, to purple or blue or any other color you like. Unfortunately, the Linux console doesn’t support this, so I’m unable to try it out, and can’t provide any examples. You can check if your terminal can do this by calling can_change_color(), which returns True if the capability is there. If you’re lucky enough to have such a talented terminal, consult your system’s man pages for more information.
import curses
def main(stdscr):
stdscr.keypad(1)
c = None
while c != curses.KEY_RIGHT:
c = stdscr.getch()
stdscr.addstr(0, 0, “%3d” % c)
stdscr.refresh()
pad = curses.newpad(20, 20)
## setting window.keypad(1) makes sure curses interprets escape sequences for you
pad.keypad(1)
while True:
c = pad.getch()
pad.addstr(0, 0, “%3d” % c)
pad.refresh(0, 0, 1, 0, 20, 20)
if __name__ == ‘__main__’:
curses.wrapper(main)
coordinate system
the coordinate system used in curses is unusual. Coordinates are always passed in the order y,x,
and the top-left corner of a window is coordinate (0,0)
Your application can determine the size of the screen by using the curses.LINES and curses.COLS
variables to obtain the y and x sizes. Legal coordinates will then extend from (0,0) to
(curses.LINES - 1, curses.COLS - 1)
pad
A pad is a special case of a window; it can be larger than the actual display screen, and only a
portion of the pad displayed at a time. Creating a pad requires the pad’s height and width,
while refreshing a pad requires giving the coordinates of the on-screen area where a subsection
of the pad will be displayed.
pad = curses.newpad(100, 100)
for y in range(0, 99):
for x in range(0, 99):
pad.addch(y,x, ord(‘a’) + (x*x+y*y) % 26)
pad.refresh( 0,0, 5,5, 20,75)
refresh / noutrefresh / doupdate
If you have multiple windows and pads on screen there is a more efficient way to update the screen and prevent annoying screen flicker as each part of the screen gets updated. refresh() actually does two things:
Calls the noutrefresh() method of each window to update an underlying data structure representing the desired state of the screen.
Calls the function doupdate() function to change the physical screen to match the desired state recorded in the data structure.
Instead you can call noutrefresh() on a number of windows to update the data structure, and then call doupdate() to update the screen.
erase a line to the end
To delete to the EOL (End Of Line) use window.clrtoeol():
Example:
import curses
window = curses.initscr()
window.clrtoeol()
window.refresh()
textpad
textbox
import curses
from curses.textpad import Textbox, rectangle
def main(stdscr):
stdscr.addstr(0, 0, “Enter IM message: (hit Ctrl-G to send)”)
editwin = curses.newwin(5,30, 2,1)
rectangle(stdscr, 1,0, 1+5+1, 1+30+1)
stdscr.refresh()
box = Textbox(editwin)
box.edit()
message = box.gather()
curses.wrapper(main)
OO
property
## using property() function
class C(object):
def __init__(self):
self._x = None
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, “I’m the ‘x’ property.”)
## using decorator
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
“”“I’m the ‘x’ property.”“”
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
static method & class method
@staticmethod and @classmethod
作者:李保银
链接:https://www.zhihu.com/question/20021164/answer/18224953
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Kls(object):
def __init__(self, data):
self.data = data
def printd(self):
print(self.data)
@staticmethod
def smethod(*arg):
print(‘Static:’, arg)
@classmethod
def cmethod(*arg):
print(‘Class:’, arg)
>>> ik = Kls(23)
>>> ik.printd()
23
>>> ik.smethod()
Static: ()
>>> ik.cmethod()
Class: (,)
>>> Kls.printd()
TypeError: unbound method printd() must be called with Kls instance as first argument (got nothing instead)
>>> Kls.smethod()
Static: ()
>>> Kls.cmethod()
Class: (,)
constructor, parent constructor
class A(object):
def __init__(self):
self.aa = 1
class B(A):
def __init__(self):
super(B, self).__init__()
self.bb = 2
b = B()
print b.aa
print b.bb
## object is manditory… new style class
hide input text instead of using raw_input
>>> import getpass
>>> getpass.getpass(“Password: “)
Password:
‘trustno1’
fileinput
$ python
Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import fileinput
>>>
>>> for line in fileinput.input(“test.txt”, inplace=True):
… print “%d: %s” % (fileinput.filelineno(), line),
…
>>>
1164 22:39:22 xunyang@hpds09 ~/src/testauto_conf
$ cat test.txt
1: 10
2: 11
3: 12
4: 13
5: 14
6: 15
7: 16
8: 17
9: 18
10: 19
11: 20
a const implementation
def constant(f):
def fset(self, value):
raise TypeError
def fget(self):
return f()
return property(fget, fset)
class _Const(object):
@constant
def FOO():
return 0xBAADFACE
@constant
def BAR():
return 0xDEADBEEF
CONST = _Const()
print CONST.FOO
##3131964110
CONST.FOO = 0
##Traceback (most recent call last):
## …
## CONST.FOO = 0
##TypeError: None
type() and isinstance()
class Vehicle:
pass
class Truck(Vehicle):
pass
in this case, a truck object is a Vehicle, but you’ll get this:
isinstance(Vehicle(), Vehicle) # returns True
type(Vehicle()) == Vehicle # returns True
isinstance(Truck(), Vehicle) # returns True
type(Truck()) == Vehicle # returns False, and this probably won’t
about super()
I’m trying to understand super()
The reason we use super is so that child classes that may be using cooperative multiple inheritance will call the correct next parent class function in the Method Resolution Order (MRO).
In Python 3, we can call it like this:
class ChildB(Base):
def __init__(self):
super().__init__()
In Python 2, we are required to use it like this:
super(ChildB, self).__init__()
Without super, you are limited in your ability to use multiple inheritance:
Base.__init__(self) # Avoid this.
“What difference is there actually in this code?:”
class ChildA(Base):
def __init__(self):
Base.__init__(self)
class ChildB(Base):
def __init__(self):
super(ChildB, self).__init__()
The primary difference in this code is that you get a layer of indirection in the __init__ with super, which uses the current class to determine the next class’s __init__ to look up in the MRO.
I illustrate this difference in an answer at the canonical question (https://stackoverflow.com/a/33469090/541136), How to use ‘super’ in Python?, which demonstrates dependency injection and cooperative multiple inheritance.
dictionary
Iterate a dictionary
for key, value in d.iteritems():
pass
has_key
in is definitely more pythonic.
In fact has_key() was removed in Python 3.x.
string
Substitute multiple whitespace with single whitespace in Python
1.
’ ‘.join(mystring.split())
2.
import re
re.sub( ‘\s+’, ’ ‘, mystring ).strip()
lowercase/uppercase
str.lower()
str.upper()
rsplit
filename.rsplit(‘.’, 1)[1] == “txt”
call external programs
Here’s a summary of the ways to call external programs and the advantages and disadvantages of each:
os.system(“some_command with args”) passes the command and arguments to your system’s shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. For example:
os.system(“some_command < input_file | another_command > output_file”)
However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, etc. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs. See the documentation.
stream = os.popen(“some_command with args”) will do the same thing as os.system except that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pass everything as a string, then your command is passed to the shell; if you pass them as a list then you don’t need to worry about escaping anything. See the documentation.
The Popen class of the subprocess module. This is intended as a replacement for os.popen but has the downside of being slightly more complicated by virtue of being so comprehensive. For example, you’d say:
print subprocess.Popen(“echo Hello World”, shell=True, stdout=subprocess.PIPE).stdout.read()
instead of:
print os.popen(“echo Hello World”).read()
but it is nice to have all of the options there in one unified class instead of 4 different popen functions. See the documentation.
The call function from the subprocess module. This is basically just like the Popen class and takes all of the same arguments, but it simply waits until the command completes and gives you the return code. For example:
return_code = subprocess.call(“echo Hello World”, shell=True)
See the documentation.
If you’re on Python 3.5 or later, you can use the new subprocess.run function, which is a lot like the above but even more flexible and returns a CompletedProcess object when the command finishes executing.
The os module also has all of the fork/exec/spawn functions that you’d have in a C program, but I don’t recommend using them directly.
The subprocess module should probably be what you use.
Finally please be aware that for all methods where you pass the final command to be executed by the shell as a string and you are responsible for escaping it. There are serious security implications if any part of the string that you pass can not be fully trusted. For example, if a user is entering some/any part of the string. If you are unsure, only use these methods with constants. To give you a hint of the implications consider this code:
print subprocess.Popen(“echo %s ” % user_input, stdout=PIPE).stdout.read()
and imagine that the user enters “my mama didnt love me && rm -rf /”.
popen
import subprocess
import sys
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
while True:
out = process.stdout.read(1)
if out == ” and process.poll() != None:
break
if out != ”:
sys.stdout.write(out)
sys.stdout.flush()
list
how to sort a list of objects based on an attribute of the objects?
ut.sort(key=lambda x: x.count, reverse=True)
newlist = sorted(ut, key=lambda x: x.count, reverse=True)
timestamp
>>> import time
>>> t = time.time()
>>> print t
1436428326.76
>>> print int(t)
1436428326
>>> import time
>>> now = int(round(time.time()*1000))
>>> now02 = time.strftime(‘%Y-%m-%d %H:%M:%S’,time.localtime(now/1000))
>>> now02
‘2017-11-07 16:47:14’
source in environment variables
def set_environment():
sourcing = ‘source /apps/configproxy.env’
logger = logging.getLogger()
logger.info(‘source environment file: %s’, sourcing.split()[-1])
dump = ‘/usr/bin/env python -c “import os,pickle;print pickle.dumps(os.environ)”’
try:
penv = os.popen(‘%s && %s’ % (sourcing, dump))
os.environ = pickle.loads(penv.read())
except Exception, err:
logger.error(‘Failed to source %s, error: %s’, sourcing, str(err))
sys.exit(0)
os.environ.get(‘CP_BIND_DN’)
one line to debug
python 2x:
with open(‘/tmp/uuc_debug’, ‘a’) as f: print >> f, aggr_config
stdout
f_handler=open(‘out.log’, ‘w’)
sys.stdout=f_handler
print(‘hello’)
## if you want to use console, you need preserve it
__console__=sys.stdout
sys.stdout=__console__
ip validation
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError:
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count(‘.’) == 3
except socket.error:
return False
return True
def is_valid_ipv6_address(address):
try:
socket.inet_pton(socket.AF_INET6, address)
except socket.error:
return False
return True
troubleshooting
Cannot uninstall ‘numpy’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
sudo pip2 install –upgrade –ignore-installed –install-option ‘–install-data=/usr/local’ numpy
unmet dependencies
One possible cause of unmet dependencies could be corrupted package database, and/or some packages weren’t installed properly. To fix this problem, hit Alt+Ctrl+T to open terminal and try to run one of the following commands:
sudo apt-get clean
or,
sudo apt-get autoclean
apt-get clean clears out the local repository of retrieved package files (the .deb files). It removes everything but the lock file from var/cache/apt/archives and var/cache/apt/archives/partial. apt-get autoclean clears out the local repository of retrieved package files, but unlike apt-get clean, it only removes package files that can no longer be downloaded, and are largely useless.
One of the most basic fixes to resolve dependencies problems is to run:
sudo apt-get -f install
The -f here stands for “fix broken”. Apt will attempt to correct broken dependencies. If you manually installed a package that had unmet dependencies, apt-get will install those dependencies, if possible, otherwise it may simply remove the package that you installed in order to resolve the problem.
Then run:
sudo dpkg –configure -a
Then run this again:
sudo apt-get -f install
If the output is:
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
That means it failed.
Next solution is to run:
sudo apt-get -u dist-upgrade
If it shows any held packages, it is best to eliminate them. Packages are held because of dependency conflicts that apt cannot resolve. Try this command to find and repair the conflicts:
sudo apt-get -o Debug::pkgProblemResolver=yes dist-upgrade
If it cannot fix the conflicts, it will exit with:
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
Delete the held packages one by one, running dist-upgrade each time, until there are no more held packages. Then reinstall any needed packages. Be sure to use the –dry-run option, so that you are fully informed of consequences:
sudo apt-get remove –dry-run package-name
Since removing the package you are trying to install may not be ideal, you might also try finding a repository that has the packages you need to satisfy the dependencies.
No module named ‘pip’ on Ubuntu
root@ans120:~# pip
Traceback (most recent call last):
File “/usr/bin/pip”, line 9, in
from pip import main
ImportError: No module named ‘pip’
root@ans120:~#
root@ans120:~# python -m ensurepip
/usr/bin/python: No module named ensurepip
root@ans120:~# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1841k 100 1841k 0 0 3030k 0 –:–:– –:–:– –:–:– 3028k
root@ans120:~# python get-pip.py –force-reinstall
DEPRECATION: Python 3.5 reached the end of its life on September 13th, 2020. Please upgrade your Python as Python 3.5 is no longer maintained. pip 21.0 will drop support for Python 3.5 in January 2021. pip 21.0 will remove support for this functionality.
Collecting pip
Downloading pip-20.2.3-py2.py3-none-any.whl (1.5 MB) | ████████████████████████████████ | 1.5 MB 8.4 MB/s
Collecting setuptools
Downloading setuptools-50.3.0-py3-none-any.whl (785 kB) | ████████████████████████████████ | 785 kB 12.0 MB/s
Collecting wheel |
Downloading wheel-0.35.1-py2.py3-none-any.whl (33 kB) |
Installing collected packages: pip, setuptools, wheel |
Successfully installed pip-20.2.3 setuptools-50.3.0 wheel-0.35.1 |
root@ans120:~# pip show pip |
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. |
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. |
To avoid this problem you can invoke Python with ‘-m pip’ instead of running pip directly. |
DEPRECATION: Python 3.5 reached the end of its life on September 13th, 2020. Please upgrade your Python as Python 3.5 is no longer maintained. pip 21.0 will drop support for Python 3.5 in January 2021. pip 21.0 will remove support for this functionality. |
Name: pip |
Version: 20.2.3 |
Summary: The PyPA recommended tool for installing Python packages. |
Home-page: https://pip.pypa.io/ |
Author: The pip developers |
Author-email: distutils-sig@python.org |
License: MIT |
Location: /usr/local/lib/python3.5/dist-packages |
Requires: |
Required-by: |
root@ans120:~# |
pip upgrade issue
root@t761:~# pip install flask
Traceback (most recent call last):
File “/usr/bin/pip”, line 9, in
from pip import main
ImportError: cannot import name main
python3 - TypeError: a bytes-like object is required, not ‘str’
Problem:
TypeError: a bytes-like object is required, not ‘str’
解决思路
问题出在python3.5和Python2.7在套接字返回值解码上有区别:
python bytes和str两种类型可以通过函数encode()和decode()相互转换,
str→bytes:encode()方法。str通过encode()方法可以转换为bytes。
bytes→str:decode()方法。如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法。
解决方法
将
line.strip().split(“,”)
改为
line.decode().strip().split(“,”)
大功告成!
directory
filter(os.path.isdir, [os.path.join(d, o) for o in os.listdir(d)])
modules
base64
import base64
s = ‘anystring’
a = base64.b64encode(s)
print a
ztLKx9fWt/u0rg==
print base64.b64decode(a)
configparser
ConfigParser – Work with configuration files
And this sample program exercies some of the methods for looking at the configuration data, including sections(), options(), and items().
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read(‘multisection.ini’)
for section_name in parser.sections():
print ‘Section:’, section_name
print ’ Options:’, parser.options(section_name)
for name, value in parser.items(section_name):
print ’ %s = %s’ % (name, value)
Both sections() and options() return lists of strings, while items() returns a list of tuples containing the name-value pairs.
$ python ConfigParser_structure.py
Section: bug_tracker
Options: [‘url’, ‘username’, ‘password’]
url = http://localhost:8080/bugs/
username = dhellmann
password = SECRET
Section: wiki
Options: [‘url’, ‘username’, ‘password’]
url = http://localhost:8080/wiki/
username = dhellmann
password = SECRET
json
write to json format
#!/usr/bin/python
import json
data = [ { ‘a’ : 1, ‘b’ : 2, ‘c’ : 3, ‘d’ : 4, ‘e’ : 5 } ]
json = json.dumps(data)
print json
with open(“data_file.json”, “w”) as write_file:
json.dump(data, write_file)
load from string
#!/usr/bin/python
import json
jsonData = ‘{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}’;
text = json.loads(jsonData)
print text
logging
def init_logging():
formatter = logging.Formatter(
fmt=’%(asctime)s %(levelname)s: %(message)s’,
datefmt=’%Y-%m-%d %H:%M:%S’
)
logfile = logging.handlers.RotatingFileHandler(
filename=’/tmp/%s.log’%(os.path.basename(sys.argv[0])),
mode=’w’
)
logfile.setLevel(logging.DEBUG)
logfile.setFormatter(formatter)
stdout = logging.StreamHandler(sys.stdout)
stdout.setLevel(logging.INFO)
stdout.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(logfile)
logger.addHandler(stdout)
logger.setLevel(logging.NOTSET)
logger.info(“logging is initialized”)
def check_args():
logger = logging.getLogger()
logger.info(“check arguments [%s]”, ’ ‘.join(sys.argv))
pandas
stock data from tushare.
series
dataframe
df = pd.read_csv(‘uk_rain_2014.csv’, header=0)
df.head(5)
df.tail(5)
df.columns = [‘water_year’,’rain_octsep’, ‘outflow_octsep’,
‘rain_decfeb’, ‘outflow_decfeb’, ‘rain_junaug’, ‘outflow_junaug’]
df.head(5)
len(df)
pd.options.display.float_format = ‘{:,.3f}’.format # Limit output to 3 decimal places.
df.describe()
>>> df.describe()
open high close low volume price_change p_change ma5 ma10 ma20 v_ma5 v_ma10 v_ma20
count 487.000000 487.000000 487.000000 487.000000 487.000000 487.000000 487.000000 487.000000 487.000000 487.000000 487.000000 487.000000 487.000000
mean 32.371027 32.799733 32.386899 31.983470 86869.068809 -0.004538 0.007002 32.406474 32.444585 32.510698 86838.101643 86888.694271 86924.623511
std 3.712862 3.770071 3.702324 3.666154 65726.843246 0.640273 1.956812 3.683297 3.672490 3.640580 51369.796250 46052.485323 40187.334349
min 24.800000 25.420000 24.930000 24.280000 41.000000 -3.530000 -10.010000 25.208000 25.386000 26.168000 26116.510000 29660.560000 35158.410000
25% 28.545000 28.805000 28.560000 28.200000 49431.135000 -0.285000 -0.840000 28.554000 28.629000 28.607000 54720.660000 56253.390000 58617.605000
50% 33.300000 33.850000 33.450000 32.870000 67802.340000 -0.030000 -0.110000 33.546000 33.722000 33.825000 70763.060000 72763.910000 71660.300000
75% 35.765000 36.100000 35.740000 35.385000 101584.630000 0.240000 0.785000 35.720000 35.767000 35.624000 105000.790000 104883.175000 102843.850000
max 39.400000 40.000000 39.490000 39.100000 632971.190000 3.280000 10.000000 39.490000 39.490000 39.490000 337676.620000 251308.710000 217560.690000
df[‘rain_octsep’]
df.rain_octsep
df.rain_octsep < 1000 # Or df[‘rain_octsep’] < 10000
>>> df.open < 30
2016-09-02 False
2016-09-01 False
2016-08-31 False
2016-08-30 False
2016-08-29 False
Name: open, Length: 487, dtype: bool
df[df.rain_octsep < 1000]
df[(df.rain_octsep < 1000) & (df.outflow_octsep < 4000)] # Can’t use the keyword ‘and’
df[df.water_year.str.startswith(‘199’)]
df.iloc[30]
df = df.set_index([‘water_year’])
df.head(5)
df.loc[‘2000/01’]
df.sort_index(ascending=False).head(5) #inplace=True to apple the sorting in places
df = df.reset_index(‘water_year’)
df.head(5)
def base_year(year):
base_year = year[:4]
base_year= pd.to_datetime(base_year).year
return base_year
df[‘year’] = df.water_year.apply(base_year)
df.head(5)
#Manipulating structure (groupby, unstack, pivot)
df.groupby(df.year // 10 *10).max()
decade_rain = df.groupby([df.year // 10 * 10, df.rain_octsep // 1000 * 1000])‘outflow_octsep’, ‘outflow_decfeb’, ‘outflow_junaug’.mean()
decade_rain
decade_rain.unstack(0)
#Pivoting
#does set_index, sort_index and unstack in a row
high_rain.pivot(‘year’, ‘rain_octsep’)‘outflow_octsep’, ‘outflow_decfeb’, ‘outflow_junaug’.fillna(”)
rain_jpn = pd.read_csv(‘jpn_rain.csv’)
rain_jpn.columns = [‘year’, ‘jpn_rainfall’]
uk_jpn_rain = df.merge(rain_jpn, on=’year’)
uk_jpn_rain.head(5)
paramiko
Paramiko is a Python (2.7, 3.4+) implementation of the SSHv2 protocol, providing both client and server functionality.
random
simple example
>>> random.random() # Random float x, 0.0 <= x < 1.0
0.37444887175646646
>>> random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0
1.1800146073117523
>>> random.randint(1, 10) # Integer from 1 to 10, endpoints included
7
>>> random.randrange(0, 101, 2) # Even integer from 0 to 100
26
>>> random.choice(‘abcdefghij’) # Choose a random element
‘c’
>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]
>>> random.sample([1, 2, 3, 4, 5], 3) # Choose 3 elements
[4, 1, 5]
Example2
import random
class Shape(object):
types = []
def factory(type):
class Circle(Shape):
def draw(self): print(“Circle.draw”)
class Square(Shape):
def draw(self): print(“Square.draw”)
if type == “Circle”: return Circle()
if type == “Square”: return Square()
assert 0, “Bad shape creation: ” + type
def shapeNameGen(n):
for i in range(n):
yield factory(random.choice([“Circle”, “Square”]))
for x in shapeNameGen(7):
x.draw()
re
Python 2.2.2 (#1, Feb 10 2003, 12:57:01)
>>> import re
>>> p = re.compile(‘[a-z]+’)
>>> p #doctest: +ELLIPSIS
>>> p = re.compile(‘(a(b)c)d’)
>>> m = p.match(‘abcd’)
>>> m.group(0)
‘abcd’
>>> m.group(1)
‘abc’
>>> m.group(2)
‘b’
identify group with group names
>>> setting_re = re.compile(r’(([ ]{0,3}[^ \t]+)+)[\t ]+(?P[^ \t]{1}.+)’)
>>> aa = setting_re.match(‘aa bb vvv’)
>>> aa.group(‘val’)
‘vvv’
online test
https://regex101.com/r/xT0oB0/1
regex(‘^(([^,= "]+=[^,"]+)(,[^,= "]+=[^,"]+)*)$’)
>>> r=re.compile(r’^((([^,= "]+=[^,"]+)(,[^,= "]+=[^,"]+)*)|)$’)
>>> r.match(‘c=a,’)
>>> r.match(‘c=a,a’)
>>> r.match(‘c=a,a=’)
>>> r.match(‘c=a,a=u’)
>>>
r=re.compile(
r’^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}?’
r’(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$’
,re.IGNORECASE
)
r.match(“1.1.1.1”)
re.compile(‘^(?:(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}|(?=(?:[A-F0-9]{0,4}:){0,7}?[A-F0-9]{0,4}$)(([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}|:)?|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7})$’, re.IGNORECASE)
r=re.compile(
r’^(?:(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}|(?=(?:[A-F0-9]{0,4}:){0,7}?’
r’[A-F0-9]{0,4}$)(([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}|:)?’
r’|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7})$’
,re.IGNORECASE
)
r.match(“c10d:100::24”)
zip
import zipfile
with zipfile.ZipFile(“file.zip”,”r”) as zip_ref:
zip_ref.extractall(“targetdir”)
utilities / tools
pip
[root@testvnf ~]# pip
Usage:
pip [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
upgrade pip or other module
python -m pip install –upgrade pip
or:
pip install –upgrade pip
pip install –upgrade robotframework-sshlibrary
specify module version in pip
pip install robotframework==3.0
pip search
[root@testvnf ~]# pip search pexpect
pexpect (4.6.0) - Pexpect allows easy control of interactive console applications.
pexpect-serial (0.0.4) - pexpect with pyserial
pexpect-nm (2.6) - Pexpect is a pure Python Expect. It allows easy control of other applications.
pexpect-u (2.5.1) - Pexpect is a pure Python Expect. It allows easy control of other applications.
pytest-docker-pexpect (0.5) - pytest plugin for writing functional tests with pexpect and docker
virtualenv
[root@typhoon ~]# virtualenv
usage: virtualenv [–version] [–with-traceback] [-v | -q] [–app-data APP_DATA] [–clear-app-data] [–discovery {builtin}] [-p py] [–creator {builtin,cpython2-posix}] [–seeder {app-data,pip}] [–no-seed] [–activators comma_sep_list]
[–clear] [–system-site-packages] [–symlinks | –copies] [–no-download | –download] [–extra-search-dir d [d …]] [–pip version] [–setuptools version] [–wheel version] [–no-pip] [–no-setuptools] [–no-wheel]
[–symlink-app-data] [–prompt prompt] [-h]
dest
virtualenv: error: too few arguments
(ansible-2.7.5) [root@typhoon ~]#
You have new mail in /var/spool/mail/root
(ansible-2.7.5) [root@typhoon ~]# ansible –version
root.ansible_virtualenvs/ansible-2.7.5/lib64/python2.7/site-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
utils.PersistentlyDeprecated2018,
ansible 2.7.5
config file = None
configured module search path = [u’root.ansible/plugins/modules’, u’usr/share/ansible/plugins/modules’]
ansible python module location = /root.ansible_virtualenvs/ansible-2.7.5/lib/python2.7/site-packages/ansible
executable location = root.ansible_virtualenvs/ansible-2.7.5/bin/ansible
python version = 2.7.5 (default, Apr 10 2015, 08:09:05) [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)]
(ansible-2.7.5) [root@typhoon ~]#
virtualenv -p python “$ANSIBLES/ansible-2.7.5”
source root.ansible_virtualenvs/ansible-2.7.5/bin/activate
pip install ansible==2.7.5
(ansible-2.7.5) [root@typhoon ~]# deactivate
[root@typhoon ~]# ansible –version
/usr/lib64/python2.7/site-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a 2.7.x release that supports hmac.compare_digest as soon as possible.
utils.PersistentlyDeprecated2018,
ansible 2.7.10
config file = None
configured module search path = [u’root.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Apr 10 2015, 08:09:05) [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)]
You have new mail in /var/spool/mail/root
[root@typhoon ~]#
pydoc
alias pydoc 2>/dev/null >/dev/null && unalias pydoc
pydoc () {
python -m pydoc “$@”
}
other
string format (py3)
Examples
Align right:
>>> ‘{:>15}’.format(‘Python’)
’ Python’
>>>
Align left:
>>> ‘{:15}’.format(‘Python’)
‘Python ’
>>>
By argument:
In the previous example, the value ‘15’ is encoded as part of the format string. It is also possible to supply such values as an argument.
>>> ‘{:
‘Python ’
>>>
In the following example we have used ‘*’ as a padding character.
>>> ‘{:*<15}’.format(‘Python’)
‘Python*********’
>>>
Align center:
>>> ‘{:^16}’.format(‘Python’)
’ Python ’
>>>
Truncating long strings:
In the following example, we have truncated ten characters from the left side of a specified string.
>>> ‘{:.10}’.format(‘Python Tutorial’)
‘Python Tut’
>>>
By argument:
>>> ‘{:.{}}’.format(‘Python Tutorial’, 10)
‘Python Tut’
>>>
Combining truncating and padding
In the following example, we have combined truncating and padding.
>>> ‘{:10.10}’.format(‘Python’)
‘Python ’
>>>
Numbers:
Integers:
>>> ‘{:d}’.format(24)
‘24’
>>>
Floats:
>>> ‘{:f}’.format(5.12345678123)
‘5.123457’
>>>
Padding numbers:
Similar to strings numbers.
Example-1:
>>> ‘{:5d}’.format(24)
’ 24’
>>>
The padding value represents the length of the complete output for floating points. In the following example ‘{:05.2f}’ will display the float using five characters with two digits after the decimal point.
Example-2:
>>> ‘{:05.2f}’.format(5.12345678123)
‘05.12’
>>>
Signed numbers:
By default only negative numbers are prefixed with a sign, but you can display numbers prefixed with the positive sign also.
Example-1:
>>> ‘{:+d}’.format(24)
‘+24’
>>>
You can use a space character to indicate that negative numbers (should be prefixed with a minus symbol) and a leading space should be used for positive numbers.
Example-2:
>>> ‘{: d}’.format((- 24))
‘-24’
>>>
Example-3:
>>> ‘{: d}’.format(24)
’ 24’
>>>
You can control the position of the sign symbol relative to the padding.
Example-4:
>>> ‘{:=6d}’.format((- 24))
‘- 24’
>>>
Named placeholders:
Both formatting styles support named placeholders. Here is an example:
>>> data = {‘first’: ‘Place’, ‘last’: ‘Holder!’}
>>> ‘{first} {last}’.format(**data)
‘Place Holder!’
>>>
.format() method can accept keyword arguments.
>>> ‘{first} {last}’.format(first=’Place’, last=’Holder!’)
‘Place Holder!’
>>>
Datetime:
You can format and print datetime object as per your requirement.
>>> from datetime import datetime
>>> ‘{:%Y-%m-%d %H:%M}’.format(datetime(2016, 7, 26, 3, 57))
‘2016-07-26 03:57’
>>>
file extname
Yes. Use os.path.splitext(see Python 2.X documentation or Python 3.X documentation):
>>> import os
>>> filename, file_extension = os.path.splitext(‘/path/to/somefile.ext’)
>>> filename
‘/path/to/somefile’
>>> file_extension
‘.ext’
Unlike most manual string-splitting attempts, os.path.splitext will correctly treat /a/b.c/d as having no extension instead of having extension .c/d, and it will treat .bashrc as having no extension instead of having extension .bashrc:
>>> os.path.splitext(‘/a/b.c/d’)
(‘/a/b.c/d’, ”)
>>> os.path.splitext(‘.bashrc’)
(‘.bashrc’, ”)
aaa
def main():
return 0
if __name__ == “__main__”:
exit(main())
__metaclass__