What is the Python “Magic” method used in the “bafang-python” package?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



What is the Python “Magic” method used in the “bafang-python” package?



I have been trying to get this piece of code working:



https://github.com/philippsandhaus/bafang-python



There is this Magic() method which only confuses me because Python has some magic methods also. I'm also a beginner in Python.


Magic()



The problem is in this part:


connect_cmd = Struct('connect',
Magic('x11x51x04xB0x05')
)



It is a serial message but what is Magic() and how I can get it working?


Magic()





This is part of why import * makes code hard to read -- it's not obvious what was imported from where.
– Charles Duffy
Aug 10 at 17:34


import *





It is probably defined in construct : from construct import *
– eyllanesc
Aug 10 at 17:35


from construct import *





On PyPI magic module is to interface with libmagic, maybe that is it?
– Severin Pappadeux
Aug 10 at 17:36





It's part of an old version of construct; in the new version it's renamed Const. See github.com/construct/construct/blob/…
– Charles Duffy
Aug 10 at 17:36



construct


Const





Your problem illustrates one of the reasons the python from xyz import * type of import is not a good idea: you have to guess where a reference comes from. In this case it looks like the construct import provides Magic and Struct and others - and construct isn’t in the github. Go ask the author?
– barny
Aug 10 at 17:40



from xyz import *


construct


construct




2 Answers
2



construct.Magic is the old name for construct.Const. (This code was presumably written for a version of the construct library before 2.8).


construct.Magic


construct.Const


construct



See https://github.com/construct/construct/blob/master/docs/transition28.rst, describing a large series of renames:



Magic was made Const


Magic


Const



The reason for the name can be inferred from the documentation (emphasis added):



Const -
A constant value that is required to exist in the data and match a given value. If the value is not matching, ConstError is raised. Useful for so called magic numbers, signatures, asserting correct protocol version, etc.





A side note, only because I had something very different in my head when I read the question title; ipython has magic commands. I've seen them called "magic methods" and the IDE I use is for Python but supports IPython magic commands in the console.
– roganjosh
Aug 10 at 17:41




"Magic" methods in python are the methods which provide operator overloading. The term "magic" does not appear in the python language with reference to these methods, and many users prefer not to use the term as it suggests incomprehensibility. See this tutorial for more information on these if needed.



The Magic function (or possibly class) cited in your link is not defined in the context of that repo, so it is not possible to say what it does with the information provided. Possibly more research into the committer's other repositories would turn up the construct module that he imports from.


Magic


construct



EDIT: Apparently there is a library called construct which is well known to a subset of python users, but not explicitly referenced by the author you cite. Sigh. https://github.com/MostAwesomeDude/construct


construct





Wasn't well-known to me, btw -- I found it via its PyPi entry. Since PyPi is used by pip, it's the closest thing to a canonical index of Python modules. If bafang-python had had a setup.py that called out the dependency, though, there would have certainly been less guesswork.
– Charles Duffy
Aug 10 at 17:44



bafang-python


setup.py





So how do I install it (construct)?
– Juhni
Aug 10 at 18:41





It seems to be pip-installable, so pip install construct should get you there. However, in the long run global installs cause heartache and regret, so I suggest working in a virtual environment. (details are searchable, out of scope for this comment)
– Jon Kiparsky
Aug 10 at 18:54


pip install construct





...doubly so if you want to install an older version of construct that the code will work with unmodified -- using a virtualenv means you aren't exposing old code to every Python user on your machine (and thus conflicting with any software that needs an up-to-date version of the same library). pip install 'construct<2.8' should get you the most recent pre-2.8 version.
– Charles Duffy
Aug 10 at 21:16



construct


pip install 'construct<2.8'






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard