Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #1

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests extended
szabadkai committed Aug 23, 2015
commit 23512c2698845f8cfa5fcb4c40aab3310fe48488
1 change: 1 addition & 0 deletions .idea/dictionaries/ray.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion app/Formater.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import abc
import json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the module should probably be with two t letters, eg Formatter

from Notebook import Notebook,Cell


class Formater():
22 changes: 12 additions & 10 deletions app/Notebook.py
Original file line number Diff line number Diff line change
@@ -62,12 +62,13 @@ def open_cell(line, execution_count):
current_cell = 'markdown'
elif '<codecell>' in line:
cell = {'cell_type': 'code',
'execution_count': execution_count,
'execution_count': execution_count,
'metadata': {'collapsed': False},
'outputs': []}
source = []
current_cell = 'code'
return cell, source, current_cell

current_cell = 'unknown'
execution_count = 1
skip_one_line = False
@@ -81,20 +82,20 @@ def open_cell(line, execution_count):
skip_one_line = False
continue

if line=='# <markdowncell>\n' or line=='# <codecell>\n':
if line == '# <markdowncell>\n' or line == '# <codecell>\n':
outputcells = close_cell(current_cell)
if current_cell=='code':
if current_cell == 'code':
execution_count += 1
cell, source, current_cell = open_cell(line, execution_count)
skip_one_line = True
continue

if current_cell=='markdown':
if current_cell == 'markdown':
if len(line) > 1:
source.append(line[2:])
else:
source.append(line)
elif current_cell=='code':
elif current_cell == 'code':
source.append(line)

last_cell = True
@@ -103,14 +104,15 @@ def open_cell(line, execution_count):
self.cells.append(Cell(cell))

def to_dict(self):
cells = { 'metadata': self.metadata,
'nbformat': self.nbformat,
'nbformat_minor': self.nbformat_minor,
'cells':[]}
cells = {'metadata': self.metadata,
'nbformat': self.notebook_format,
'nbformat_minor': self.nbformat_minor,
'cells': []}
for cell in self.cells:
cells['cells'].append(cell.to_dict())
return cells


class Cell(object):
def __init__(self, cell):
self.cell_type = cell['cell_type']
@@ -126,4 +128,4 @@ def generate_field_output(self):
return output + "\n"

def to_dict(self):
return {'cell_type':self.cell_type, 'source': self.source}
return {'cell_type': self.cell_type, 'source': self.source}
6 changes: 3 additions & 3 deletions app/NotebookToPy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
import fnmatch
from Notebook import Notebook
from Formater import ToPy,ToNotebook
from Formater import ToPy


class PyGenerator(object):
class NotebookToPy(object):
def convert_all_notebook_to_py(self, directory, overwrite=False):
for root, dirnames, filenames in os.walk(directory):
for filename in fnmatch.filter(filenames, '*.ipynb'):
@@ -33,7 +33,7 @@ def construct_output_path_for_py(input_path):
parser.add_argument('-f', '--file', help='Specify an Ipython notebook if you only want to convert one. '
'(This will overwrite default.)')
args = parser.parse_args()
pg = PyGenerator()
pg = NotebookToPy()

if args.file is not None:
pg.convert_notebook_to_py(args.file, overwrite=args.overwrite)
4 changes: 2 additions & 2 deletions app/PythonToNotebook.py → app/PyToNotebook.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
from Formater import ToNotebook


class NbGenerator(object):
class PyToNotebook(object):
def convert_all_py_to_notebook(self, directory, overwrite=False, dry_run=False):
for root, dirnames, filenames in os.walk(directory):
for filename in fnmatch.filter(filenames, '*.py'):
@@ -34,7 +34,7 @@ def construct_output_path_for_py(input_path):
'(This will overwrite default.)')
parser.add_argument('--dry-run', action='store_true', help='Only prints what would happen', default=False)
args = parser.parse_args()
ng = NbGenerator()
ng = PyToNotebook()

if args.file is not None:
ng.convert_py_to_notebook(args.file, overwrite=args.overwrite, dry_run=args.dry_run)
Loading