After uninstalling, if there are still problems with the version, switch to anaconda2 and install Anaconda3 instead. 2 and 3 can be installed on a computer at the same time.

in the Python installation path to find Scripts file under the pyinstaller-script.py file and open, if the path does not have quotes then put quotes, this situation generally occurs in the pyinstaller version is low
p>
if the file cannot be packaged with quotation marks, then from the DOS command line into the Scripts folder of the Python installation path execute: Python pyinstaller-script.py G:\Python\Project\Pychar\Qt5\demo5.py (replace G:\Python\Project\Pychar\Qt5\demo5.py with the file you need to package yourself)
p>
in a function
def fun():pass
How does the function
know who called it?In a C language, it seems difficult. But in python it’s very simple
import traceback
def fun():
s = traceback.extract_stack()
print '%s Invoked me!'%s[-2][2]
the fun function will tell you who called it and print it out, so let’s try it:
>>> def a():fun()
>>> def b():fun()
>>> a()
a Invoked me!
>>> b()
b Invoked me!
>>>
div>
NameError: name ‘requests‘ is not defined is lack of package import requests
;
NameError: name ‘request’ is not defined because of lack of package from flask import request
.
these two look too much alike, but they don’t work the same way. “Requests” is for getting GET, POST requests, etc., such as
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
“request” is used to get form data such as
request.form.get("value")
, or the front-end request method, etc. For more details, please refer to request
in flask
reference:
Requests: let HTTP service humans
NameError: name ‘request’ is not defined
first edition:
# BEGIN VECTOR_V1
from array import array
import reprlib
import math
class Vector:
typecode = 'd'
def __init__(self, components):
self._components = array(self.typecode, components) # <1>
def __iter__(self):
return iter(self._components) # <2>
def __repr__(self):
components = reprlib.repr(self._components) # <3>
components = components[components.find('['):-1] # <4>
return 'Vector({})'.format(components)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return (bytes([ord(self.typecode)]) +
bytes(self._components)) # <5>
def __eq__(self, other):
return tuple(self) == tuple(other)
def __abs__(self):
return math.sqrt(sum(x * x for x in self)) # <6>
def __bool__(self):
return bool(abs(self))
@classmethod
def frombytes(cls, octets):
typecode = chr(octets[0])
memv = memoryview(octets[1:]).cast(typecode)
return cls(memv) # <7>
# END VECTOR_V1
second edition: slicable sequence
from array import array
import reprlib
import math
import numbers
class Vector:
typecode = 'd'
def __init__(self, components):
self._components = array(self.typecode, components)
def __iter__(self):
return iter(self._components)
def __repr__(self):
components = reprlib.repr(self._components)
components = components[components.find('['):-1]
return 'Vector({})'.format(components)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return (bytes([ord(self.typecode)]) +
bytes(self._components))
def __eq__(self, other):
return tuple(self) == tuple(other)
def __abs__(self):
return math.sqrt(sum(x * x for x in self))
def __bool__(self):
return bool(abs(self))
# BEGIN VECTOR_V2
def __len__(self):
return len(self._components)
def __getitem__(self, index):
cls = type(self) # <1>
if isinstance(index, slice): # <2>
return cls(self._components[index]) # <3>
elif isinstance(index, numbers.Integral): # <4>
return self._components[index] # <5>
else:
msg = '{cls.__name__} indices must be integers'
raise TypeError(msg.format(cls=cls)) # <6>
# END VECTOR_V2
@classmethod
def frombytes(cls, octets):
typecode = chr(octets[0])
memv = memoryview(octets[1:]).cast(typecode)
return cls(memv)
The biggest change in
V2 is the implementation of ___ and ___, thereby enabling slicing, both of which are also necessary for vector to behave as a sequence:
if [1:4] is used, a slice is returned. Slice is a built-in type. A review of the slice reveals that it has the start,stop, step data attributes, and the indices methods. In indices, given a sequence of len, indexes at the beginning and the end of an extension section marked with S, as well as the stride length, are computed. Indexes exceeding the boundary are truncated.
Vector version 3: dynamic access property
from array import array
import reprlib
import math
import numbers
class Vector:
typecode = 'd'
def __init__(self, components):
self._components = array(self.typecode, components)
def __iter__(self):
return iter(self._components)
def __repr__(self):
components = reprlib.repr(self._components)
components = components[components.find('['):-1]
return 'Vector({})'.format(components)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return (bytes([ord(self.typecode)]) +
bytes(self._components))
def __eq__(self, other):
return tuple(self) == tuple(other)
def __abs__(self):
return math.sqrt(sum(x * x for x in self))
def __bool__(self):
return bool(abs(self))
def __len__(self):
return len(self._components)
def __getitem__(self, index):
cls = type(self)
if isinstance(index, slice):
return cls(self._components[index])
elif isinstance(index, numbers.Integral):
return self._components[index]
else:
msg = '{.__name__} indices must be integers'
raise TypeError(msg.format(cls))
# BEGIN VECTOR_V3_GETATTR
shortcut_names = 'xyzt'
def __getattr__(self, name):
cls = type(self) # <1>
if len(name) == 1: # <2>
pos = cls.shortcut_names.find(name) # <3>
if 0 <= pos < len(self._components): # <4>
return self._components[pos]
msg = '{.__name__!r} object has no attribute {!r}' # <5>
raise AttributeError(msg.format(cls, name))
# END VECTOR_V3_GETATTR
# BEGIN VECTOR_V3_SETATTR
def __setattr__(self, name, value):
cls = type(self)
if len(name) == 1: # <1>
if name in cls.shortcut_names: # <2>
error = 'readonly attribute {attr_name!r}'
elif name.islower(): # <3>
error = "can't set attributes 'a' to 'z' in {cls_name!r}"
else:
error = '' # <4>
if error: # <5>
msg = error.format(cls_name=cls.__name__, attr_name=name)
raise AttributeError(msg)
super().__setattr__(name, value) # <6>
# END VECTOR_V3_SETATTR
@classmethod
def frombytes(cls, octets):
typecode = chr(octets[0])
memv = memoryview(octets[1:]).cast(typecode)
return cls(memv)
, ___ getattr__ and ___ setattr__, the former is to get vector components and the latter is to protect the existing components. V.x cannot be directly assigned because x has become an attribute of v. The value of v.x has changed, but the value of v has not.
Vector iv: hashing and fast equivalence testing
from array import array
import reprlib
import math
import numbers
import functools
import operator
class Vector:
typecode = 'd'
def __init__(self, components):
self._components = array(self.typecode, components)
def __iter__(self):
return iter(self._components)
def __repr__(self):
components = reprlib.repr(self._components)
components = components[components.find('['):-1]
return 'Vector({})'.format(components)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return (bytes([ord(self.typecode)]) +
bytes(self._components))
def __eq__(self, other):
return (len(self) == len(other) and
all(a == b for a, b in zip(self, other)))
def __hash__(self):
hashes = (hash(x) for x in self)
return functools.reduce(operator.xor, hashes, 0)
def __abs__(self):
return math.sqrt(sum(x * x for x in self))
def __bool__(self):
return bool(abs(self))
def __len__(self):
return len(self._components)
def __getitem__(self, index):
cls = type(self)
if isinstance(index, slice):
return cls(self._components[index])
elif isinstance(index, numbers.Integral):
return self._components[index]
else:
msg = '{cls.__name__} indices must be integers'
raise TypeError(msg.format(cls=cls))
shortcut_names = 'xyzt'
def __getattr__(self, name):
cls = type(self)
if len(name) == 1:
pos = cls.shortcut_names.find(name)
if 0 <= pos < len(self._components):
return self._components[pos]
msg = '{.__name__!r} object has no attribute {!r}'
raise AttributeError(msg.format(cls, name))
@classmethod
def frombytes(cls, octets):
typecode = chr(octets[0])
memv = memoryview(octets[1:]).cast(typecode)
return cls(memv)
fifth edition: format
# BEGIN VECTOR_V5
"""
A multi-dimensional ``Vector`` class, take 5
A ``Vector`` is built from an iterable of numbers::
>>> Vector([3.1, 4.2])
Vector([3.1, 4.2])
>>> Vector((3, 4, 5))
Vector([3.0, 4.0, 5.0])
>>> Vector(range(10))
Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
Tests with 2-dimensions (same results as ``vector2d_v1.py``)::
>>> v1 = Vector([3, 4])
>>> x, y = v1
>>> x, y
(3.0, 4.0)
>>> v1
Vector([3.0, 4.0])
>>> v1_clone = eval(repr(v1))
>>> v1 == v1_clone
True
>>> print(v1)
(3.0, 4.0)
>>> octets = bytes(v1)
>>> octets
b'd\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
>>> abs(v1)
5.0
>>> bool(v1), bool(Vector([0, 0]))
(True, False)
Test of ``.frombytes()`` class method:
>>> v1_clone = Vector.frombytes(bytes(v1))
>>> v1_clone
Vector([3.0, 4.0])
>>> v1 == v1_clone
True
Tests with 3-dimensions::
>>> v1 = Vector([3, 4, 5])
>>> x, y, z = v1
>>> x, y, z
(3.0, 4.0, 5.0)
>>> v1
Vector([3.0, 4.0, 5.0])
>>> v1_clone = eval(repr(v1))
>>> v1 == v1_clone
True
>>> print(v1)
(3.0, 4.0, 5.0)
>>> abs(v1) # doctest:+ELLIPSIS
7.071067811...
>>> bool(v1), bool(Vector([0, 0, 0]))
(True, False)
Tests with many dimensions::
>>> v7 = Vector(range(7))
>>> v7
Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
>>> abs(v7) # doctest:+ELLIPSIS
9.53939201...
Test of ``.__bytes__`` and ``.frombytes()`` methods::
>>> v1 = Vector([3, 4, 5])
>>> v1_clone = Vector.frombytes(bytes(v1))
>>> v1_clone
Vector([3.0, 4.0, 5.0])
>>> v1 == v1_clone
True
Tests of sequence behavior::
>>> v1 = Vector([3, 4, 5])
>>> len(v1)
3
>>> v1[0], v1[len(v1)-1], v1[-1]
(3.0, 5.0, 5.0)
Test of slicing::
>>> v7 = Vector(range(7))
>>> v7[-1]
6.0
>>> v7[1:4]
Vector([1.0, 2.0, 3.0])
>>> v7[-1:]
Vector([6.0])
>>> v7[1,2]
Traceback (most recent call last):
...
TypeError: Vector indices must be integers
Tests of dynamic attribute access::
>>> v7 = Vector(range(10))
>>> v7.x
0.0
>>> v7.y, v7.z, v7.t
(1.0, 2.0, 3.0)
Dynamic attribute lookup failures::
>>> v7.k
Traceback (most recent call last):
...
AttributeError: 'Vector' object has no attribute 'k'
>>> v3 = Vector(range(3))
>>> v3.t
Traceback (most recent call last):
...
AttributeError: 'Vector' object has no attribute 't'
>>> v3.spam
Traceback (most recent call last):
...
AttributeError: 'Vector' object has no attribute 'spam'
Tests of hashing::
>>> v1 = Vector([3, 4])
>>> v2 = Vector([3.1, 4.2])
>>> v3 = Vector([3, 4, 5])
>>> v6 = Vector(range(6))
>>> hash(v1), hash(v3), hash(v6)
(7, 2, 1)
Most hash values of non-integers vary from a 32-bit to 64-bit CPython build::
>>> import sys
>>> hash(v2) == (384307168202284039 if sys.maxsize > 2**32 else 357915986)
True
Tests of ``format()`` with Cartesian coordinates in 2D::
>>> v1 = Vector([3, 4])
>>> format(v1)
'(3.0, 4.0)'
>>> format(v1, '.2f')
'(3.00, 4.00)'
>>> format(v1, '.3e')
'(3.000e+00, 4.000e+00)'
Tests of ``format()`` with Cartesian coordinates in 3D and 7D::
>>> v3 = Vector([3, 4, 5])
>>> format(v3)
'(3.0, 4.0, 5.0)'
>>> format(Vector(range(7)))
'(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0)'
Tests of ``format()`` with spherical coordinates in 2D, 3D and 4D::
>>> format(Vector([1, 1]), 'h') # doctest:+ELLIPSIS
'<1.414213..., 0.785398...>'
>>> format(Vector([1, 1]), '.3eh')
'<1.414e+00, 7.854e-01>'
>>> format(Vector([1, 1]), '0.5fh')
'<1.41421, 0.78540>'
>>> format(Vector([1, 1, 1]), 'h') # doctest:+ELLIPSIS
'<1.73205..., 0.95531..., 0.78539...>'
>>> format(Vector([2, 2, 2]), '.3eh')
'<3.464e+00, 9.553e-01, 7.854e-01>'
>>> format(Vector([0, 0, 0]), '0.5fh')
'<0.00000, 0.00000, 0.00000>'
>>> format(Vector([-1, -1, -1, -1]), 'h') # doctest:+ELLIPSIS
'<2.0, 2.09439..., 2.18627..., 3.92699...>'
>>> format(Vector([2, 2, 2, 2]), '.3eh')
'<4.000e+00, 1.047e+00, 9.553e-01, 7.854e-01>'
>>> format(Vector([0, 1, 0, 0]), '0.5fh')
'<1.00000, 1.57080, 0.00000, 0.00000>'
"""
from array import array
import reprlib
import math
import numbers
import functools
import operator
import itertools # <1>
class Vector:
typecode = 'd'
def __init__(self, components):
self._components = array(self.typecode, components)
def __iter__(self):
return iter(self._components)
def __repr__(self):
components = reprlib.repr(self._components)
components = components[components.find('['):-1]
return 'Vector({})'.format(components)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return (bytes([ord(self.typecode)]) +
bytes(self._components))
def __eq__(self, other):
return (len(self) == len(other) and
all(a == b for a, b in zip(self, other)))
def __hash__(self):
hashes = (hash(x) for x in self)
return functools.reduce(operator.xor, hashes, 0)
def __abs__(self):
return math.sqrt(sum(x * x for x in self))
def __bool__(self):
return bool(abs(self))
def __len__(self):
return len(self._components)
def __getitem__(self, index):
cls = type(self)
if isinstance(index, slice):
return cls(self._components[index])
elif isinstance(index, numbers.Integral):
return self._components[index]
else:
msg = '{.__name__} indices must be integers'
raise TypeError(msg.format(cls))
shortcut_names = 'xyzt'
def __getattr__(self, name):
cls = type(self)
if len(name) == 1:
pos = cls.shortcut_names.find(name)
if 0 <= pos < len(self._components):
return self._components[pos]
msg = '{.__name__!r} object has no attribute {!r}'
raise AttributeError(msg.format(cls, name))
def angle(self, n): # <2>
r = math.sqrt(sum(x * x for x in self[n:]))
a = math.atan2(r, self[n-1])
if (n == len(self) - 1) and (self[-1] < 0):
return math.pi * 2 - a
else:
return a
def angles(self): # <3>
return (self.angle(n) for n in range(1, len(self)))
def __format__(self, fmt_spec=''):
if fmt_spec.endswith('h'): # hyperspherical coordinates
fmt_spec = fmt_spec[:-1]
coords = itertools.chain([abs(self)],
self.angles()) # <4>
outer_fmt = '<{}>' # <5>
else:
coords = self
outer_fmt = '({})' # <6>
components = (format(c, fmt_spec) for c in coords) # <7>
return outer_fmt.format(', '.join(components)) # <8>
@classmethod
def frombytes(cls, octets):
typecode = chr(octets[0])
memv = memoryview(octets[1:]).cast(typecode)
return cls(memv)
# END VECTOR_V5
p>
div>
I just started learning Python recently. To implement this same function, iterate through an XML file in the res\\value directory of an Android app code. Because its XML file format is basically the following, relatively simple.
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActivityLife</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
As you can see, there are actually three String children under the resources parent node. Because I’m new to Python, I find it easier to use the XML.dom method (forgive me for being a whitehead).
The
XML dom defines the objects and attributes of XML elements and the methods to access them. Dom treats XML documents as a tree structure. To see this, click the open link. Ok, let’s get back to business. To solve the above problem, the specific idea is
1. Traverse the folder to get all the XML files. (glob.glob())
2. Read and parse each XML. Gets the child node attribute value and the text node value.
below is the implementation code:
# -*- coding: UTF-8 -*-
#遍历某个文件夹下所有xml文件
import sys
import glob
import os
import xml.dom.minidom
def traversalDir_XMLFile(path):
#判断路径是否存在
if (os.path.exists(path)):
#得到该文件夹路径下下的所有xml文件路径
f = glob.glob(path + '\\*.xml' )
for file in f :
print file
#打开xml文档
dom = xml.dom.minidom.parse(file)
#得到文档元素对象
root = dom.documentElement
#得到子节点列表,print childs
childs = root.childNodes
for child in childs:
#筛选符合需求的child
if(child.nodeType == 1):
#得出子节点属性和文本节点值
print'key:', child.getAttribute('name')
print'value:',child.firstChild.data
traversalDir_XMLFile('E:\\work\\ActivityLife\\res\\values')
The path path is one of my value folders with dimens.xml; string.xml; styles.xml; There are also several Word files and TXT format files. The output result is:
E:\work\ActivityLife\res\values\dimens.xml
key: activity_horizontal_margin
value: 16dp
key: activity_vertical_margin
value: 16dp
E:\work\ActivityLife\res\values\strings.xml
key: app_name
value: ActivityLife
key: hello_world
value: Hello world!
key: action_settings
value: Settings
E:\work\ActivityLife\res\values\styles.xml
key: AppBaseTheme
value:
key: AppTheme
value:
The comments in the code are pretty clear. Because I have other XML files in my file, although the parent nodes are all under Resources, the children are different. There are strings, there are dimen and so on. But the format is the same. So, instead of using child.nodeValue when I print value, I get none, which is not so clear. I think it might be this:
A common mistake in DOM processing is to assume that the element node contains text.
However, the text of the element node is stored in the text node.
In this case: < year> 2005< /year> , element node < year> , has a text node with a value of “2005”.
“2005” not < year> Element value!
specific reasons hope readers can tell me ha!! Do ⌒ (* ^ – ゜) v p>
some reference documents are given below:
Python golb methods: http://www.cnblogs.com/hongten/p/hongten_python_glob.html p>
XML parsing: http://www.cnblogs.com/fnng/p/3581433.html p>
p>
http://www.runoob.com/python/python-xml.html