Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Update documentation. Fix placing of .close() prematurely due to empt…
Browse files Browse the repository at this point in the history
…y lines
  • Loading branch information
alixander committed Dec 30, 2014
1 parent d38dc8d commit 2fac9bb
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 88 deletions.
35 changes: 23 additions & 12 deletions README.md
Expand Up @@ -23,26 +23,43 @@ $ pip uninstall pyscribe
Usage
------
1. Include `import pyscribe` at the top of the files you are debugging.
2. Initialize a variable of your choice to `pyscriber.Scribe()` (E.g.: `ps = pyscriber.Scribe()`)
2. Initialize a variable of your choice to `pyscribe.Scriber()` (E.g.: `ps = pyscribe.Scriber()`)
3. Make API calls as needed. (E.g.: `ps.p(x)`)
4. Run
```bash
$ pyscribe myfile.py
$ pyscribe myfile.py --run
```
This outputs a myfile_desugared.py, which is intended to be run to debug.
This is the equivalent of running `$ python myfile.py` with all calls desugared.
```bash
$ pyscribe myfile.py --run --extraargs "-u asdf"
```
This is the equivalent of running `$ python myfile.py -u asdf` with all calls desugared.
```bash
$ pyscribe myfile.py --desugared
```
This does not run anything, but rather outputs a myfile_desugared.py, which is intended to be run to debug.


Argument Options
-----------------
- `--run` -- Run the desugared version
- `--extraargs` -- Arguments intended to be passed to Python file when run. Must be called with --run set
- `--clean` -- Produce a clean version of the file with all references to PyScribe removed
- `--desugared` -- Produce a desugared version of the file with all API calls replaced with valid Python.

API Calls
----------
- `pyscribe.scribe(object, label=None)` -- Logs the object value with relevant info dependent on type
- `pyscribe.is_debugging(boolean)` -- Enable or disable the library
- `pyscribe.log_lines(boolean)` -- Insert a "from line xx" before each line
- `pyscribe.save_logs(boolean)` -- Save the logs with a timestamp in a file
- `pyscribe.iterscribe(object)` -- Log the object value from inside a for or while loop which prints current iteration
- `pyscribe.watch(object)` -- Log the object whenever its value changes
- `pyscribe.scribevalues(object)` -- Log the internal values of lists and dictionaries in a pretty way
- `pyscribe.scribestate()` -- Log every variable within the current scope
- `pyscribe.distinguish(object, unit="*")` -- Distinguish the log with a clear separator defined by the unit

Planned
----------
- `pyscribe.filter_labels(list)` -- Only log the pyscribe.scribe calls that are labeled with a label in the list
- `pyscribe.scribevalues(object)` -- Log the internal values of lists and dictionaries in a pretty way

Example
--------
Expand Down Expand Up @@ -75,12 +92,6 @@ y = "world" # From line xx: y changed from the string 'hello' to the string 'wor

