full output:
82 INFO: PyInstaller: 4.0.dev0+a9b299995
82 INFO: Python: 3.7.0
82 INFO: Platform: Windows-10-10.0.17763-SP0
90 INFO: UPX is not available.
92 INFO: Extending PYTHONPATH with paths
['C:\Users\bWX826206\Documents\GVF\2019-10-02_09-04-03__epoch_4',
'C:\Users\bWX826206\Documents\GVF\2019-10-02_09-04-03__epoch_4']
92 INFO: checking Analysis
92 INFO: Building Analysis because Analysis-00.toc is non existent
92 INFO: Initializing module dependency graph...
92 INFO: Caching module graph hooks...
100 INFO: Analyzing base_library.zip ...
3205 INFO: Caching module dependency graph...
3308 INFO: running Analysis Analysis-00.toc
3328 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by C:UsersbWX826206PycharmProjectsfeature_extractorvenvScriptspython.exe
3632 INFO: Analyzing runner.py
4667 INFO: Processing pre-find module path hook distutils
4668 INFO: distutils: retargeting to non-venv dir 'C:\Users\bWX826206\AppData\Local\Programs\Python\Python37\lib'
6018 INFO: Processing pre-safe import module hook setuptools.extern.six.moves
6708 INFO: Processing pre-find module path hook site
6708 INFO: site: retargeting to fake-dir 'C:\Users\bWX826206\PycharmProjects\feature_extractor\venv\lib\site-packages\pyins
taller-4.0.dev0+a9b299995-py3.7.egg\PyInstaller\fake-modules'
16895 INFO: Processing pre-safe import module hook six.moves
17334 INFO: Processing pre-safe import module hook urllib3.packages.six.moves
49078 INFO: Processing pre-safe import module hook win32com
55056 INFO: Analyzing hidden import 'sklearn.utils._cython_blas'
55056 INFO: Analyzing hidden import 'sklearn.neighbors.typedefs'
55060 INFO: Analyzing hidden import 'sklearn.neighbors.quad_tree'
55060 INFO: Analyzing hidden import 'sklearn.tree._utils'
55162 INFO: Processing module hooks...
55162 INFO: Loading module hook "hook-certifi.py"...
55164 INFO: Loading module hook "hook-distutils.py"...
55166 INFO: Loading module hook "hook-encodings.py"...
55286 INFO: Loading module hook "hook-IPython.py"...
55306 INFO: Excluding import 'PySide'
55306 INFO: Removing import of PySide from module IPython.external.qt_loaders
55306 INFO: Excluding import 'tkinter'
55316 INFO: Removing import of tkinter from module IPython.lib.clipboard
55316 INFO: Excluding import 'gtk'
55316 INFO: Excluding import 'PyQt4'
55326 INFO: Removing import of PyQt4 from module IPython.external.qt_loaders
55326 INFO: Excluding import 'PyQt5'
55326 INFO: Removing import of PyQt5 from module IPython.external.qt_loaders
55326 INFO: Excluding import 'matplotlib'
55336 INFO: Removing import of matplotlib._pylab_helpers from module IPython.core.pylabtools
55336 INFO: Removing import of matplotlib.figure from module IPython.core.pylabtools
55336 INFO: Removing import of matplotlib from module IPython.core.pylabtools
55336 INFO: Removing import of matplotlib.pyplot from module IPython.core.pylabtools
55336 INFO: Loading module hook "hook-jedi.py"...
55670 INFO: Loading module hook "hook-jinja2.py"...
55682 INFO: Loading module hook "hook-jsonschema.py"...
55689 INFO: Loading module hook "hook-lib2to3.py"...
55696 INFO: Loading module hook "hook-llvmlite.py"...
55706 INFO: Loading module hook "hook-lxml.etree.py"...
55707 INFO: Loading module hook "hook-matplotlib.backends.py"...
56566 INFO: Matplotlib backend "GTK3Agg": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
57026 INFO: Matplotlib backend "GTK3Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
57596 INFO: Matplotlib backend "MacOSX": ignored
cannot import name '_macosx' from 'matplotlib.backends' (C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-pac
kagesmatplotlibbackends__init__.py)
58678 INFO: Matplotlib backend "nbAgg": added
59166 INFO: Matplotlib backend "Qt4Agg": ignored
Failed to import any qt binding
59616 INFO: Matplotlib backend "Qt4Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
60105 INFO: Matplotlib backend "Qt5Agg": ignored
Failed to import any qt binding
60554 INFO: Matplotlib backend "Qt5Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
61186 INFO: Matplotlib backend "TkAgg": added
61786 INFO: Matplotlib backend "TkCairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
62396 INFO: Matplotlib backend "WebAgg": added
62960 INFO: Matplotlib backend "WX": ignored
No module named 'wx'
63411 INFO: Matplotlib backend "WXAgg": ignored
No module named 'wx'
63856 INFO: Matplotlib backend "WXCairo": ignored
No module named 'wx'
64326 INFO: Matplotlib backend "agg": added
64775 INFO: Matplotlib backend "cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
65356 INFO: Matplotlib backend "pdf": added
65935 INFO: Matplotlib backend "pgf": added
66411 INFO: Matplotlib backend "ps": added
66873 INFO: Matplotlib backend "svg": added
67426 INFO: Matplotlib backend "template": added
67665 INFO: Loading module hook "hook-matplotlib.py"...
68095 INFO: Loading module hook "hook-nbconvert.py"...
68126 INFO: Loading module hook "hook-nbformat.py"...
68135 INFO: Loading module hook "hook-notebook.py"...
69426 INFO: Loading module hook "hook-numba.py"...
69430 INFO: Excluding import 'IPython'
69435 INFO: Removing import of IPython.utils.path from module numba.caching
69435 INFO: Removing import of IPython.paths from module numba.caching
69440 INFO: Excluding import 'scipy'
69446 INFO: Removing import of scipy.linalg.cython_blas from module numba.targets.linalg
69446 INFO: Removing import of scipy.linalg.cython_lapack from module numba.targets.linalg
69446 INFO: Loading module hook "hook-numpy.core.py"...
69585 INFO: Loading module hook "hook-numpy.py"...
69585 INFO: Loading module hook "hook-openpyxl.py"...
69600 INFO: Loading module hook "hook-pandas.py"...
71171 INFO: Loading module hook "hook-PIL.Image.py"...
71516 INFO: Loading module hook "hook-PIL.py"...
71516 INFO: Excluding import 'tkinter'
71516 INFO: Removing import of tkinter from module PIL.ImageTk
71516 INFO: Excluding import 'PySide'
71525 INFO: Removing import of PySide from module PIL.ImageQt
71525 INFO: Excluding import 'PyQt4'
71525 INFO: Removing import of PyQt4 from module PIL.ImageQt
71525 INFO: Excluding import 'PyQt5'
71525 INFO: Removing import of PyQt5 from module PIL.ImageQt
71535 INFO: Import to be excluded not found: 'FixTk'
71535 INFO: Loading module hook "hook-PIL.SpiderImagePlugin.py"...
71535 INFO: Excluding import 'tkinter'
71535 INFO: Import to be excluded not found: 'FixTk'
71535 INFO: Loading module hook "hook-pkg_resources.py"...
72300 INFO: Excluding import '__main__'
72305 INFO: Removing import of __main__ from module pkg_resources
72305 INFO: Loading module hook "hook-pycparser.py"...
72308 INFO: Loading module hook "hook-pydoc.py"...
72308 INFO: Loading module hook "hook-pygments.py"...
73835 INFO: Loading module hook "hook-pythoncom.py"...
74407 INFO: Loading module hook "hook-pytz.py"...
74570 INFO: Loading module hook "hook-pywintypes.py"...
75125 INFO: Loading module hook "hook-resampy.py"...
75125 INFO: Loading module hook "hook-scipy.io.matlab.py"...
75125 INFO: Loading module hook "hook-scipy.linalg.py"...
75125 INFO: Loading module hook "hook-scipy.py"...
75140 INFO: Loading module hook "hook-scipy.sparse.csgraph.py"...
75140 INFO: Loading module hook "hook-scipy.special._ellip_harm_2.py"...
75140 INFO: Loading module hook "hook-scipy.special._ufuncs.py"...
75145 INFO: Loading module hook "hook-setuptools.py"...
75985 INFO: Loading module hook "hook-sklearn.metrics.cluster.py"...
75985 WARNING: Hidden import "sklearn.utils.sparsetools._graph_validation" not found!
75985 WARNING: Hidden import "sklearn.utils.sparsetools._graph_tools" not found!
75991 INFO: Loading module hook "hook-soundfile.py"...
75993 INFO: Loading module hook "hook-sqlite3.py"...
76115 INFO: Loading module hook "hook-sysconfig.py"...
76115 INFO: Loading module hook "hook-torch.py"...
76445 INFO: Loading module hook "hook-win32com.py"...
76967 INFO: Loading module hook "hook-xml.dom.domreg.py"...
76967 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
76967 INFO: Loading module hook "hook-xml.py"...
76967 INFO: Loading module hook "hook-zmq.py"...
82475 WARNING: Hidden import "'sys/un.h':" not found!
82565 WARNING: Hidden import "fatal" not found!
82570 WARNING: Hidden import "C1083:" not found!
82635 WARNING: Hidden import "error" not found!
82641 WARNING: Hidden import "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packageszmqbackendcffi__pycach
e___cffi_ext.c(213):" not found!
82643 WARNING: Hidden import "Cannot" not found!
82645 WARNING: Hidden import "open" not found!
82645 WARNING: Hidden import "No" not found!
82650 WARNING: Hidden import "file" not found!
82651 WARNING: Hidden import "such" not found!
82653 WARNING: Hidden import "directory" not found!
82655 WARNING: Hidden import "file:" not found!
82655 WARNING: Hidden import "_cffi_ext.c" not found!
82660 WARNING: Hidden import "or" not found!
82660 INFO: Loading module hook "hook-_tkinter.py"...
82905 INFO: checking Tree
82905 INFO: Building Tree because Tree-00.toc is non existent
82905 INFO: Building Tree Tree-00.toc
83027 INFO: checking Tree
83027 INFO: Building Tree because Tree-01.toc is non existent
83027 INFO: Building Tree Tree-01.toc
83241 INFO: checking Tree
83241 INFO: Building Tree because Tree-02.toc is non existent
83241 INFO: Building Tree Tree-02.toc
83269 INFO: Looking for ctypes DLLs
83545 INFO: Analyzing run-time hooks ...
83568 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
83568 INFO: Including run-time hook 'pyi_rth_mpldata.py'
83568 INFO: Including run-time hook 'pyi_rth_pkgres.py'
83576 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
83578 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
83578 INFO: Including run-time hook 'pyi_rth_traitlets.py'
83578 INFO: Including run-time hook 'pyi_rth__tkinter.py'
83578 INFO: Including run-time hook 'pyi_rth_certifi.py'
83629 INFO: Looking for dynamic libraries
84126 INFO: Cannot get manifest resource from non-PE file C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packag
es_soundfile_dataCOPYING
84126 WARNING: Can not get binary dependencies for file: C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-package
s_soundfile_dataCOPYING
84126 WARNING: Reason: 'DOS Header magic not found.'
84747 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcythonerror.cp37-win_amd64.pyd
84755 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcython_poll.cp37-win_amd64.pyd
84767 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcython_device.cp37-win_amd64.pyd
84777 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcythonmessage.cp37-win_amd64.pyd
84787 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcython_version.cp37-win_amd64.pyd
84795 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcythonutils.cp37-win_amd64.pyd
84818 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcython_proxy_steerable.cp37-win_amd64.pyd
84826 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcythonsocket.cp37-win_amd64.pyd
84836 WARNING: lib not found: libzmq.cp37-win_amd64.pyd dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlib
site-packageszmqbackendcythoncontext.cp37-win_amd64.pyd
85855 WARNING: lib not found: tbb.dll dependency of C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagesnum
banpyufunctbbpool.cp37-win_amd64.pyd
86327 INFO: Looking for eggs
86327 INFO: Using Python library C:UsersbWX826206PycharmProjectsfeature_extractorvenvScriptspython37.dll
86327 INFO: Found binding redirects:
[]
86348 INFO: Warnings written to C:UsersbWX826206DocumentsGVF2019-10-02_09-04-03__epoch_4buildrunnerwarn-runner.txt
86820 INFO: Graph cross-reference written to C:UsersbWX826206DocumentsGVF2019-10-02_09-04-03__epoch_4buildrunnerxref-runner
.html
87107 INFO: Appending 'datas' from .spec
87169 INFO: checking PYZ
87169 INFO: Building PYZ because PYZ-00.toc is non existent
87169 INFO: Building PYZ (ZlibArchive) C:UsersbWX826206DocumentsGVF2019-10-02_09-04-03__epoch_4buildrunnerPYZ-00.pyz
92390 INFO: Building PYZ (ZlibArchive) C:UsersbWX826206DocumentsGVF2019-10-02_09-04-03__epoch_4buildrunnerPYZ-00.pyz comple
ted successfully.
92494 INFO: checking PKG
92494 INFO: Building PKG because PKG-00.toc is non existent
92494 INFO: Building PKG (CArchive) PKG-00.pkg
Traceback (most recent call last):
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvScriptspyinstaller-script.py", line 11, in <module>
load_entry_point('PyInstaller==4.0.dev0+a9b299995', 'console_scripts', 'pyinstaller')()
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
aller__main__.py", line 112, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
aller__main__.py", line 63, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
allerbuildingbuild_main.py", line 732, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
allerbuildingbuild_main.py", line 679, in build
exec(code, spec_namespace)
File "runner.spec", line 32, in <module>
console=True )
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
allerbuildingapi.py", line 435, in __init__
upx_exclude=self.upx_exclude
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
allerbuildingapi.py", line 197, in __init__
self.__postinit__()
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
allerbuildingdatastruct.py", line 158, in __postinit__
self.assemble()
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
allerbuildingapi.py", line 284, in assemble
pylib_name=pylib_name)
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
allerarchivewriters.py", line 334, in __init__
super(CArchiveWriter, self).__init__(archive_path, logical_toc)
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyInst
allerarchivewriters.py", line 62, in __init__
self._finalize()
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyIns
tallerarchivewriters.py", line 94, in _finalize
self.save_trailer(toc_pos)
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyIns
tallerarchivewriters.py", line 441, in save_trailer
tocstr = self.toc.tobinary()
File "C:UsersbWX826206PycharmProjectsfeature_extractorvenvlibsite-packagespyinstaller-4.0.dev0+a9b299995-py3.7.eggPyIns
tallerarchivewriters.py", line 266, in tobinary
flag, ord(typcd), nm + pad))
struct.error: argument out of range
In C programming, a structure (struct
) creates a type that can group items of possibly different types into one type. The Python struct module facilitates conversions between Python values and C structs represented as Python bytes objects.
About the Function
The struct.pack() converts a list of values into corresponding string types. The user should specify both the format and order of the values that should be converted.
Function Syntax and Parameters
struct.pack(format, d1, d2, ...)
The first parameter of the function represents the format string. A format string specifies how the data should be packed and unpacked.
Format Characters
Here are some of the common format characters.
- ‘c’ – character
- ‘s’ – char[]
- ‘i’ – integer
- ‘f’ – float
- ‘?’ – boolean
- ‘q’ – long long integer
The data to be packed is represented by the rest of the arguments(d1, d1, d(n)…).
Returns
The function returns a Python string type consisting of the packed data in the specified format.
Importing the Module
As of the latest Python 3.10, this module is available in the Python Standard Library under the Binary Data Services category. Make sure to implement these programs on your own. Try our Online Python Interpreter.
import struct
Simple Program to Pack Data
Here is a code example illustrating how to pack data into its binary form using struct pack.
import struct packedData = struct.pack('i 4s f', 10, b'Code', 2022) print(packedData)
Output
b'nx00x00x00Codex00xc0xfcD'
Other struct Functions to Learn
Alongside the struct pack, there are other functions within the module you should know.
struct.unpack()
Strings of binary representation are converted into their original form using this function. The return of struct.unpack()
is always a tuple.
import struct packedData = b'nx00x00x00PythonPool[email protected]x1cE' unpackedData = struct.unpack('i 4s f', packed) print(unpackedData)
Output
(10, b'PythonPool', 2500.0)
struct.calcsize()
This function calculates the size of the String representation of struct. Given that you have provided a format string.
import struct structSize = struct.calcsize('i 4s f') print("Size in bytes: {}".format(structSize))
Output
Size in bytes: 12
struct.pack() Big Endian & Little Endian Explained
The terms Big Endian and Little Endian represent the byte order based on the host system. For example, Intel x86 and AMD64 (x86-64) are little-endian processors. Motorola 68000 and PowerPC G5 are considered big-endian. There’s another category that acts as both types called bi-endian. ARM and Intel Itanium are examples of bi-endian. You can use sys.byteorder()
to check the endianness of your own system processor.
struct.pack() in Python 2 vs Python 3
In Python 2, struct.pack() always returned a string type. It is in Python 3 that the function, in certain cases, will return a bytes object. Due to a lack of support of byte objects with this function in Python 2, it considered both bytes and string to be the same when returning.
Packing/Unpacking Float using struct.pack()
If you’re packing Python float values as binary using .pack(), you should keep in mind that the C language calls Python floats as a double. If you pass the values in as a float, you will lose your values accuracy after packing. This is because C floats are less accurate than Python floats.
import struct f = open('file.bin', 'wb') value = 1.23456 data = struct.pack('f',value)
Printing this data would result in 1.2345600128173828
Instead of,
data = struct.pack('f',value)
Do this.
data = struct.pack('d',value)
Packing binary floats as doubles ensure that .pack() keeps the data’s integrity after packing or unpacking processes. Failing to do so may result in inaccurate data in the long run.
struct.pack() vs struct.pack_into()
struct.pack() | struct.pack_into() |
---|---|
Requires a format along with the values. | Requires a buffer and an offset along with the values. It can be created using the ctype module. |
Simply returns the packed data | Packs the provided data into the buffer |
Returns a string type | Do not return anything. |
Common Errors in struct.pack()
struct.pack() Argument Out of Range
Let’s look at the following code:
data_length=30 code=202 data=[51400,31400,100,51400,31400,100,51400,31400,100] for i in struct.pack('<2B%dh' % len(data), *total_data[3:len(total_data)]): checksum = checksum ^ ord(i)
Error Output
struct.error: short format requires SHRT_MIN <= number <= SHRT_MAX
The list data has the highest value of 51400. However, signed short (h) has a max value of 32768. A perfect solution is to provide an unsigned short (H) with a max value of 65535.
FAQs on Python struct pack
What is struct.iter_unpack()?
struct.iter_unpack() allows you to iterate the unpacking process of data. A buffer is required so the iteration can happen. It returns a formatted string type.
Can we struct.pack() numpy array?
No, the data present in a numpy array is already packed in memory. Therefore using struct is pointless in this scenario.
Conclusion
We have looked at struct.pack() and multiple functions that work alongside. Struct module provides us the ability to work with C-type structures and manipulate struct values.
Trending Python Articles
-
“Other Commands Don’t Work After on_message” in Discord Bots
●February 5, 2023
-
Botocore.Exceptions.NoCredentialsError: Unable to Locate Credentials
by Rahul Kumar Yadav●February 5, 2023
-
[Resolved] NameError: Name _mysql is Not Defined
by Rahul Kumar Yadav●February 5, 2023
-
Best Ways to Implement Regex New Line in Python
by Rahul Kumar Yadav●February 5, 2023
This module performs conversions between Python values and C structs represented
as Python bytes
objects. This can be used in handling binary data
stored in files or from network connections, among other sources. It uses
Format Strings as compact descriptions of the layout of the C
structs and the intended conversion to/from Python values.
Several struct
functions (and methods of Struct
) take a buffer
argument. This refers to objects that implement the Buffer Protocol and
provide either a readable or read-writable buffer. The most common types used
for that purpose are bytes
and bytearray
, but many other types
that can be viewed as an array of bytes implement the buffer protocol, so that
they can be read/filled without additional copying from a bytes
object.
7.1.1. Functions and Exceptions¶
The module defines the following exception and functions:
-
exception
struct.
error
¶ -
Exception raised on various occasions; argument is a string describing what
is wrong.
-
struct.
pack
(fmt, v1, v2, …)¶ -
Return a bytes object containing the values v1, v2, … packed according
to the format string fmt. The arguments must match the values required by
the format exactly.
-
struct.
pack_into
(fmt, buffer, offset, v1, v2, …)¶ -
Pack the values v1, v2, … according to the format string fmt and
write the packed bytes into the writable buffer buffer starting at
position offset. Note that offset is a required argument.
-
struct.
unpack
(fmt, buffer)¶ -
Unpack from the buffer buffer (presumably packed by
pack(fmt, ...)
)
according to the format string fmt. The result is a tuple even if it
contains exactly one item. The buffer’s size in bytes must match the
size required by the format, as reflected bycalcsize()
.
-
struct.
unpack_from
(fmt, buffer, offset=0)¶ -
Unpack from buffer starting at position offset, according to the format
string fmt. The result is a tuple even if it contains exactly one
item. The buffer’s size in bytes, minus offset, must be at least
the size required by the format, as reflected bycalcsize()
.
-
struct.
iter_unpack
(fmt, buffer)¶ -
Iteratively unpack from the buffer buffer according to the format
string fmt. This function returns an iterator which will read
equally-sized chunks from the buffer until all its contents have been
consumed. The buffer’s size in bytes must be a multiple of the size
required by the format, as reflected bycalcsize()
.Each iteration yields a tuple as specified by the format string.
New in version 3.4.
-
struct.
calcsize
(fmt)¶ -
Return the size of the struct (and hence of the bytes object produced by
pack(fmt, ...)
) corresponding to the format string fmt.
7.1.2. Format Strings¶
Format strings are the mechanism used to specify the expected layout when
packing and unpacking data. They are built up from Format Characters,
which specify the type of data being packed/unpacked. In addition, there are
special characters for controlling the Byte Order, Size, and Alignment.
7.1.2.1. Byte Order, Size, and Alignment¶
By default, C types are represented in the machine’s native format and byte
order, and properly aligned by skipping pad bytes if necessary (according to the
rules used by the C compiler).
Alternatively, the first character of the format string can be used to indicate
the byte order, size and alignment of the packed data, according to the
following table:
Character | Byte order | Size | Alignment |
---|---|---|---|
@ |
native | native | native |
= |
native | standard | none |
< |
little-endian | standard | none |
> |
big-endian | standard | none |
! |
network (= big-endian) | standard | none |
If the first character is not one of these, '@'
is assumed.
Native byte order is big-endian or little-endian, depending on the host
system. For example, Intel x86 and AMD64 (x86-64) are little-endian;
Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature
switchable endianness (bi-endian). Use sys.byteorder
to check the
endianness of your system.
Native size and alignment are determined using the C compiler’s
sizeof
expression. This is always combined with native byte order.
Standard size depends only on the format character; see the table in
the Format Characters section.
Note the difference between '@'
and '='
: both use native byte order, but
the size and alignment of the latter is standardized.
The form '!'
is available for those poor souls who claim they can’t remember
whether network byte order is big-endian or little-endian.
There is no way to indicate non-native byte order (force byte-swapping); use the
appropriate choice of '<'
or '>'
.
Notes:
- Padding is only automatically added between successive structure members.
No padding is added at the beginning or the end of the encoded struct. - No padding is added when using non-native size and alignment, e.g.
with ‘<’, ‘>’, ‘=’, and ‘!’. - To align the end of a structure to the alignment requirement of a
particular type, end the format with the code for that type with a repeat
count of zero. See Examples.
7.1.2.2. Format Characters¶
Format characters have the following meaning; the conversion between C and
Python values should be obvious given their types. The ‘Standard size’ column
refers to the size of the packed value in bytes when using standard size; that
is, when the format string starts with one of '<'
, '>'
, '!'
or
'='
. When using native size, the size of the packed value is
platform-dependent.
Format | C Type | Python type | Standard size | Notes |
---|---|---|---|---|
x |
pad byte | no value | ||
c |
char |
bytes of length 1 | 1 | |
b |
signed char |
integer | 1 | (1),(3) |
B |
unsigned char |
integer | 1 | (3) |
? |
_Bool |
bool | 1 | (1) |
h |
short |
integer | 2 | (3) |
H |
unsigned short |
integer | 2 | (3) |
i |
int |
integer | 4 | (3) |
I |
unsigned int |
integer | 4 | (3) |
l |
long |
integer | 4 | (3) |
L |
unsigned long |
integer | 4 | (3) |
q |
long long |
integer | 8 | (2), (3) |
Q |
unsigned long |
integer | 8 | (2), (3) |
n |
ssize_t |
integer | (4) | |
N |
size_t |
integer | (4) | |
e |
(7) | float | 2 | (5) |
f |
float |
float | 4 | (5) |
d |
double |
float | 8 | (5) |
s |
char[] |
bytes | ||
p |
char[] |
bytes | ||
P |
void * |
integer | (6) |
Changed in version 3.3: Added support for the 'n'
and 'N'
formats.
Changed in version 3.6: Added support for the 'e'
format.
Notes:
-
The
'?'
conversion code corresponds to the_Bool
type defined by
C99. If this type is not available, it is simulated using achar
. In
standard mode, it is always represented by one byte. -
The
'q'
and'Q'
conversion codes are available in native mode only if
the platform C compiler supports Clong long
, or, on Windows,
__int64
. They are always available in standard modes. -
When attempting to pack a non-integer using any of the integer conversion
codes, if the non-integer has a__index__()
method then that method is
called to convert the argument to an integer before packing.Changed in version 3.2: Use of the
__index__()
method for non-integers is new in 3.2. -
The
'n'
and'N'
conversion codes are only available for the native
size (selected as the default or with the'@'
byte order character).
For the standard size, you can use whichever of the other integer formats
fits your application. -
For the
'f'
,'d'
and'e'
conversion codes, the packed
representation uses the IEEE 754 binary32, binary64 or binary16 format (for
'f'
,'d'
or'e'
respectively), regardless of the floating-point
format used by the platform. -
The
'P'
format character is only available for the native byte ordering
(selected as the default or with the'@'
byte order character). The byte
order character'='
chooses to use little- or big-endian ordering based
on the host system. The struct module does not interpret this as native
ordering, so the'P'
format is not available. -
The IEEE 754 binary16 “half precision” type was introduced in the 2008
revision of the IEEE 754 standard. It has a sign
bit, a 5-bit exponent and 11-bit precision (with 10 bits explicitly stored),
and can represent numbers between approximately6.1e-05
and6.5e+04
at full precision. This type is not widely supported by C compilers: on a
typical machine, an unsigned short can be used for storage, but not for math
operations. See the Wikipedia page on the half-precision floating-point
format for more information.
A format character may be preceded by an integral repeat count. For example,
the format string '4h'
means exactly the same as 'hhhh'
.
Whitespace characters between formats are ignored; a count and its format must
not contain whitespace though.
For the 's'
format character, the count is interpreted as the length of the
bytes, not a repeat count like for the other format characters; for example,
'10s'
means a single 10-byte string, while '10c'
means 10 characters.
If a count is not given, it defaults to 1. For packing, the string is
truncated or padded with null bytes as appropriate to make it fit. For
unpacking, the resulting bytes object always has exactly the specified number
of bytes. As a special case, '0s'
means a single, empty string (while
'0c'
means 0 characters).
When packing a value x
using one of the integer formats ('b'
,
'B'
, 'h'
, 'H'
, 'i'
, 'I'
, 'l'
, 'L'
,
'q'
, 'Q'
), if x
is outside the valid range for that format
then struct.error
is raised.
Changed in version 3.1: In 3.0, some of the integer formats wrapped out-of-range values and
raised DeprecationWarning
instead of struct.error
.
The 'p'
format character encodes a “Pascal string”, meaning a short
variable-length string stored in a fixed number of bytes, given by the count.
The first byte stored is the length of the string, or 255, whichever is
smaller. The bytes of the string follow. If the string passed in to
pack()
is too long (longer than the count minus 1), only the leading
count-1
bytes of the string are stored. If the string is shorter than
count-1
, it is padded with null bytes so that exactly count bytes in all
are used. Note that for unpack()
, the 'p'
format character consumes
count
bytes, but that the string returned can never contain more than 255
bytes.
For the '?'
format character, the return value is either True
or
False
. When packing, the truth value of the argument object is used.
Either 0 or 1 in the native or standard bool representation will be packed, and
any non-zero value will be True
when unpacking.
7.1.2.3. Examples¶
Note
All examples assume a native byte order, size, and alignment with a
big-endian machine.
A basic example of packing/unpacking three integers:
>>> from struct import * >>> pack('hhl', 1, 2, 3) b'x00x01x00x02x00x00x00x03' >>> unpack('hhl', b'x00x01x00x02x00x00x00x03') (1, 2, 3) >>> calcsize('hhl') 8
Unpacked fields can be named by assigning them to variables or by wrapping
the result in a named tuple:
>>> record = b'raymond x32x12x08x01x08' >>> name, serialnum, school, gradelevel = unpack('<10sHHb', record) >>> from collections import namedtuple >>> Student = namedtuple('Student', 'name serialnum school gradelevel') >>> Student._make(unpack('<10sHHb', record)) Student(name=b'raymond ', serialnum=4658, school=264, gradelevel=8)
The ordering of format characters may have an impact on size since the padding
needed to satisfy alignment requirements is different:
>>> pack('ci', b'*', 0x12131415) b'*x00x00x00x12x13x14x15' >>> pack('ic', 0x12131415, b'*') b'x12x13x14x15*' >>> calcsize('ci') 8 >>> calcsize('ic') 5
The following format 'llh0l'
specifies two pad bytes at the end, assuming
longs are aligned on 4-byte boundaries:
>>> pack('llh0l', 1, 2, 3) b'x00x00x00x01x00x00x00x02x00x03x00x00'
This only works when native size and alignment are in effect; standard size and
alignment does not enforce any alignment.
See also
- Module
array
- Packed binary storage of homogeneous data.
- Module
xdrlib
- Packing and unpacking of XDR data.
7.1.3. Classes¶
The struct
module also defines the following type:
-
class
struct.
Struct
(format)¶ -
Return a new Struct object which writes and reads binary data according to
the format string format. Creating a Struct object once and calling its
methods is more efficient than calling thestruct
functions with the
same format since the format string only needs to be compiled once.Compiled Struct objects support the following methods and attributes:
-
pack
(v1, v2, …)¶ -
Identical to the
pack()
function, using the compiled format.
(len(result)
will equalsize
.)
-
pack_into
(buffer, offset, v1, v2, …)¶ -
Identical to the
pack_into()
function, using the compiled format.
-
unpack
(buffer)¶ -
Identical to the
unpack()
function, using the compiled format.
The buffer’s size in bytes must equalsize
.
-
unpack_from
(buffer, offset=0)¶ -
Identical to the
unpack_from()
function, using the compiled format.
The buffer’s size in bytes, minus offset, must be at least
size
.
-
iter_unpack
(buffer)¶ -
Identical to the
iter_unpack()
function, using the compiled format.
The buffer’s size in bytes must be a multiple ofsize
.New in version 3.4.
-
format
¶ -
The format string used to construct this Struct object.
-
size
¶ -
The calculated size of the struct (and hence of the bytes object produced
by thepack()
method) corresponding toformat
.
-
Я новичок в питоне. Я пытаюсь использовать оболочку телемарафона для добавления и удаления пользователей из чата. Я использую взятый здесь пример кода https://tl.telethon.dev/methods/ messages / add_chat_user.html
Мой код:
(ID и хеш я заменил в тексте на произвольные числа)
from telethon.sync import TelegramClient
from telethon import functions, types
from telethon.errors import ChatIdInvalidError
from telethon.errors import PeerIdInvalidError
from telethon.errors import UserNotParticipantError
from telethon.tl.functions.messages import DeleteChatUserRequest
from telethon.tl.functions.messages import AddChatUserRequest
api_id = 1234567
api_hash = 'blablablabla123456'
phone = '+123456789'
name = 'testname'
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.messages.AddChatUserRequest(
chat_id=12345678912,
user_id='username',
fwd_limit=42
))
print(result.stringify())
Но, к сожалению, для меня это не работает и выдает ошибку.
(ID и хеш я заменил в тексте произвольными числами.)
Request caused struct.error: argument out of range: AddChatUserRequest(chat_id=123456789, user_id=InputUser(user_id=123456789, access_hash=-123456789789), fwd_limit=42)
Traceback (most recent call last):
File "d:pythonAddUsernewfrom telethon.tl.functions.messages impo.py", line 18, in <module>
result = client(functions.messages.AddChatUserRequest(
File "c:UsersUserAppDataLocalProgramsPythonPython39libsite-packagestelethonsync.py", line 39, in syncified
return loop.run_until_complete(coro)
File "c:UsersUserAppDataLocalProgramsPythonPython39libasynciobase_events.py", line 642, in run_until_complete
return future.result()
File "c:UsersUserAppDataLocalProgramsPythonPython39libsite-packagestelethonclientusers.py", line 30, in __call__
return await self._call(self._sender, request, ordered=ordered)
File "c:UsersUserAppDataLocalProgramsPythonPython39libsite-packagestelethonclientusers.py", line 58, in _call
future = sender.send(request, ordered=ordered)
File "c:UsersUserAppDataLocalProgramsPythonPython39libsite-packagestelethonnetworkmtprotosender.py", line 176, in send
state = RequestState(request)
File "c:UsersUserAppDataLocalProgramsPythonPython39libsite-packagestelethonnetworkrequeststate.py", line 17, in __init__
self.data = bytes(request)
File "c:UsersUserAppDataLocalProgramsPythonPython39libsite-packagestelethontltlobject.py", line 194, in __bytes__
return self._bytes()
File "c:UsersUserAppDataLocalProgramsPythonPython39libsite-packagestelethontlfunctionsmessages.py", line 139, in _bytes
struct.pack('<i', self.chat_id),
struct.error: argument out of range
Telethon ver. 1.22.0
Буду благодарен за любую помощь
Спасибо!
2 ответа
Лучший ответ
Ошибка «аргумент вне допустимого диапазона», которую я указал в вопросе, возникла из-за того, что я пытался указать 40-байтовый Chat_id. В то время как для обычных чатов максимальный идентификатор составляет 32 байта.
Это произошло из-за того, что вам нужно указать так называемый «настоящий» id. Например, идентификатор, который получают боты Telegram, выглядит примерно так -1001234567891, реальный идентификатор будет 1234567891.
Вы можете использовать следующий код, чтобы получить настоящий идентификатор:
from telethon import utils
real_id, peer_type = utils.resolve_id(-1001234567891)
print(real_id) # 1234567891
print(peer_type) # <class 'telethon.tl.types.PeerChannel'>
peer = peer_type(real_id)
print(peer) # PeerChannel(channel_id=1234567891)
Однако, если вы укажете реальный идентификатор, может появиться ошибка ChatIdInvalidError. Проблема в том, что метод AddChatUserRequest можно использовать только с обычными «чатами». В моем случае это была «супергруппа» — это канал с атрибутом channel.megagroup, установленным на True.
Для них нужно использовать метод InviteToChannelRequest, код, который работал, был таким:
from telethon.sync import TelegramClient
from telethon import functions, types
api_id = 123456
api_hash = '******'
name = 'name'
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.channels.InviteToChannelRequest(
channel='channelname',
users = ['username']
))
print(result.stringify())
Затем я создал обычную «чат» группу.
Для нее рабочий код был таким:
from telethon.sync import TelegramClient
from telethon import functions, types
api_id = 123456
api_hash = '*******'
name = 'name'
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.messages.AddChatUserRequest(
chat_id=chatid,
user_id='username',
fwd_limit=42
))
print(result.stringify())
3
Aleksandr Filippov
8 Июл 2021 в 21:43
Использование:
result = await client(functions.messages.AddChatUserRequest(
chat_id=12345678912,
user_id='username',
fwd_limit=42
))
Вместо того:
result = client(functions.messages.AddChatUserRequest(
chat_id=12345678912,
user_id='username',
fwd_limit=42
))
-1
SAL
6 Июл 2021 в 00:14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
Добрый день друзья. Использую данный метод в своей игре:
public void GenerateLevel()
{
Word = "GALAXY"; Hint = "Hint for answer"; Path = "Sprites/Img1";
ImageWord = Resources.Load<Sprite>(Path);
ImageDefault.sprite = ImageWord;
tempCell = new GameObject[Word.Length];
checkAddDel = new int[Word.Length];
RandomSym = new string[14 - Word.Length];
answerSym = new string[Word.Length];
foreach (char ch in Word.ToUpper().ToCharArray())
{
int rnd = Random.Range(0, AllSymbols.Count);
while (AllSymbols[rnd].GetComponent<Text>().text != "-")
rnd = Random.Range(0, AllSymbols.Count);
AllSymbols[rnd].GetComponent<Text>().text = ch.ToString();
totalSymbol++; // Считаем символы
answerSym[answChar] = AllSymbols[rnd].GetComponent<Text>().text;
answChar++;
}
foreach (GameObject go in AllSymbols) // Заполняем пустые кубики
{
if (go.GetComponent<Text>().text == "-")
{
go.GetComponent<Text>().text = ((char)Random.Range(65, 91)).ToString(); // 65,91 - коды Латинских символов
RandomSym[randChar] = go.GetComponent<Text>().text;
randChar++;
}
}
foreach (char ch in Word.ToUpper().ToCharArray())
{
tempCell[indexTemp] = Instantiate(PrefabCharCell);
tempCell[indexTemp].transform.SetParent(GridCellPos, false);
int tName = indexTemp;
tempCell[indexTemp].GetComponent<Button>().onClick.AddListener(() => DeleteSymbol(tName));
tempCell[indexTemp].gameObject.name = indexTemp.ToString();
indexTemp++;
}
}
Он генерирует слово, разбивает на символы, в случайном порядке их расставляет. Здесь переменная Word определена, в итоге и переменная Hint и Path приходит с кнопки, события OnClick.
Первый раз, при запуске сцены, метод отрабатывает хорошо, без ошибок.
После того, как слово отгадано, я обнуляю все переменные другим методом
Используется csharp
public void CleanLevel()
{
for (int i = 0; i < Word.Length; i++)
tempCell[i].GetComponentInChildren<Text>().text = "";
Word = ""; Hint = ""; Path = ""; totalSymbol = 0; indexTemp = 0; numChar = 0; tempAnswer = 0; flChar = 0; randChar = 0; answChar = 0;
AllSymbols.Clear();
GoodAnswerPanel.SetActive(false);
checkAddDel = new int[Word.Length];
RandomSym = new string[14 - Word.Length];
answerSym = new string[Word.Length];
}
В итоге жму на кнопку с уже другими переменными Word, Hint, Path и вываливается ошибка:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
Не могу понять, в чём проблема и почему так происходит. Первые строчки точно отрабатывают нормально, так как меняется Path.
Подскажите пожалуйста, что не так делаю, всю голову сломал. Спасибо огромное заранее!