● why python? ● what is & isn't possible ● pitfalls ● tips & tricks /...
TRANSCRIPT
![Page 1: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/1.jpg)
What I won't tell you
● General tutorial in how to use Python– Would take too long– Available from several other places (UCS,
python.org website, elsewhere on web)
● Generating pulse programs with Python– I haven't needed to do this myself
![Page 2: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/2.jpg)
What is Python?
●Named after Monty Python●Interpreted (not compiled) language (“script”)●Aims for code readability & re-use●Features that cause pitfalls with Topspin + Python:
–Leading whitespace (space vs tab) is significant
–Pass-by-reference
![Page 3: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/3.jpg)
Why Python?
● Allows automating tedious tasks● Easier & quicker than C ('AU' programs)
– Python has a more complete standard library– Python does not require type declarations– Many AU commands risk causing buffer overruns
● Record of what you did last time
![Page 4: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/4.jpg)
Getting started (1)
![Page 5: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/5.jpg)
Getting started (2)
![Page 6: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/6.jpg)
Getting started (3)
![Page 7: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/7.jpg)
Getting started (4)
![Page 8: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/8.jpg)
Whitespace problems (1)Spot the difference between the two script files...
Incorrect Correct
![Page 9: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/9.jpg)
Whitespace problems (1)Spot the difference between the two script files...
Incorrect CorrectHere!
![Page 10: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/10.jpg)
Whitespace problems (2)
•Default indent in the topspin editor is tab•Default indent in code distributed with Topspin is two spaces•Editor displays a tab as two spaces•Confusing errors!
![Page 11: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/11.jpg)
Whitespace problems (3)
![Page 12: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/12.jpg)
Whitespace problems (4)
![Page 13: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/13.jpg)
Whitespace problems fixed...
![Page 14: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/14.jpg)
Pass / copy by reference
● Every variable in Python is a reference● Can't tell the difference for immutable data● For mutable data structures (lists etc.) this can
give surprising results
![Page 15: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/15.jpg)
Reference vs Value (immutable)
![Page 16: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/16.jpg)
Reference vs Value (mutable)Value: Word Document attachmentReference: Google Docs
![Page 17: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/17.jpg)
Workaround – deep copy
![Page 18: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/18.jpg)
Tips & tricks 1 – look in TopCmds.py
![Page 19: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/19.jpg)
Tips & Tricks 2 – text files in exp folder
![Page 20: ● Why Python? ● What is & isn't possible ● Pitfalls ● Tips & tricks / workarounds ● Real-life example: automatically run efp on all one- dimensional data](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649cfe5503460f949cf38e/html5/thumbnails/20.jpg)
More info about Python
● Online tutorial at Python website– http://docs.python.org/2/tutorial/
● UCS courses– http://www.training.cam.ac.uk/ucs/search?query=P
ython
● Download (to use Python outside Topspin)– http://www.python.org/download/