[sphinx] generating doc from neighboring folders
Hi there, we are evaluating the possibility to use Sphinx and rst to document our projects. What we have is a project structure that would look like this: ./sandbox/project/ ├── components │ ├── module1 │ │ ├── doc │ │ │ └── module1.rst │ │ └── src │ └── module2 │ ├── doc │ │ └── module2.rst │ └── src └── doc └── spec ├── conf.py └── project_spec.rst Since components are reusable amongs projects, we would like to get them documented properly with their own conf.py, but then how would it be possible to 'include' them in the project documentation (under project/doc)? It looks like sphinx wants all the sources under the same directory root. What we thought of is writing a python script to traverse the project folder and look for rst files and _copy_ them in the doc directory, but I find this solution rather ugly. Any ideas on how we should organize the rst files to accomplish that? As a side note, I would like to include automatically links to my components documentation in the index.rst file, any suggestion on how to do that? Any suggestion/hint/comment is appreciated. Al -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? -- https://mail.python.org/mailman/listinfo/python-list
parsing tree from excel sheet
Hi everyone, I've a document structure which is extremely simple and represented on a spreadsheet in the following way (a made up example): subsystem | chapter | section | subsection | subsubsec | A | | || | | func0 | || | | |interface|| | | |latency || | | |priority || | | func1 | || | | |interface|| | | |latency || | | |priority || | | |depend || | | | | variables | | | | || static| | | || global| | | | functions | | | | || internal | | | || external | And I'd like to get a tree like this: A +---> func0 | +---> interface | +---> latency | \---> priority \---> func1 +---> interface +---> latency +---> priority \---> depend +---> variables | +---> static | \---> local \---> functions +---> internal \---> external I know about the xlrd module to get data from excel and I'm also aware about the ETE toolkit (which is more specific for bioinformatics, but I guess can suitable fill the need). Does anyone recommend any other path other than scripting through these two modules? Is there any more suitable module/example/project out there that would achieve the same result? The reason for parsing is because the need behind is to create documents edited in excel but typeset in LaTeX, therefore my script will spill out \chapter, \section and so forth based on the tree structure. Every node will have some text and some images with a very light markup like mediawiki that I can easily convert into latex. Hope I've not been too confusing. Thanks for any pointer/suggestion/comment. Al p.s.: I'm not extremely proficient in python, actually I'm just starting with it! -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? -- https://mail.python.org/mailman/listinfo/python-list
Re: parsing tree from excel sheet
Hi Peter, Peter Otten <[email protected]> wrote: [] > You can save the excel sheet as csv so that you an use the csv module which > may be easier to use than xlrd. The rest should be doable by hand. Here's > what I hacked together: > > $ cat parse_column_tree.py > import csv > > def column_index(row): >for result, cell in enumerate(row, 0): >if cell: >return result >raise ValueError > > > class Node: >def __init__(self, name, level): >self.name = name >self.level = level >self.children = [] > >def append(self, child): >self.children.append(child) > >def __str__(self): >return "\%s{%s}" % (self.level, self.name) > >def show(self): >yield [self.name] >for i, child in enumerate(self.children): >lastchild = i == len(self.children)-1 >first = True >for c in child.show(): >if first: >yield ["\---> " if lastchild else "+---> "] + c >first = False >else: >yield [" " if lastchild else "| "] + c >def show2(self): >yield str(self) >for child in self.children: >yield from child.show2() > > def show(root): >for row in root.show(): >print("".join(row)) > > def show2(root): >for line in root.show2(): >print(line) > > def read_tree(rows, levelnames): >root = Node("#ROOT", "#ROOT") >old_level = 0 >stack = [root] >for i, row in enumerate(rows, 1): > >new_level = column_index(row) >node = Node(row[new_level], levelnames[new_level]) > >if new_level == old_level: >stack[-1].append(node) >elif new_level > old_level: >if new_level - old_level != 1: >raise ValueError > >stack.append(stack[-1].children[-1]) >stack[-1].append(node) >old_level = new_level >else: >while new_level < old_level: >stack.pop(-1) >old_level -= 1 >stack[-1].append(node) >return root > > def main(): >import argparse >parser = argparse.ArgumentParser() >parser.add_argument("infile") >parser.add_argument("--latex", action="store_true") > >args = parser.parse_args() > >with open(args.infile) as f: >rows = csv.reader(f) >levelnames = next(rows) # skip header >tree = read_tree(rows, levelnames) > >show_tree = show2 if args.latex else show >for node in tree.children: >show_tree(node) >print("") > > if __name__ == "__main__": >main() > $ cat data.csv > subsystem,chapter,section,subsection,subsubsec, > A, > ,func0 > ,,interface,,, > ,,latency,,, > ,,priority,,, > ,func1 > ,,interface,,, > ,,latency,,, > ,,priority,,, > ,,depend,,, > ,,,variables,, > static, > global, > ,,,functions,, > internal, > external, > $ python3 parse_column_tree.py data.csv > A > +---> func0 > | +---> interface > | +---> latency > | \---> priority > \---> func1 > +---> interface > +---> latency > +---> priority > \---> depend >+---> variables >| +---> static >| \---> global >\---> functions > +---> internal > \---> external > > $ python3 parse_column_tree.py data.csv --latex > \subsystem{A} > \chapter{func0} > \section{interface} > \section{latency} > \section{priority} > \chapter{func1} > \section{interface} > \section{latency} > \section{priority} > \section{depend} > \subsection{variables} > \subsubsec{static} > \subsubsec{global} > \subsection{functions} > \subsubsec{internal} > \subsubsec{external} WOW! I didn't really want someone else to write what I needed but thanks a lot! That's a lot of food to digest in a single byte, so I'll first play a bit with it (hopefully understanding what is doing) and then come back with comments. I really appreciated your time and effort. Al -- https://mail.python.org/mailman/listinfo/python-list
Re: parsing tree from excel sheet
Hi Peter, Peter Otten <[email protected]> wrote: [] >def show2(self): >yield str(self) >for child in self.children: >yield from child.show2() here is what I get: > SyntaxError: invalid syntax > debian@debian:example$ python3 export_latex.py doctree.csv > File "export_latex.py", line 36 > yield from child.show2() > ^ > SyntaxError: invalid syntax and I've tried with both python and python3 (see below versions). debian@debian:example$ python Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> debian@debian:example$ python3 Python 3.1.3 (r313:86834, Nov 28 2010, 11:28:10) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> Is it an issue related to my installation? Shall I upgrade and/or downgrade? Thanks for any hint, Al -- https://mail.python.org/mailman/listinfo/python-list
Re: parsing tree from excel sheet
Hi Tim, Tim Chase wrote: [] >> I know about the xlrd module to get data from excel > > If I have to get my code to read Excel files, xlrd is usually my > first and only stop. > It provides quite a good interface to manipulating excel files and I find it pretty easy even for my entry level! >> Does anyone recommend any other path other than scripting through >> these two modules? > > Well, if you export from Excel as CSV, you can use the "csv" module > in the standard library. This is actually my preferred route because > it prevents people (coughclientscough) from messing up the CSV file > with formatting, joined cells, and other weirdnesses that can choke > my utilities. In my case there's no such risk of manipulating the excel file. I'm in charge of it! :-) Sure it might at a later stage be misused and messed up inadvertedly, but we're just trying to validate an idea, i.e. writing specs without using any word processor. I'm trying to bypass the need to go through a mark up language (to a certain point), in order to facilitate the transition from an unstructured approach to document writing to a more structured one. I would have proposed SGML or XML and style sheets but unfortunately is hard to move from M$Word to XML (OMG I need to write code?!?!!). So to facilitate the transition to a structured approach I've come up with the idea to go through an automatic generation of documents using excel as a UI. In a later stage with could move onto a full-fledged database and have simpler web access, but using the same backend for generating documents (i.e. some parser and latex). >> Is there any more suitable module/example/project out there that >> would achieve the same result? > > I don't believe there's anything that will natively do the work for > you. Additionally, you'd have to clarify what should happen if two > rows in the same section had different sub-trees but the same > content/name. Based on your use-case (LaTex export using these as > headers) I suspect you'd want a warning so you can repair the input > and re-run. But it would be possible to default to either keeping or > squashing the duplicates. Sure, there are corner cases that might mess up the whole structure, which at the moment is not too fool proof, but I'm trying to test the idea and see what I can come up with. Once the flow is in place I could think over some more reliable approach as an interface to a database. >> p.s.: I'm not extremely proficient in python, actually I'm just >> starting with it! > > Well, you've come to the right place. Most of us are pretty fond of > Python here. :-) I've never understood people discarding newsgroups in favor of more 'recent' technologies like social networks. Long live the USENET! -- https://mail.python.org/mailman/listinfo/python-list
Re: parsing tree from excel sheet
Hi MRAB, MRAB wrote: [] >>> SyntaxError: invalid syntax >>> debian@debian:example$ python3 export_latex.py doctree.csv >>> File "export_latex.py", line 36 >>> yield from child.show2() >>> ^ >>> SyntaxError: invalid syntax >> >> and I've tried with both python and python3 (see below versions). >> >> debian@debian:example$ python >> Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) >> [GCC 4.4.5] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. > >> debian@debian:example$ python3 >> Python 3.1.3 (r313:86834, Nov 28 2010, 11:28:10) >> [GCC 4.4.5] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. > >> >> Is it an issue related to my installation? Shall I upgrade and/or >> downgrade? >> > "yield from" was introduced in Python 3.3. > Ok, that either means I need to upgrade to 3.3 or need to modify the snippet to a suitable syntax that would work with other versions. Considering that upgrading is something that I'm not keen to do on my production system I believe I've only have one available choice. It seems I could use the generator and iterate with .next() in python 2.6, at least from what I found here: http://stackoverflow.com/questions/1756096/understanding-generators-in-python Al -- https://mail.python.org/mailman/listinfo/python-list
Re: parsing tree from excel sheet
Hi Peter, I'll try to comment the code below to verify if I understood it correctly or missing some major parts. Comments are just below code with the intent to let you read the code first and my understanding afterwards. Peter Otten <[email protected]> wrote: [] > $ cat parse_column_tree.py > import csv > > def column_index(row): >for result, cell in enumerate(row, 0): >if cell: >return result >raise ValueError Here you get the depth of your first node in this row. > class Node: >def __init__(self, name, level): >self.name = name >self.level = level >self.children = [] > >def append(self, child): >self.children.append(child) > >def __str__(self): >return "\%s{%s}" % (self.level, self.name) Up to here everything is fine, essentially defining the basic methods for the node object. A node is represented univocally with its name and the level. Here I could say that two nodes with the same name cannot be on the same level but this is cosmetic. The important part would be that 'Name' can be also 'Attributes', with a dictionary instead. This would allow to store more information on each node. >def show(self): >yield [self.name] Here I'm lost in translation! Why using yield in the first place? What this snippet is used for? >for i, child in enumerate(self.children): >lastchild = i == len(self.children)-1 >first = True >for c in child.show(): >if first: >yield ["\---> " if lastchild else "+---> "] + c >first = False >else: >yield [" " if lastchild else "| "] + c Here I understand more, essentially 'yield' returns a string that would be used further down in the show(root) function. Yet I doubt that I grasp the true meaning of the code. It seems those 'show' functions have lots of iterations that I'm not quite able to trace. Here you loop over children, as well as in the main()... >def show2(self): >yield str(self) >for child in self.children: >yield from child.show2() ok, this as well requires some explanation. Kinda lost again. From what I can naively deduce is that it is a generator that returns the str defined in the node as __str__ and it shows it for the whole tree. > def show(root): >for row in root.show(): >print("".join(row)) > > def show2(root): >for line in root.show2(): >print(line) Here we implement the functions to print a node, but I'm not sure I understand why do I have to iterate if the main() iterates again over the nodes. > > def read_tree(rows, levelnames): >root = Node("#ROOT", "#ROOT") >old_level = 0 >stack = [root] >for i, row in enumerate(rows, 1): I'm not quite sure I understand what is the stack for. As of now is a list whose only element is root. >new_level = column_index(row) >node = Node(row[new_level], levelnames[new_level]) here you are getting the node based on the current row, with its level. >if new_level == old_level: >stack[-1].append(node) I'm not sure I understand here. Why the end of the list and not the beginning? >elif new_level > old_level: >if new_level - old_level != 1: >raise ValueError here you avoid having a node which is distant more than one level from its parent. >stack.append(stack[-1].children[-1]) here I get a crash: IndexError: list index out of range! >stack[-1].append(node) >old_level = new_level >else: >while new_level < old_level: >stack.pop(-1) >old_level -= 1 >stack[-1].append(node) Why do I need to pop something from the stack??? Here you are saying that if current row has a depth (new_level) that is smaller than the previous one (old_level) I decrement by one the old_level (even if I may have a bigger jump) and pop something from the stack...??? >return root once filled, the tree is returned. I thought the tree would have been the stack, but instead is root...nice surprise. > > def main(): [strip arg parsing] >with open(args.infile) as f: >rows = csv.reader(f) >levelnames = next(rows) # skip header >tree = read_tree(rows, levelnames) filling the tree with the data in the csv. > >show_tree = show2 if args.latex else show >for node in tree.children: >show_tree(node) >print("") It's nice to define show_tree as a function of the argument. The for loop now is more than clear, traversing each node of the tree. As I said earlier in the thread there's a lot of food for a newbie, but better going through these sort of exercises than dumb tutorial which don't teach you much. Al -- https://mail.python.org/mailman/listinfo/python-list
Re: parsing tree from excel sheet
Hi Peter, Peter Otten <[email protected]> wrote: [] > Let's start with the simplest: > >> Peter Otten <[email protected]> wrote: > >>>def show2(self): >>>yield str(self) >>>for child in self.children: >>>yield from child.show2() [] > > Given a tree > > A --> A1 > A2 --> A21 > A22 > A3 > > assume a slightly modified show2(): > > def append_nodes(node, nodes): >nodes.append(node) >for child in node.children: >append_nodes(child, nodes) I'm assuming you are referring to the method in the Node class. > > When you invoke this with the root node in the above sample tree and > an empty list > > nodes = [] append_nodes(A, nodes) > > the first thing it will do is append the root node to the nodes list > > [A] > > Then it iterates over A's children: > > append_nodes(A1, nodes) will append A1 and return immediately because > A1 itself has not children. > > [A, A1] > > append_nodes(A2, nodes) will append A2 and then iterate over A2's > children. As A21 and A22 don't have any children append_nodes(A21, > nodes) and append_nodes(A22, nodes) will just append the respective > node with no further nested ("recursive") invocation, and thus the > list is now > > [A, A1, A21, A22] > > Finally the append_nodes(A3, nodes) will append A3 and then return > because it has no children, and we end up with > > nodes = [A, A1, A21, A22, A3] So the recursive function will append children as long as there are any, traversing the whole tree structure (yep, I saw the missing A2 in the list as you mentioned already). > Now why the generator? For such a small problem it doesn't matter, for > large datasets it is convenient that you can process the first item > immmediately, when the following ones may not yet be available. I've read something about generators and they are a strong concept (especially for a C-minded guy like me!). [] > Ok, how to get from the recursive list building to yielding nodes as > they are encountered? The basic process is always the same: > > def f(items) > items.append(3) > items.append(6) > for i in range(10): > items.append(i) > > items = [] > f(items) > for item in items: > print(item) > > becomes > > def g(): >yield 3 >yield 6 >for i in range(10): >yield i > > for item in g(): >print(items) ---^ should be item and not items. > > In Python 3.3 there was added some syntactic sugar so that you can > write > > def g(): >yield 3 >yield 6 >yield from range(10) > > > Thus > > def append_nodes(node, nodes): >nodes.append(node) >for child in node.children: >append_nodes(child, nodes) > > > becomes > > def generate_nodes(node): >yield node >for child in node.children: >yield from generate_nodes(child) I'm with you now! I guess it would have been nearly impossible to see the real picture behind. > This looks a lot like show2() except that it's not a method and thus > the node not called self and that the node itself is yielded rather > than str(node). The latter makes the function a bit more flexible and > is what I should have done in the first place. Indeed returning the node might be more useful than just yielding its string. > > The show() method is basically the the same, but there are varying > prefixes before the node name. Here's a simpler variant that just adds > some indentation. We start with generate_nodes() without the syntactic > sugar. This is because we need a name for the nodes yielded from the > nested generator call so that we can modify them: > > def indented_nodes(node): >yield node >for child in node.children: >for desc in from indented_nodes(child): >yield desc > > Now let's modify the yielded nodes: > > def indented_nodes(node): >yield [node] why this line has changed from 'yield node'? >for child in node.children: >for desc in indented_nodes(child): >yield ["***"] + desc Ok, the need for manipulation does not allow to use the syntax sugar of above. > > How does it fare on the example tree? > > A --> A1 > A2 --> A21 > A22 > A3 > > The lists will have an "***" entry for every nesting level, so we get > > [A] > ["***", A1] > ["***", A2] > ["***", "***", A21] > ["***", "***", A22] > ["***", A3] > > With "".join() we can print it nicely: > > for item in indented_nodes(tree): >print("".join(item)) > > But wait, "".join() only accepts strings so let's change > >yield [node] > > to >yield [node.name] # str(node) would also work Again my question, why not simply yield node.name? > A > ***A1 > ***A2 > **A21 > **A22 > ***A3 > >>> def show2(root): >>>for line in root.show2(): >>>print(line) > >> Here we implement the functions to print a node, but I'm not sure I >> understand why do I have to iterate if the main() iterates again over the >> nodes. >
pypandoc and restructured text
Hi everyone, I'm trying to convert restructured text to latex with pandoc and it seems to me there's something not correctly working. I have the following text: .. figure:: picture.png :scale: 50 % :alt: map to buried treasure This is the caption of the figure (a simple paragraph). The legend consists of all elements after the caption. In this case, the legend consists of this paragraph and the following table: +---+---+ | Symbol| Meaning | +===+===+ | .. image:: tent.png | Campground| +---+---+ | .. image:: waves.png | Lake | +---+---+ | .. image:: peak.png | Mountain | +---+---+ which is essentially copied from here: http://docutils.sourceforge.net/docs/ref/rst/directives.html#figure But unfortunately the pypandoc silently ignores everything. The same happens with pandoc directly. My installation is the following: pandoc 1.5.1.1 python 2.6.6 debian squeeze Any idea why? Should I upgrade somehow beyond what the debian repository delivers? Al p.s.: I understand that this is potentially not related to python and more related to the package, but there's a chance somebody here already had the same problem. -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? -- https://mail.python.org/mailman/listinfo/python-list
Re: pypandoc and restructured text
Hi Wolfgang, Wolfgang Maier wrote: [] > I have pandoc 1.12.2.1 and it recognizes the figure directive just fine > (tested with html output so I cannot say anything about LaTeX). This reminds me that I need to move sooner or later from squeeze to wheezy... Do you know of anyway to install wheezy packages on squeeze? On backports there's nothing more than what comes with the squeeze sources. > I've found an old forum post from 2011: > > https://groups.google.com/forum/#!topic/pandoc-discuss/OmGYDycaMjs > > confirming that the figure directive was not supported at that time. > So, yes, I think upgrading pandoc could help. Thanks a lot for the hint. Maybe I should seriously think about upgrading the whole distro. It's just that Gnome3 really sucks to my taste and I'm not in the mood to look for another Desktop Environment...(maybe I should go back to CDE). Al -- https://mail.python.org/mailman/listinfo/python-list
Re: pypandoc and restructured text
Hi Chris, Chris Angelico wrote: [] >> Thanks a lot for the hint. Maybe I should seriously think about >> upgrading the whole distro. It's just that Gnome3 really sucks to my >> taste and I'm not in the mood to look for another Desktop >> Environment...(maybe I should go back to CDE). >> > > This is a bit off-topic for python-list, being more about Debian than > the language, but trying to install just one Wheezy package on Squeeze > is probably a bad idea; you'll probably find that it's built against a > different version of libc and such, so you'd need to upgrade pretty > much your whole system. I apologize for littering the group, thought somehow many users here might have had a similar issues with pypandoc. [] > 2) Upgrade the entire OS to either Wheezy or Jessie. Personally, I'm > quite happy with Jessie, even though she's not officially stable yet > (and have you _seen_ her? Definitely mentally unstable); but whether > you go Wheezy or Jessie, grab Xfce rather than Gnome 3. The latest > Jessie installers are giving an option on installation as to which > desktop environment(s) to install, but if you do it as an upgrade, you > may want to explicitly uninstall Gnome and install Xfce, either before > or after the upgrade. I finally upgraded! And I'm currently trying out xfce! Thanks again for the suggestions. Al p.s.: now pandoc works as expected. -- https://mail.python.org/mailman/listinfo/python-list
rst and pypandoc
Hi everyone,
I'm writing a document in restructured text and I'd like to convert it
to latex for printing. To accomplish this I've used semi-successfully
pandoc and the wrapper pypandoc.
My biggest issue is with figures and references to them. We've our macro
to allocate figures so I'm forced to bypass the rst directive /..
figure/, moreover I haven't happened to find how you can reference to a
figure in the rst docs.
For all the above reasons I'm writing snippets of pure latex in my rst
doc, but I'm having issues with the escape characters:
i = '\ref{fig:abc}'
print pypandoc.convert(i, 'latex', format='rst')
ef\{fig:abc\}
because of the \r that is interpreted by python as special character.
If I try to escape with '\' I don't seem to find a way out...
Any idea/pointer/suggestion?
Al
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
--
https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi Dave,
Dave Angel wrote:
[]
> You should be a lot more explicit with all three parts of that
> statement. Try:
>
>
> I'm trying to get a string of
\ref{fig:A.B}
but unfortunately I need to go through a conversion between rst and
latex. This is because a simple text like this:
this is a simple list of items:
- item A.
- item B.
gets translated into latex by pypandoc as this:
\begin{itemize}
\item item A.
\item item B.
\end{itemize}
And it's much simpler to write my document with rst markup rather than latex.
So my question is what should my restructured text look like in order to
get it through pypandoc and get the following:
\ref{fig:abc}
Apparently rst only allows the following type of references:
- external hyperlink targets
- internal hyperlink targets
- indirect hyperlink targets
- implicit hyperlink targets
and I want to get a later that has a reference to a figure, but none of
those seem to be able to do so. Therefore I thought about passing an
inline text in my rst in order to get it through the conversion as is,
but apparently I'm stuck with the various escaping mechanisms.
My python script reads the text and passes it on to pypandoc:
i = "%\n" % text
o = pypandoc.convert(i, 'latex', format='rst')
So if text is:
this is some text with a reference to Figure \ref{fig:abc}
I would like o to be like:
this is some text with a reference to Figaure \ref{fig:abc}
but I get:
ef\{fig:abc\}
Al
--
https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi Steven,
Steven D'Aprano wrote:
[]
> Since \r is an escape character, that will give you carriage return followed
> by "ef{fig:abc".
>
> The solution to that is to either escape the backslash:
>
> i = '\\ref{fig:abc}'
>
>
> or use a raw string:
>
> i = r'\\ref{fig:abc}'
ok, maybe I wasn't clear from the very beginning, but searching for a
solution is a journey that takes time and patience.
The worngly named variable i (as noted below), contains the *i*nput of
my text which is supposed to be restructured text. The output is what
pypandoc spits out after conversion:
i = "\\begin{tag}{%s}{%s}\n %s\n \\end{tag}" % (some, restructured, text)
o = pypandoc.convert(i, 'latex', format='rst')
Now if i contains some inline text, i.e. text I do not want to convert
in any other format, I need my text to be formatted accordingly in order
to inject some escape symbols in i.
Rst escapes with "\", but unfortunately python also uses "\" for escaping!
>
> Oh, by the way, "i" is normally a terrible variable name for a string. Not
> only doesn't it explain what the variable is for, but there is a very
> strong convention in programming circles (not just Python, but hundreds of
> languages) that "i" is a generic variable name for an integer. Not a
> string.
I'm not in the position to argue about good practices, I simply found
more appropriate to have i for input and o for output, considering they
are used like this:
i = "some string"
o = pypandoc.convert(i, ...)
f.write(o)
with very little risk to cause misunderstanding.
> Can you show what you are doing? Escaping the backslash with another
> backslash does work:
>
> py> for c in '\\ref':
> ... print(c, ord(c))
> ...
> \ 92
> r 114
> e 101
> f 102
>
> so either you are doing something wrong, or the error lies elsewhere.
As said above, the string is converted by pandoc first and then printed.
At this point the escaping becomes tricky (at least to me).
In [17]: inp = '\\ref{fig:abc}'
In [18]: print pypandoc.convert(inp, 'latex', format='rst')
ref\{fig:abc\}
Al
--
https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi Dave,
Dave Angel wrote:
[]
>> Rst escapes with "\", but unfortunately python also uses "\" for escaping!
>
> Only when the string is in a literal. If you've read it from a file, or
> built it by combining other strings, or... then the backslash is just
> another character to Python.
Holy s***t! that is enlightning. I'm not going to ask why is that so,
but essentially this changes everything. Indeed I'm passing some strings
as literal (as my example), some others are simply read from a file
(well the file is read into a list of dictionaries and then I convert
one of those keys into latex).
The it would mean that the following text (in a file) should be
swallowed by python as if the backslash was just another character:
this is \some text
unfortunately when I pass that to pypandoc, as if it was restructured
text, I get the following:
In [36]: f = open('test.txt', 'r')
In [37]: s = f.read()
In [38]: print s
this is \some restructured text.
In [39]: print pypandoc.convert(s, 'latex', format='rst')
this is some restructured text.
what happened to my backslash???
If I try to escape my backslash I get something worse:
In [40]: f = open('test.txt', 'r')
In [41]: s = f.read()
In [42]: print s
this is \\some restructured text.
In [43]: print pypandoc.convert(s, 'latex', format='rst')
this is \textbackslash{}some restructured text.
since a literal backslash gets converted to a literal latex backslash.
[]
>> As said above, the string is converted by pandoc first and then printed.
>> At this point the escaping becomes tricky (at least to me).
>>
>> In [17]: inp = '\\ref{fig:abc}'
>>
>> In [18]: print pypandoc.convert(inp, 'latex', format='rst')
>> ref\{fig:abc\}
>>
>
> What did you expect/desire the pyandoc output to be? Now that you don't
> have the embedded 0x0a, is there something else that's wrong?
I need to get \ref{fig:abc} in my latex file in order to get a
reference. It seems to me I'm not able to pass inline text to pandoc and
every backslash is treated...somehow.
> If it's in the internals of pyandoc, I'll probably be of no help. But
> your first question was about escaping; I'm not sure what it's about now.
It's still about escaping in both python and restructured text since I
want my substring (is part of the text) to pass unchanged through
pypandoc.
Al
--
https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi MRAB,
MRAB wrote:
[]
> Have you tried escaping the escape character by doubling the backslash?
>
> inp = 'ref{fig:abc}'
In [54]: inp = 'ref{fig:abc}'
In [55]: print pypandoc.convert(inp, 'latex', format='rst')
\textbackslash{}ref\{fig:abc\}
the backslash is considered as literal text for latex and is escaped
with the appropriate command.
> or:
>
> inp = r'\\ref{fig:abc}'
>
In [56]: inp = r'\\ref{fig:abc}'
In [57]: print pypandoc.convert(inp, 'latex', format='rst')
\textbackslash{}ref\{fig:abc\}
same as above. The result I aim to would be:
In [BINGO]: print pypandoc.convert(inp, 'latex', format='rst')
\ref{fig:abc}
Al
--
https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi Dave,
Dave Angel wrote:
[]
>>> or use a raw string:
>>>
>>> i = r'\\ref{fig:abc}'
>
> Actually that'd be:
>i = r'\ref{fig:abc}'
Could you explain why I then see the following difference:
In [56]: inp = r'\\ref{fig:abc}'
In [57]: print pypandoc.convert(inp, 'latex', format='rst')
\textbackslash{}ref\{fig:abc\}
In [58]: inp = r'\ref{fig:abc}'
In [59]: print pypandoc.convert(inp, 'latex', format='rst')
ref\{fig:abc\}
The two results are clearly *not* the same, even though the two inp
/claim/ to be the same...
Al
--
https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi Steven,
Steven D'Aprano wrote:
[]
>> In [43]: print pypandoc.convert(s, 'latex', format='rst')
>> this is \textbackslash{}some restructured text.
>>
>> since a literal backslash gets converted to a literal latex backslash.
>
> Why is this a problem? Isn't the ultimate aim to pass it through latex,
> which will then covert the \textbackslash{} back into a backslash? If not,
> I have misunderstood something.
\textbackslash{} is a latex command to typeset a backslash into the
text. This is not what I need. I need to have a string of the form
"\some" (actually we are talking about \ref or \hyperref commands).
> If not, you could do something like this:
>
> s = 'this is %(b)ssome restructured text.'
> t = pypandoc.convert(s, 'latex', format='rst')
> assert t == 'this is %(b)ssome restructured text.'
> print t % {'b': '\\'}
This is somehow what I'm doing now, but is very dirty and difficult to
expand to other corner cases.
Al
--
https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi Mark, Mark Lawrence wrote: [] > The two inps are *not* the same. My bad. I did not notice the difference, thanks for pointing that out. Al -- https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi Steven, Steven D'Aprano wrote: [] >> The two results are clearly *not* the same, even though the two inp >> /claim/ to be the same... > > The two inp are not the same. Correct. My statement was wrong. [] > I'm sure that you know how to do such simple things to investigate whether > two inputs are in fact the same or not, and the fact that you failed to do > so is just a sign of your frustration and stress. You nailed it! Indeed there were all the symptoms of a stressed situation from the very beginning: 1. the OP was unclear and full of misleading information 2. part of the posts were misunderstood, hence causing more confusion than anything else. 3. my code has become a mess of workarounds, being far from pythonic. Now that the delivery date is passed behind me I'll have some time to clean up the mess and get everything straight. Being pragmatic and finding workaround is not bad but the mess should be cleaned up afterwards! Al -- https://mail.python.org/mailman/listinfo/python-list
Re: rst and pypandoc
Hi Gregory, Gregory Ewing wrote: [] > From a cursory reading of the pypandoc docs, it looks > like enabling the raw_tex extension in pypandoc will > give you what you want. > > Search for raw_tex on this page: > > http://johnmacfarlane.net/pandoc/README.html As far as I understood the docs, it seems this extension should be passed to pandoc through +EXTERNSION, but I don't seem to get it working: In [14]: print pypandoc.convert(s, 'latex', format="md+raw_tex") --- RuntimeError Traceback (most recent call last) in () > 1 print pypandoc.convert(s, 'latex', format="md+raw_tex") /usr/local/lib/python2.7/dist-packages/pypandoc.pyc in convert(source, to, format, extra_args, encoding) 25 ''' 26 return _convert(_read_file, _process_file, source, to, ---> 27 format, extra_args, encoding=encoding) 28 29 /usr/local/lib/python2.7/dist-packages/pypandoc.pyc in _convert(reader, processor, source, to, format, extra_args, encoding) 50 raise RuntimeError( 51 'Invalid input format! Expected one of these: ' + ---> 52 ', '.join(from_formats)) 53 54 if to not in to_formats: RuntimeError: Invalid input format! Expected one of these: native, json, markdown, markdown+lhs, rst, rst+lhs, docbook, textile, html, latex, latex+lhs -- https://mail.python.org/mailman/listinfo/python-list
SyntaxError on progress module
Hi everyone, I've installed the 'progress' module (ver 1.2) and I have the following error when used: File "/home/debian/repos/2418_IASI-NG/Documents/Tools/tex_tool/venv/local/lib/python3.2/site-packages/progress/bar.py", line 48 empty_fill = u'∙' ^ SyntaxError: invalid syntax I believe I have some problems with unicode handling but it's just a rough guess. I'm running in a virtual environment with python3.2 with the following configuration: (venv)debian@debian:tex_tool$ pip list pip (7.0.1) progress (1.2) pypandoc (0.9.7) setuptools (15.0) Any suggestions/comments/pointer is appreciated. Al -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? -- https://mail.python.org/mailman/listinfo/python-list
Re: SyntaxError on progress module
Hi Chris, Chris Angelico wrote: [] >> Python 3.0 removed the 'u' for unicode in front of strings but due to >> popular demand to ease porting it was reinstated in 3.3. Strip it away and >> you should be fine to go. > > Or upgrade to 3.3 or better; is there anything holding you on 3.2? > Building CPython from source is pretty easy on Debian, and of course > upgrading to Jessie will correspondingly upgrade you to a more recent > Python (3.4, to be precise). I moved recently from squeeze to wheezy in my production environment, I've nothing really holding me back on my current configuration... I'll give it a try in the coming days. Thanks for the prompt answer, Al -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? -- https://mail.python.org/mailman/listinfo/python-list
Re: SyntaxError on progress module
Hi Mark, Mark Lawrence wrote: [] >>File >> "/home/debian/repos/2418_IASI-NG/Documents/Tools/tex_tool/venv/local/lib/python3.2/site-packages/progress/bar.py", >> line 48 >> empty_fill = u'∙' >>^ >> SyntaxError: invalid syntax >> [] > > Python 3.0 removed the 'u' for unicode in front of strings but due to > popular demand to ease porting it was reinstated in 3.3. Strip it away > and you should be fine to go. I'm not particularly comfortable in fiddling with the library source, even if the change seems really minor, therefore I guess that I'll upgrade to a more recent version of Python/Debian. But here I have another question, as a python novice is there really any reason for me to use any particular version of Python? Should I start directly with the newest? What about 2.7? Al -- https://mail.python.org/mailman/listinfo/python-list
kbhit/getch python equivalent
Hi everyone, I'm looking for a kbhit/getch equivalent in python in order to be able to stop my inner loop in a controlled way (communication with external hardware is involved and breaking it abruptly may cause unwanted errors on the protocol). I'm programming on *nix systems, no need to be portable on Windows. I've seen the msvcrt module, but it looks like is for Windows only. Any ideas/suggestions? Al -- A: Because it fouls the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? -- http://mail.python.org/mailman/listinfo/python-list
