python_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python_examples [2018/01/05 16:01] – g00r00 | python_examples [2019/03/09 21:40] (current) – [Simple File Lister] g00r00 | ||
---|---|---|---|
Line 129: | Line 129: | ||
<file python mcihelp.mpy> | <file python mcihelp.mpy> | ||
- | |||
import mystic_bbs as bbs; | import mystic_bbs as bbs; | ||
Line 153: | Line 152: | ||
bbs.writeln(" | bbs.writeln(" | ||
+ | </ | ||
+ | |||
+ | ----- | ||
+ | ===== Simple File Lister ===== | ||
+ | |||
+ | <file python filelist.mpy> | ||
+ | ################################################### | ||
+ | # Simple File Listing Example using Mystic Python # | ||
+ | ################################################### | ||
+ | |||
+ | # List files in the user's current file base | ||
+ | |||
+ | from mystic_bbs import * | ||
+ | |||
+ | # filelist flags | ||
+ | |||
+ | file_offline = int(" | ||
+ | file_invalid = int(" | ||
+ | file_deleted = int(" | ||
+ | file_failed | ||
+ | file_free | ||
+ | file_hatched = int(" | ||
+ | |||
+ | # Load the current user and then load their current file base | ||
+ | # but fail if they have not selected a file base | ||
+ | |||
+ | user = getuser(0) | ||
+ | fbase = getfbaseid(user[" | ||
+ | |||
+ | if fbase is None: | ||
+ | writeln (" | ||
+ | quit() | ||
+ | |||
+ | # Open the file list then check to make sure its open before reading | ||
+ | # data from it | ||
+ | |||
+ | flist = fl_open(fbase[" | ||
+ | |||
+ | if flist is None: | ||
+ | writeln(" | ||
+ | quit() | ||
+ | |||
+ | done = False | ||
+ | |||
+ | # Seek to the first file in the list and loop while a file is found calling | ||
+ | # next after each file so the next one will be loaded. | ||
+ | # called before cycling through the list | ||
+ | |||
+ | fl_seek(flist, | ||
+ | |||
+ | while fl_found(flist) and not done and not shutdown(): | ||
+ | |||
+ | # Load the file information into a dictionary and then get the file | ||
+ | # description | ||
+ | |||
+ | fileinfo = fl_getfile(flist) | ||
+ | filedesc = fl_getdesc(flist) | ||
+ | |||
+ | # Show the file | ||
+ | |||
+ | writeln(" | ||
+ | writeln("" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | |||
+ | flags = "" | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + "FREE " | ||
+ | |||
+ | if fileinfo[" | ||
+ | flags = flags + " | ||
+ | |||
+ | if flags == "": | ||
+ | flags = " | ||
+ | |||
+ | writeln(" | ||
+ | writeln("" | ||
+ | writeln(" | ||
+ | writeln(" | ||
+ | |||
+ | # only print up to the first 10 lines so we can fit it on the screen | ||
+ | |||
+ | for line in range(min(fileinfo[" | ||
+ | writeln(filedesc[line]) | ||
+ | |||
+ | writeln("" | ||
+ | write(" | ||
+ | |||
+ | ch = onekey(chr(13) + ' | ||
+ | |||
+ | if ch == ' | ||
+ | # do nothing here so it redisplays the same file | ||
+ | pass | ||
+ | |||
+ | elif ch == ' | ||
+ | fl_prev(flist) | ||
+ | |||
+ | elif ch == ' | ||
+ | done = True | ||
+ | break | ||
+ | else: | ||
+ | fl_next(flist); | ||
+ | |||
+ | # Close the file list and report that we're done | ||
+ | |||
+ | fl_close(flist) | ||
+ | |||
+ | writeln(" | ||
+ | </ | ||
+ | |||
+ | ===== View Dictionaries and Lists ===== | ||
+ | <file python dump.mpy> | ||
+ | from mystic_bbs import * | ||
+ | |||
+ | # Dump contents of a dictionary or list to the screen with | ||
+ | # screen pause | ||
+ | |||
+ | def dump (obj, linecount): | ||
+ | if linecount == 1: | ||
+ | textcolor(7) | ||
+ | if type(obj) == dict: | ||
+ | for k, v in obj.items(): | ||
+ | if hasattr(v, ' | ||
+ | writeln(k) | ||
+ | dump(v, linecount) | ||
+ | else: | ||
+ | writeln ("{} : {}" | ||
+ | linecount = linecount + 1 | ||
+ | if linecount == 23: | ||
+ | write(" | ||
+ | elif type(obj) == list: | ||
+ | for v in obj: | ||
+ | if hasattr(v, ' | ||
+ | dump(v, linecount) | ||
+ | else: | ||
+ | writeln (" | ||
+ | linecount = linecount + 1 | ||
+ | if linecount == 23: | ||
+ | write(" | ||
+ | else: | ||
+ | writeln(" | ||
+ | linecount = linecount + 1 | ||
+ | if linecount == 23: | ||
+ | write(" | ||
+ | |||
+ | # grab the current user and dump the contents of their user dictionary | ||
+ | # This function can be used to dump anything, users, bases, groups, etc | ||
+ | |||
+ | writeln(" | ||
+ | user = getuser(0) | ||
+ | dump(user, 1) | ||
</ | </ |
python_examples.1515189700.txt.gz · Last modified: 2018/01/05 16:01 by g00r00