Related: http://massivetechinterview.blogspot.com/2015/09/python-misc.html
巧用Google Fire简化Python命令行程序
https://stackoverflow.com/questions/1285911/how-do-i-check-that-multiple-keys-are-in-a-dict-in-a-single-pass
with
https://stackoverflow.com/questions/6130374/empty-set-literal
https://realpython.com/python-string-formatting/
#1 “Old Style” String Formatting (% operator)
'Hey %(name)s, there is a 0x%(errno)x error!' % {
... "name": name, "errno": errno }
#2 “New Style” String Formatting (str.format)
#3 String Interpolation / f-Strings (Python 3.6+)
https://stackoverflow.com/questions/1712227/how-to-get-the-number-of-elements-in-a-list-in-python
https://stackoverflow.com/questions/53513/how-do-i-check-if-a-list-is-empty/45778282#45778282
http://www.raychase.net/4881
讲到 pip 必须提一提 easy_install。pip 始于 2008 年,而 easy_install 始于 2004 年,二者的关系就如同 Yum 和 rpm 一样,是后者的替代品,功能更强大,但最重要的一点是有了服务端的支持——对版本库的支持和依赖的解决。
virtualenv venv;
source venv/bin/activate;
pip install --trusted-host=artifactory.xxx.xxx -i https://artifactory.xxx.xxx/api/pypi/global-release-pypi/simple -U xoxo -r requirements.txt;
https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide
Unit Test
https://cpython-test-docs.readthedocs.io/en/latest/library/unittest.mock-examples.html
http://boris-42.me/the-simplest-way-in-python-to-mock-open-during-unittest/
https://stackoverflow.com/questions/3829742/assert-that-a-method-was-called-in-a-python-unit-test
https://stackoverflow.com/questions/27270958/patch-multiple-methods-from-different-modules-using-python-mock
https://stackoverflow.com/questions/24897145/python-mock-multiple-return-values
# Change return value for each call
mock_method.side_effect = ['foo', 'bar', 'baz']
https://stackoverflow.com/questions/12187122/assert-a-function-method-was-not-called-using-mock
https://gist.github.com/vpetro/1174019
https://docs.python.org/3/tutorial/modules.html#packages
https://gist.github.com/rochacbruno/ed19c5d9ba9bb50391a2
https://stackoverflow.com/questions/4042905/what-is-main-py
The primary use of
import unittest
https://www.patricksoftwareblog.com/python-unit-testing-structuring-your-project/
https://docs.python.org/2/howto/logging.html
https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary
class CarError(Exception):
def __init__(self, car, msg=None):
super(CarError, self).__init__(msg)
self.car = car
http://www.java2s.com/Code/Python/Function/Passfunctionasparameter2.htm
https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters
The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is because the double star allows us to pass through keyword arguments (and any number of them).
https://www.geeksforgeeks.org/args-kwargs-python/
A keyword argument is where you provide a name to the variable as you pass it into the function.
http://book.pythontips.com/en/latest/args_and_kwargs.html
*args and **kwargs allow you to pass a variable number of arguments to a function
*args is used to send a non-keyworded variable length argument list to the function.
print(list)
https://stackoverflow.com/questions/847850/cross-platform-way-of-getting-temp-directory-in-python
https://stackoverflow.com/questions/9573244/most-elegant-way-to-check-if-the-string-is-empty-in-python
https://stackabuse.com/read-a-file-line-by-line-in-python/
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
print("Line {}: {}".format(cnt, line.strip()))
line = fp.readline()
cnt += 1
with open(filepath) as fp:
for cnt, line in enumerate(fp):
print("Line {}: {}".format(cnt, line))
https://stackoverflow.com/questions/40346498/python-foreach-equivalent/40346541
https://stackoverflow.com/questions/139180/how-to-list-all-functions-in-a-python-module
https://stackoverflow.com/questions/139180/how-to-list-all-functions-in-a-python-module
https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression
https://stackoverflow.com/questions/18962785/oserror-errno-2-no-such-file-or-directory-while-using-python-subprocess-in-dj
https://learnbyexample.gitbooks.io/python-basics/content/Executing_external_commands.html
subprocess.call(["command1", "arg1", "arg2"])
subprocess.call(["ls", "-l", "/etc/resolv.conf"])
p = subprocess.Popen(["ls", "-l", "/etc/resolv.conf"], stdout=subprocess.PIPE)
output, err = p.communicate()
https://docs.python.org/2/library/subprocess.html#module-subprocess
import shutil
shutil.rmtree("the_folder")
os.path.exists(directory)
os.makedirs(directory)
https://stackoverflow.com/questions/303200/how-do-i-remove-delete-a-folder-that-is-not-empty-with-python
https://stackoverflow.com/questions/10116518/im-getting-key-error-in-python
A
class XXTest(unittest.TestCase):
For running python locally, you should use virtualenv: https://virtualenv.pypa.io/en/stable/.
https://stackoverflow.com/questions/2812520/pip-dealing-with-multiple-python-versions
http://docs.python-guide.org/en/latest/starting/install3/osx/
brew uninstall --ignore-dependencies python
##### [pip install --ignore-installed six](https://github.com/pypa/pip/issues/3165)
pip install --ignore-installed six --user
http://marcelog.github.io/articles/mac_osx_python_pip_install_operation_not_permitted.html
https://dbader.org/blog/difference-between-is-and-equals-in-python
https://stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script
https://docs.python.org/2/library/functions.html#staticmethod
https://stackoverflow.com/questions/735975/static-methods-in-python
https://stackoverflow.com/questions/2632677/python-integer-incrementing-with
https://pythonadventures.wordpress.com/2013/03/03/execute-command-show-its-output-get-its-exit-code/
Execute command, show its output, get its exit code
https://pythonadventures.wordpress.com/2018/01/19/what-are-the-built-in-functions/
https://stackoverflow.com/questions/1712227/how-to-get-the-number-of-elements-in-a-list-in-python
https://stackoverflow.com/questions/273192/how-can-i-create-a-directory-if-it-does-not-exist
https://stackoverflow.com/questions/1034573/python-most-idiomatic-way-to-convert-none-to-empty-string
https://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r
https://stackoverflow.com/questions/311627/how-to-print-date-in-a-regular-format-in-python
https://stackoverflow.com/questions/690622/whats-a-standard-way-to-do-a-no-op-in-python
巧用Google Fire简化Python命令行程序
https://stackoverflow.com/questions/1285911/how-do-i-check-that-multiple-keys-are-in-a-dict-in-a-single-pass
if set(("foo", "bar")) <= set(myDict): ...
>>> if any(test not in d for test in tests):
... raise ValueError
not_exist_fields = set(must_exist_fields).difference(set(record))
a.difference(b)
which returns a new set; or with a set.difference_update(other)
; or with set operators a -= b
, which modify the set in-place.https://stackoverflow.com/questions/6130374/empty-set-literal
[]
= empty list
()
= empty tuple
{}
= empty dict
if you want to initialize empty set then apply below code. this one is works perfectly for me in python 3:
object = set([])
object = object.union([1,2,3,4]) #set operation.
#1 “Old Style” String Formatting (% operator)
Because the
It’s also possible to refer to variable substitutions by name in your format string, if you pass a mapping to the %
operator takes only one argument, you need to wrap the right-hand side in a tuple%
operator:'Hey %(name)s, there is a 0x%(errno)x error!' % {
... "name": name, "errno": errno }
#2 “New Style” String Formatting (str.format)
In Python 3, this “new style” string formatting is to be preferred over %-style formatting.
#3 String Interpolation / f-Strings (Python 3.6+)
The
len()
function can be used with a lot of types in Python - both built-in types and library types.>>> len([1,2,3])
Everything in Python is an object, including lists. All objects have a header of some sort in the C implementation.
Lists and other similar builtin objects with a "size" in Python, in particular, have an attribute called ob_size
, where the number of elements in the object is cached. So checking the number of objects in a list is very fast.
But if you're checking if list size is zero or not, don't use len
- instead, put the list in a boolean context - it treated as False if empty, True otherwise.
Place the list in a boolean context (for example, with an
if
or while
statement). It will test False
if it is empty, and True
otherwise. For example:if not a: # do this!
print('a is an empty list')
PEP 8, the official Python style guide for Python code in Python's standard library, asserts:
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
Yes: if not seq: if seq: No: if len(seq): if not len(seq):
We should expect that standard library code should be as performant and correct as possible. But why is that the case, and why do we need this guidance?
Explanation
I frequently see code like this from experienced programmers new to Python:
if len(a) == 0: # Don't do this!
print('a is an empty list')
And users of lazy languages may be tempted to do this:
if a == []: # Don't do this!
print('a is an empty list')
These are correct in their respective other languages. And this is even semantically correct in Python.
But we consider it un-Pythonic because Python supports these semantics directly in the list object's interface via boolean coercion.
From the docs (and note specifically the inclusion of the empty list, []
):
By default, an object is considered true unless its class defines either a __bool__()
method that returns False
or a __len__()
method that returns zero, when called with the object. Here are most of the built-in objects considered false:
- constants defined to be false:
None
and False
.
- zero of any numeric type:
0
, 0.0
, 0j
, Decimal(0)
, Fraction(0, 1)
- empty sequences and collections:
''
, ()
, []
, {}
, set()
, range(0)
http://www.raychase.net/4881
讲到 pip 必须提一提 easy_install。pip 始于 2008 年,而 easy_install 始于 2004 年,二者的关系就如同 Yum 和 rpm 一样,是后者的替代品,功能更强大,但最重要的一点是有了服务端的支持——对版本库的支持和依赖的解决。
virtualenv venv;
source venv/bin/activate;
pip install --trusted-host=artifactory.xxx.xxx -i https://artifactory.xxx.xxx/api/pypi/global-release-pypi/simple -U xoxo -r requirements.txt;
https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide
A Python class attribute is an attribute of the class (circular, I know), rather than an attribute of an instance of a class
The
__init__.py
files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string
, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py
can just be an empty file, but it can also execute initialization code for the package or set the __all__
variable, described later.https://cpython-test-docs.readthedocs.io/en/latest/library/unittest.mock-examples.html
http://boris-42.me/the-simplest-way-in-python-to-mock-open-during-unittest/
Unfortunately, there is one unclear place (at least for me) how to mock open() properly especially in case where when with open() in used in code.
def some(file_path): with open(file_path) as f: return f.read() class MyTestCase(testtools.TestCase): @mock.patch("open", create=True) def test_some(self, mock_open): mock_open.side_effect = [ mock.mock_open(read_data="Data1").return_value, mock.mock_open(read_data="Data2").return_value ] self.assertEqual("Data1", some("fileA")) mock_open.assert_called_once_with("fileA") mock_open.reset_mock() self.assertEqual("Data2", some("fileB")) mock_open.assert_called_once_with("fileB")
self.assertTrue(mock.called)
mock.assert_called_with(42) # or mock.assert_called_once_with(42)
@patch("foo.load",side_effects=["a","b","c"])
@patch("bar.check",return_value=True)
def test_mytest(mock_check,mock_load):
take_action()
assert mock_load.called
assert mock_check.called
If you need it in all tests of a test class you can decorate the class and use the mocks in all test methods:
@patch("foo.load",side_effects=["a","b","c"])
@patch("bar.check",return_value=True)
class TestMyTest(unittest.TestCase)
def test_mytestA(self,mock_check,mock_load):
take_action()
self.assertTrue(mock_load.called)
self.assertTrue(mock_check.called)
def test_mytestA(self,mock_check,mock_load):
mock_check.return_value = False
take_action()
self.assertTrue(mock_load.called)
self.assertTrue(mock_check.called
https://stackoverflow.com/questions/29318987/preferred-way-of-patching-multiple-methods-in-python-unit-test@patch('mbgmodule.MBG120Simulator._send_reply', autospec=True)
@patch('mbgmodule.MBG120Simulator._reset_watchdog', autospec=True)
@patch('mbgmodule.MBG120Simulator._handle_set_watchdog', autospec=True)
def test_handle_command_too_short(self,mock_handle_set_watchdog,
mock_reset_watchdog,
mock_send_reply):
simulator = MBG120Simulator()
simulator._handle_command('XA99')
# You can use ANY instead simulator if you don't know it
mock_send_reply.assert_called_once_with(simulator, 'X?')
self.assertFalse(mock_reset_watchdog.called)
self.assertFalse(mock_handle_set_watchdog_mock.called)
simulator.stop()
- Patching is out of the test method code
- Every mock starts by
mock_
prefix - I prefer to use simple
patch
call and absolute path: it is clear and neat what you are doing
https://stackoverflow.com/questions/24897145/python-mock-multiple-return-values
# Change return value for each call
mock_method.side_effect = ['foo', 'bar', 'baz']
with self.assertRaises(TypeError) as cm:
failure.fail()
self.assertEqual(
'The registeraddress must be an integer. Given: 1.0',
str(cm.exception)
)
self.assertRaisesRegexp(ValueError, 'invalid literal for.*XYZ$', int, 'XYZ')
or:
with self.assertRaisesRegexp(ValueError, 'literal'):
int('XYZ')
with self.assertRaises(ValueError) as error:
do_something()
self.assertEqual(error.exception.message, 'error message')
assert not my_var.called, 'method should not have been called'
@mock.patch("my_module.method_to_mock")
def test(self, mock_method):
my_module.method_to_mock()
self.assertFalse(mock_method.called,
self.message.format(times=mock_method.call_count))
m = mock.Mock() m.side_effect = [1, 2, 3] for i in ['a', 'b', 'c']: print(i, m())
https://docs.python.org/3/tutorial/modules.html#packages
When you run a Python module with
the code in the module will be executed, just as if you imported it, but with the
__name__
set to "__main__"
. That means that by adding this code at the end of your module:https://gist.github.com/rochacbruno/ed19c5d9ba9bb50391a2
https://stackoverflow.com/questions/4042905/what-is-main-py
Often, a Python program is run by naming a .py file on the command line:
$ python my_program.py
You can also create a directory or zipfile full of code, and include a
__main__.py
. Then you can simply name the directory or zipfile on the command line, and it executes the __main__.py
automatically:$ python my_program_dir
$ python my_program.zip
# Or, if the program is accessible as a module
$ python -m my_program
The primary use of
__init__.py
is to initialize Python packages.__init__.py
can be an empty file but it is often used to perform setup needed for the package(import things, load things into path, etc).
One common thing to do in your
__init__.py
is to import selected Classes, functions, etc into the package level so they can be convieniently imported from the package.
When importing the package, Python searches through the directories on
sys.path
looking for the package subdirectory.
The
__init__.py
files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string
, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py
can just be an empty file, but it can also execute initialization code for the package or set the __all__
variableWimport unittest
@unittest.skip("integration test")
@classmethod
def setUpClass(cls):
@classmethod
def tearDownClass(cls):
if __name__ == '__main__':
unittest.main()
from unittest import TestCase
import
unittest
class
TestBasicFunction(unittest.TestCase):
def
test(
self
):
self
.assertTrue(
True
)
if
__name__
=
=
'__main__'
:
unittest.main()
https://docs.python.org/2/howto/logging.html
https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary
if 'key1' in dict:
https://julien.danjou.info/python-exceptions-guide/
class CarError(Exception):
def __init__(self, car, msg=None):
super(CarError, self).__init__(msg)
self.car = car
def minmax(test, *args): res = args[0] for arg in args[1:]: if test(arg, res): res = arg return res def lessthan(x, y): return x < y def grtrthan(x, y): return x > y print minmax(lessthan, 4, 2, 1, 5, 6, 3) print minmax(grtrthan, 4, 2, 1, 5, 6, 3)https://stackoverflow.com/questions/423379/using-global-variables-in-a-function
You can use a global variable in other functions by declaring it as
global
in each function that assigns to it:globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print(globvar) # No need for global declaration to read value of globvar
I imagine the reason for it is that, since global variables are so dangerous, Python wants to make sure that you really know that's what you're playing with by explicitly requiring the global
keyword.
The
*args
and **kwargs
is a common idiom to allow arbitrary number of arguments to functions as described in the section more on defining functions in the Python documentation.
The
*args
will give you all function parameters as a tuple:
The
**kwargs
will give you all keyword arguments except for those corresponding to a formal parameter as a dictionary.In [5]: def bar(**kwargs):
...: for a in kwargs:
...: print a, kwargs[a]
...:
...:
In [6]: bar(name='one', age=27)
Both idioms can be mixed with normal arguments to allow a set of fixed and some variable arguments:
def foo(kind, *args, **kwargs):
pass
Another usage of the *l
idiom is to unpack argument lists when calling a function.
In [9]: def foo(bar, lee):
...: print bar, lee
...:
...:
In [10]: l = [1,2]
In [11]: foo(*l)
The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is because the double star allows us to pass through keyword arguments (and any number of them).
https://www.geeksforgeeks.org/args-kwargs-python/
A keyword argument is where you provide a name to the variable as you pass it into the function.
- One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn’t seem to be any order in which they were printed out.
- Example for usage of **kwargs:
def
hello(
*
*
kwargs):
if
kwargs
is
not
None
:
for
key, value
in
kwargs.iteritems():
print
"%s == %s"
%
(key, value)
*args and **kwargs allow you to pass a variable number of arguments to a function
*args is used to send a non-keyworded variable length argument list to the function.
def greet_me(**kwargs): for key, value in kwargs.items(): print("{0} = {1}".format(key, value))https://stackoverflow.com/questions/35067932/pycharm-a-python-ide-can-only-show-the-first-300-members-of-a-list
print(list)
https://stackoverflow.com/questions/847850/cross-platform-way-of-getting-temp-directory-in-python
That would be the tempfile module.
It has functions to get the temporary directory, and also has some shortcuts to create temporary files and directories in it, either named or unnamed.
Example:
import tempfile
print tempfile.gettempdir() # prints the current temporary directory
f = tempfile.TemporaryFile()
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
So you should use:
if not some_string:
or:
if some_string:
Just to clarify, sequences are evaluated to
False
or True
in a Boolean context if they are empty or not. They are not equal to False
or True
.with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
print("Line {}: {}".format(cnt, line.strip()))
line = fp.readline()
cnt += 1
with open(filepath) as fp:
for cnt, line in enumerate(fp):
print("Line {}: {}".format(cnt, line))
https://stackoverflow.com/questions/40346498/python-foreach-equivalent/40346541
for iterating_var in sequence: statements(s)
The
else
block only gets executed if no exception gets caught. And the finally
block gets executed no matter what.
try:
assert i >= 1
return l[i]
except TypeError,e: ####A
print "dealing with TypeError"
except IndexError, e: ####B
print "dealing with IndexError"
except: ####C
print "oh dear"
finally: ####D
print "the end"
class MyException(Exception):
def __init__(self,*args,**kwargs):
Exception.__init__(self,*args,**kwargs)
class MyIndexError(IndexError):
def __init__(self,*args,**kwargs):
IndexError.__init__(self,*args,**kwargs)
https://stackoverflow.com/questions/690622/whats-a-standard-way-to-do-a-no-op-in-python
Use pass for no-op:
def f():
pass
Or:
class c:
pass
https://stackoverflow.com/questions/139180/how-to-list-all-functions-in-a-python-module
Once you've
import
ed the module, you can just do: help(modulename)
... To get the docs on all the functions at once, interactively. Or you can use:
dir(modulename)
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
If the object has a method named __dir__()
, this method will be called and must return the list of attributes. This allows objects that implement a custom __getattr__()
or __getattribute__()
function to customize the way dir()
reports their attributes.
If the object does not provide __dir__()
, the function tries its best to gather information from the object’s __dict__
attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom __getattr__()
.
def merge_two_dicts(x, y):
z = x.copy() # start with x's keys and values
z.update(y) # modifies z with y's keys and values & returns None
return z
https://stackoverflow.com/questions/18962785/oserror-errno-2-no-such-file-or-directory-while-using-python-subprocess-in-dj
Use
shell=True
if you're passing a string to subprocess.call
.
From docs:
If passing a single string, eithershell
must beTrue
or else the string must simply name the program to be executed without specifying any arguments.
subprocess.call(crop, shell=True)
or:
import shlex
subprocess.call(shlex.split(crop))
https://learnbyexample.gitbooks.io/python-basics/content/Executing_external_commands.html
- By default,
subprocess.call
will not expand shell wildcards, perform command substitution, etc - This can be overridden by passing
True
value forshell
argument - Note that the entire command is now passed as string and not as a list of strings
- Quotes need to be escaped if they clash between command string and quotes within the command itself
subprocess.call(["command1", "arg1", "arg2"])
subprocess.call(["ls", "-l", "/etc/resolv.conf"])
p = subprocess.Popen(["ls", "-l", "/etc/resolv.conf"], stdout=subprocess.PIPE)
output, err = p.communicate()
p = subprocess.Popen(["ping", "-c", "10", "www.cyberciti.biz"], stdout=subprocess.PIPE)
output, err = p.communicate()
Executing shell commands that incorporate unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. For this reason, the use of
shell=True
is strongly discouraged in cases where the command string is constructed from external input:shell=False
disables all shell based features, but does not suffer from this vulnerability; see the Note in the Popen
constructor documentation for helpful hints in getting shell=False
to work.
import subprocess
subprocess.check_call("git clone --depth 1 -b master " + git_repo, cwd=tmp_folder, shell=True)
commit_id = subprocess.check_output(["git log --pretty=format:%H"], cwd=git_repo_folder, shell=True)
shutil.rmtree("the_folder")
os.path.exists(directory)
os.makedirs(directory)
import shutil
shutil.rmtree('/folder_name')
By design,
rmtree
fails on folder trees containing read-only files. If you want the folder to be deleted regardless of whether it contains read-only files, then useshutil.rmtree('/folder_name', ignore_errors=True)
A
KeyError
generally means the key doesn't exist.
exception KeyError
import unittestRaised when a mapping (dictionary) key is not found in the set of existing keys.
class XXTest(unittest.TestCase):
For running python locally, you should use virtualenv: https://virtualenv.pypa.io/en/stable/.
https://stackoverflow.com/questions/2812520/pip-dealing-with-multiple-python-versions
The current recommendation is to use
python -m pip
, where python
is the version of you would like to use. This is the recommendation because it works across all versions of Python, and in all forms of virtualenv. For example:# The system default python:
$ python -m pip install fish
# A virtualenv's python:
$ .env/bin/python -m pip install fish
# A specific version of python:
$ python-3.6 -m pip install fis
http://docs.python-guide.org/en/latest/starting/install3/osx/
A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.
brew uninstall --ignore-dependencies python
##### [pip install --ignore-installed six](https://github.com/pypa/pip/issues/3165)
pip install --ignore-installed six --user
pip install <package>
on a Mac and get an error like
Could not create /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7: Operation not permitted
You hit an internal protection called System Integrity Protection built-in OS X El Capitan and laterthat, among other things, protects some paths of your root filesystem in a way that not even the user root can write to them.
And your are not alone, this "issue" has hit a lot of users of different open source Python packages, as one can see in some GitHub Issues.
In the specific case of using PIP to install Python packages, you can overcome this by installing everything into your own home directory with the --user flag, like:
pip install <package> --user
This flag is documented in the "Installing to the User Site" section of the "Python Packaging User Guide", and there is more in depth description of the process in the "User Installs" section of the "PIP User Guide"
https://dbader.org/blog/difference-between-is-and-equals-in-python
The
==
operator compares by checking for equality: If these cats were Python objects and we’d compare them with the ==
operator, we’d get “both cats are equal” as an answer.
The
is
operator, however, compares identities: If we compared our cats with the is
operator, we’d get “these are two different cats” as an answer.- An
is
expression evaluates toTrue
if two variables point to the same (identical) object. - An
==
expression evaluates toTrue
if the objects referred to by the variables are equal (have the same contents).
Menu: Run->Edit configurations->"+" (add new config)->Python.
Script name: program.py
Script params: -t input1 -t1 input2
sys.argv
is automatically a list of strings representing the arguments (as separated by spaces) on the command-line.> python print_args.py
['print_args.py'] 1
> python print_args.py foo and bar
['print_args.py', 'foo', 'and', 'bar'] 4
import sys
print "This is the name of the script: ", sys.argv[0]
print "Number of arguments: ", len(sys.argv)
print "The arguments are: " , str(sys.argv)
staticmethod
(function)
Return a static method for function.
A static method does not receive an implicit first argument. To declare a static method, use this idiom:
class MyClass(object):
@staticmethod
def the_static_method(x):
print x
MyClass.the_static_method(2) # outputs 2
Python doesn't support
++
, but you can do:number += 1
Simply put, the
++
and --
operators don't exist in Python because they wouldn't be operators, they would have to be statements. All namespace modification in Python is a statement, for simplicity and consistency. That's one of the design decisions. And because integers are immutable, the only way to 'change' a variable is by reassigning it.
Fortunately we have wonderful tools for the use-cases of
++
and --
in other languages, like enumerate()
and itertools.count()
.Execute command, show its output, get its exit code
import
shlex
from
subprocess
import
Popen, PIPE
cmd
=
"..."
process
=
Popen(shlex.split(cmd), stdout
=
PIPE)
process.communicate()
# execute it, the output goes to the stdout
exit_code
=
process.wait()
# when finished, get the exit code
https://pythonadventures.wordpress.com/2018/01/19/what-are-the-built-in-functions/
In [
1
]:
import
builtins
In [
2
]:
dir
(builtins)
The
len()
function can be used with a lot of types in Python - both built-in types and library types.>>> len([1,2,3])
if items:
https://stackoverflow.com/questions/273192/how-can-i-create-a-directory-if-it-does-not-exist
Try os.path.exists
, and consider os.makedirs
for the creation.
if not os.path.exists(directory):
os.makedirs(directory)
https://docs.python.org/2/library/functions.html#func-reprrepr
(object)- Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to
eval()
, otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a__repr__()
method.
Probably the shortest would be
str(s or '')
Because None is False, and "x or y" returns y if x is false. See Boolean Operators for a detailed explanation. It's short, but not very explicit.
def xstr(s):
return str(s) or ""
https://stackoverflow.com/questions/5914627/prepend-line-to-beginning-of-a-file
In modes
'a'
or 'a+'
, any writing is done at the end of the file, even if at the current moment when the write()
function is triggered the file's pointer is not at the end of the file: the pointer is moved to the end of file before any writing. You can do what you want in two manners.
1st way, can be used if there are no issues to load the file into memory:
def line_prepender(filename, line):
with open(filename, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(line.rstrip('\r\n') + '\n' + content)
2nd way:
def line_pre_adder(filename, line_to_prepend):
f = fileinput.input(filename, inplace=1)
for xline in f:
if f.isfirstline():
print line_to_prepend.rstrip('\r\n') + '\n' + xline,
else:
print xline,
I don't know how this method works under the hood and if it can be employed on big big file. The argument 1 passed to input is what allows to rewrite a line in place; the following lines must be moved forwards or backwards in order that the inplace operation takes place, but I don't know the mechanism
The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):
``r'' Open text file for reading. The stream is positioned at the
beginning of the file.
``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.
``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
``w+'' Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.
``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.
``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.
print today.strftime('We are the %d, %b %Y')
https://dbader.org/blog/python-repr-vs-str
you’ll be better off adding the __str__
and __repr__
“dunder” methods to your class.
def __str__(self): return f'a {self.color} car'
This experiment confirms that inspecting an object in a Python interpreter session simply prints the result of the object’s
__repr__
.
Interestingly, containers like lists and dicts always use the result of
__repr__
to represent the objects they contain. Even if you call str
on the container itself:http://brennerm.github.io/posts/python-str-vs-repr.html
str() | repr() |
---|---|
- make object readable | - need code that reproduces object |
- generate output for end user | - generate output for developer |
https://stackoverflow.com/questions/690622/whats-a-standard-way-to-do-a-no-op-in-python
Use pass for no-op:
if x == 0:
pass
else:
print "x not equal 0"
And here's another example:
def f():
pass