Python 2.7 Error

Asked by ian@iwjohnston.com

Referring to section 2.3 of the nfcpy documentation, (Read and Write Tags), Using the example shown, I keep getting the error as shown below. Not sure if it is my knowledge of Python of some other issue:

>>> import nfc
>>> def connected(tag): print(tag); return False
...
>>> clf = nfc.ContactlessFrontend('usb')
>>> clf.connect(rdwr={'on-connect': connected})
Type3Tag 'FeliCa Lite-S (RC-S966)' ID=012E34E829CE1E71 PMM=00F1000000014300 SYS=88B4
<nfc.tag.tt3_sony.FelicaLiteS object at 0x765d9310>
>>> print(tag.ndef.message.pretty() if tag.ndef else "Sorry")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tag' is not defined
>>>

Regards,
Ian

Question information

Language:
English Edit question
Status:
Solved
For:
nfcpy Edit question
Assignee:
No assignee Edit question
Solved by:
Stephen Tiedemann
Solved:
Last query:
Last reply:
Revision history for this message
Best Stephen Tiedemann (stephen-tiedemann) said :
#1

This is a documentation error. The captured card must be assigned to the "tag" variable for further example code to work. If you reload the documentation page (be careful of browser cache) it should now read as:

tag = clf.connect(rdwr={'on-connect': connected})

Revision history for this message
kyle (kyldu) said :
#2

I am having a similar issue with the same part of the documentation, although my error reads:

AttributeError: 'NDEF' object has no attribute 'Message'

Any suggestions?

My code:

def connected(tag):
    freq = 2500
    dur = 500
    winsound.Beep(freq,dur)
    ndefData = tag.ndef.Message.pretty() if nfc.ndef else 'No NDEF'
    print (ndefData)

rdwrOptions = {'on-connect':connected}
clf = nfc.ContactlessFrontend('usb')
clf.connect(rdwr=rdwrOptions)

Revision history for this message
ian@iwjohnston.com (ian-5) said :
#3

Hello Stephen,

This fixed the issue. I was sure it was a doco issue but not sure what.

Thx again,
Ian

Revision history for this message
ian@iwjohnston.com (ian-5) said :
#4

Hello,

I’m no expert on this stuff, but you may need to change “Message” to “message” in the code. All the refs in the nfc module are to “message"

Ian

> On 12 Dec 2016, at 11:43 , kyle <email address hidden> wrote:
>
> Your question #406611 on nfcpy changed:
> https://answers.launchpad.net/nfcpy/+question/406611
>
> kyle requested more information:
> I am having a similar issue with the same part of the documentation,
> although my error reads:
>
> AttributeError: 'NDEF' object has no attribute 'Message'
>
> Any suggestions?
>
> My code:
>
> def connected(tag):
> freq = 2500
> dur = 500
> winsound.Beep(freq,dur)
> ndefData = tag.ndef.Message.pretty() if nfc.ndef else 'No NDEF'
> print (ndefData)
>
> rdwrOptions = {'on-connect':connected}
> clf = nfc.ContactlessFrontend('usb')
> clf.connect(rdwr=rdwrOptions)
>
> --
> To answer this request for more information, you can either reply to
> this email or enter your reply at the following page:
> https://answers.launchpad.net/nfcpy/+question/406611
>
> You received this question notification because you asked the question.