synonyms = {"clerk": "secretary", "student": "apprentice", "ground": "floor"}
scriber.scribe(synonyms) # From line xx: synonyms is a dictionary of length 3
scriber.scribevalues(synonyms) # From line xx: synonyms contains the following values:
# ========================
# 'clerk': 'secretary'
# 'student': 'apprentice'
# 'ground': 'floor'
# ========================
```

####logs.txt:
Expand Down
10 changes: 8 additions & 2 deletions pyscribe/pyscribe.py
Expand Up @@ -55,6 +55,9 @@ def iterscribe(self, obj):
def d(self, obj, unit="-"):
pass

def save_logs(self, do_save):
pass


class Watcher(object):
def __init__(self):
Expand Down Expand Up @@ -150,18 +153,20 @@ def gen_desugared(self, line_mapping, program_file, program_ast):
self.save_logs and
self.initialized and
first_call_indentation != "" and
len(line_content) > 2 and
len(indentation) < len(first_call_indentation)):
closing_line = (first_call_indentation +
"pyscribe_log.close()\n")
closing_line_added = True
self.desugared_lines.append(closing_line)
if "Scriber()" in line_content: # Line matches initial call
self.initialized = True
if self.save_logs:
self.initialized = True
desugared_line = (indentation +
"pyscribe_log = open('pyscribe_logs.txt', 'w')\n")
self.desugared_lines.append(desugared_line)
first_call_indentation = indentation
self.desugared_lines.append(line_content)
elif line_content in line_mapping.values(): # Line matches an API call
self.desugared_lines.append(self.desugar_line(line_content[:-1],
line_num,
Expand Down Expand Up @@ -250,9 +255,10 @@ def iter_start(self, node, line, line_num, program_ast, indentation):
def iterscribe(self, line, line_num, indentation, program_ast):
variable_id, variable_type = utils.get_id_and_type(line, program_ast)
for node in ast.walk(program_ast):
# TODO: handle nested for loops
if ('iter' in node._fields and
ast.dump(ast.parse(line).body[0]) in ast.dump(node)):
self.desugared_lines.insert(node.lineno-1,
self.desugared_lines.insert(node.lineno,
self.iter_start(node,
line,
line_num,
Expand Down
3 changes: 0 additions & 3 deletions pyscribe_logs.txt
@@ -1,3 +0,0 @@
From line 4: Watching variable x, currently int 5
From line 5: x changed to 3
From line 7: x changed to 7
24 changes: 24 additions & 0 deletions tests/comp/basic.py
@@ -0,0 +1,24 @@
import pyscribe

def main():
ps = pyscribe.Scriber()

ps.save_logs(True)

x = 5
ps.p(x)

for i in xrange(5):
ps.iterscribe(i)

y = "hello"
ps.p(y)
ps.watch(y)

y = "world"

synonyms = {"clerk": "secretary", "student": "apprentice", "ground": "floor"}
ps.p(synonyms)

if __name__ == "__main__":
main()
31 changes: 31 additions & 0 deletions tests/comp/basic_desugared.py
@@ -0,0 +1,31 @@
import re
import pprint
import pyscribe

def main():
pyscribe_log = open('pyscribe_logs.txt', 'w')
ps = pyscribe.Scriber()

ps.save_logs(True)

x = 5
pyscribe_log.write('From line 9: x is the ' + re.search(r'\'[a-zA-Z]*\'', str(type(x))).group()[1:-1] + ' ' + str(x)+ '\n')

BSGPBKPBDB = -1
pyscribe_log.write('----------------------------------------\n' + 'i is the ' + re.search(r'\'[a-zA-Z]*\'', str(type(i))).group()[1:-1] + ' ' + str(i) + ' at beginning of for loop at line 11' + '\n')
for i in xrange(5):
BSGPBKPBDB += 1
pyscribe_log.write('From line 12: In iteration ' + str(BSGPBKPBDB) + ', i changed to ' + str(i) + '\n')

y = "hello"
pyscribe_log.write('From line 15: y is the ' + re.search(r'\'[a-zA-Z]*\'', str(type(y))).group()[1:-1] + ' ' + str(y)+ '\n')
pyscribe_log.write('From line 16: Watching variable y, currently ' + re.search(r'\'[a-zA-Z]*\'', str(type(y))).group()[1:-1] + ' ' + str(y)+ '\n')

y = "world"

synonyms = {"clerk": "secretary", "student": "apprentice", "ground": "floor"}
pyscribe_log.write('From line 21: synonyms is the ' + re.search(r'\'[a-zA-Z]*\'', str(type(synonyms))).group()[1:-1] + ' ' + str(synonyms)+ '\n')

pyscribe_log.close()
if __name__ == "__main__":
main()
15 changes: 0 additions & 15 deletions tests/iterscribe_tests/basic_desugared.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/scribe_tests/basic.py
@@ -1,5 +1,5 @@
def main():
ps = pyscriber.Scriber()
ps = pyscribe.Scriber()
x = 5
ps.p(x)
y = "hello"
Expand Down
12 changes: 0 additions & 12 deletions tests/scribe_tests/basic_desugared.py

This file was deleted.

12 changes: 0 additions & 12 deletions tests/scribe_tests/distinguish_desugared.py

This file was deleted.

16 changes: 0 additions & 16 deletions tests/scribe_tests/more_objects_desugared.py

This file was deleted.

15 changes: 0 additions & 15 deletions tests/watch_tests/basic_desugared.py

This file was deleted.

0 comments on commit 2fac9bb

Please sign in to comment.