I recently wrote a simple and tiny embedded HTTP server for my C++ app (QT) and I played a little bit with Ry's http-parser and loved it. This guy is crazy.
So I told to myself: "Hey! Why not port the django template engine to C?" That'd be awesome!
I know, it won't be an easy task (not at all, I know) but I'd really love to implement this. So I came here for inspiration, ideas, opinions...
I'd really love to have some pointers on the subject, ideas, what is already done, which major problems I'll encounter (and how to solve them) - How not to reinvent the wheel... anyway, you got the idea :)
Thanks a million times!
P.S. Simple code snippets, and links to tools and libs are very welcome!
P.P.S. I'm already aware of grantlee, I took a look into its sources. Well... that's C++ and it's specific to Qt.
Hmm, I don't see why anything in the django templates code would be hard to implement in C. The template syntax looks a bit pythonic, but it's not actually python; they implemented their own parser for it. So the first place to look would be the django template implementation in python. It's really not much code, and it's reasonably easy to understand.
Of course, C will be much more verbose. What you're writing is exactly a compiler: it reads in some code (the django templates) and writes out some code in another language (the html). So all the documentation you can find about writing compilers in C (ie. tokenizers + parsers + code generators) is relevant.
First you'll probably want to generate a syntax tree. The syntax of django templates is very regular... every {% whatever %} block has a corresponding {% endwhatever %} block, so your parser could actually generate the tree without actually knowing what all the whatever keywords might be.
Then, you walk through the tree, doing the "code gen" phase for each block. For example, you'd codegen {% if %} by checking the value of the if parameter, and printing either its contents or nothing, depending whether the if clause is true or false. And so on with while loops, filters, blocks, etc.
Mind you, all this is a lot of work... have you considered just embedding a python interpreter into your C program? (Seriously! It's not that hard to do, since the python interpreter is open source.)
In case it is useful, the Synth framework features a complete stand-alone re-implementation of Django's template system in C++. It includes all built-in tags and filters as well as support for custom ones; besides the library itself, there are Python bindings, in addition to a native command-line tool, which can read in data from arbitrary sources like JSON, INIs or XML.
For reference, onion has a Django-link template system.
It compiles the template to C, and even allows a little bit of gdb debugging on them.
The code generated is quite onion oriented, but with some work it can be more generic.
If you or anyone else is interested in a C++ implementation of the Django template engine, I am working on it: http://git.fawek.net/Cjango/. It's not ready yet though.
hello why don't you use the official python binding for c++
you will be able to call python module and python script throw you c++ application
if you want to use this just include <python.h> in your application and link with python32.DLL
when you install python on your computer python come with his c++ binding by default
look at this image for more details
where you can find lib include and dll
read the doc for more details or contact me
use python in c++
Related
Greetings!
So contrary to my last question I think I was pursuing the wrong method. Basically I need to be able to compile a Python script without fear of any common leecher to obtain my source code and begin usage of it themselves. Now I know Python is a open source language not meant for being protected but there must be a way?
For example Py2Exe asks you to leave your source code in a plain text file for it then to be compiled. Which is exactly not what I'm wanting. The whole reason for this is that compilation will be happening not just on my own PC. My project is going to be compiled to PE executable form and is intended for public use and I can't have them simply opening and viewing my source.
Can anyone aid me?
You can search for a Python obfuscator or scrumbler but there will be no solution that will protect you intellectual property anyway. If you have developed something completely new go for a patent and try to control it.
In the meantime continue reading at How do I protect python code?.
There's no way to protect anything completely against reverse engineering. Some things you can do:
distribute the .pyc files. This requires slightly more effort to reverse engineer
Use an obfuscator (there aren't a lot of these out there and I haven't found any I would call first class)
Write critical sections (maybe license checks, etc.) in c and call into them
Write critical sections as web services
If you distribute .pyc or py2exe, most people will probably never look at your code. IF someone reverses engineers your code from the byte code, what's the big deal? If they're determined enough, they could just write their own application. Don't worry about it so much and ship your application if it's great enough to be protective of the source.
Well, its not very likely to happen anytime soon - Python simply is an interpreted language that is not readily fit for compilation to native code. What you could do, however, is to use Cython to create a Python extension using Cython's Python-like syntax, compile this to native code and bundle it with a "harmless" application that makes use of your extension module while at the same time making your IP harder to obtain.
You might also try ShedSkin, a native Python to C++ compiler, but you will soon find it very limited.
Still, you might consider if all that is really worth the effort - most of the time it is not.
You could contact the author of Nuitka to see if it could help with distribution of non-readable code.
Edit: Try the Google cache...
Ok, so I really love HAML. Particularly, I love the integration with RedCloth and BlueCloth, so I can use Markdown and Textile intermixed with my HAML.
I also love Python and Django.
So, I would like to use HAML with Django. Now, I already understand that there are some attempts at cloning HAML-like syntax in Python (SHPAML and others). I've tried these, and while they aren't bad, I've found that I really just want the real HAML. Partially for its syntax, but also for things like RedCloth and BlueCloth.
So, my question is, how to make HAML and Django work together?
One solution, I think, would be to create HAML templates, and then compile them into HTML using the command line tool every time they're updated.
Quesiton 1: Will I run into any problems here?
I also wonder if there's a way to get Python and Ruby to play together a little more. One idea I had was actually forking out Ruby processes. This is probably a bad idea, but anyone have any thoughts about this?
Question 2: What about using Python to call the real Ruby HAML?
Finally, if anyone knows of a Python implementation of HAML that is complete, and that supports either Textile or Markdown, as well as plaintext passthru, then let me know.
Question 3: Is there a full translation of HAML to Python including Markdown or Textile support?
Thanks!
Question 1: static HTML files should work finely (unless you plan to use HAML's ruby evaluation feature to dynamically content). I use a similar way on a php website with SASS stylesheets. Just make sure you start HAML in directory watch mode before starting to hack ;)
Question 2: while forking a ruby process to create HTML code is possible, but I don't recommend it you, because initializing a ruby interpreter, and loading required files takes a lot of cpu times, so you better stick with static files. If you need the ruby evaluation feature to include runtime created data into the document, you'd better have a look at eventmachine, and make it a server (so your Django application can connect to it, and request HTML generation, without forking a new interpreter each time).
Question 3: maybe this is the hardest. There's GHRML which is abandoned; SHPAML which only implements a small subset of HAML, DMSL which is currently very experimental, but already supports most of HAML and also calling python code, but lacks Markdown or Textile support. But apparently there's no alternative (yet) to Ruby HAML that supports all the required features.
Try: http://github.com/fitoria/django-haml
I strongly recommend that you do not fork any processes out of your django views, because the overhead is significant.
You should have a persistent ruby process to serve your templates for you, and invoke it from your django code. I leave the IPC technology to you, but the obvious choices would either be some kind of message queuing technology, or speaking HTTP over a socket to the ruby process.
While this could end up being more trouble than it is worth, it is PROBABLY possible to leverage the Java or .NET platform and still run your Django application in Jython or IronPython (with some minor adjustments I'm sure) and also be able to leverage Ruby's HAML gem via jRuby or IronRuby.
I'm sure there will be some quirks in getting this to work, but I'm sure it would be possible.
Again, this is probably a lot more trouble than it's worth (considering that you'd have to move your application to a completely new platform), but it would be a pretty fun project to work on.
i am creating ( researching possibility of ) a highly customizable python client and would like to allow users to actually edit the code in another language to customize the running of program. ( analogous to browser which itself coded in c/c++ and run another language html/js ). so my question is , is there any programming language implemented in pure python which i can see as a reference ( or use directly ? ) -- i need simple language ( simple statements and ifs can do )
edit: sorry if i did not make myself clear but what i want is "a language to customize the running of program" , even though pypi seems a great option, what i am looking for is more simple which i can study and extend myself if need arise. my google searches pointing towards xml based langagues. ( BMEL , XForms etc ).
The question isn't completely clear on scope, but I have a hunch that PyPy, embedding other full languages, and similar solutions might be overkill. It sounds like iamgopal may really be interested in something more like Interpreter Pattern or Little Language.
If the language you want to support is really small (see the Interpreter Pattern link), then hand-coding this yourself in Python won't be too hard. You can write a simple parser (Google around; here's one example), then walk the AST and evaluate user expressions.
However, if you expect this to be used for a long time or by many people, it may be worth throwing a real language at the problem. (I'd recommend Python itself if your users are already familiar with basic Python syntax).
Ren'Py is a modification to Python syntax built on top of Python itself, using the language tools in the stdlib.
For your user's sake, don't use an XML based language - XML is an awful basis for a programming language and your users will hate you for it.
Here is a suggestion. Use a strict subset of Python for your language. Use the compiler module to convert their code into an abstract syntax tree and walk the tree to to validate that the code conforms to your subset before converting the AST into python bytecode.
N.B. I just checked the docs and see that the compiler package is deprecated in 2.6 and removed in Python 3.x. Does anyone know why that is?
Numerous template languages such as Cheetah, Django templates, Genshi, Mako, Mighty might serve as an example.
Why not Python itself? With some care you can use eval to run user code.
One of the good thing about interpreted scripting languages is that you don't need another extra scripting language!
PLY (Python Lex-Yacc)
is something of your interest.
Possibly Common Lisp (or any other Lisp) will be the best choice for that task. Because Lisp make it possible to easily extend host language with powerful macroses and construct DSL (domain specific language).
If all you need is simple if statements and expressions, I'm sure it wouldn't be an awful task to parse each line. Something like
if some flag
activate some feature
deactivate some feature
elif some other flag
activate some feature
activate some feature
else
logout
Just write a class which, while parsing takes the first word, checks if it's "if, elif, else," etc, and if so, check a flag and set a flag saying you either are or are not executing until the next conditional. If it's not a conditional, call a function based on the first keyword that would modify the program state in some way.
The class could store some local execution state (are we in an if statement? If so are we executing this branch?) and have another class containing some global application state (flags that are checkable by if statements, etc).
This is probably the wrong thing to do in your situation (it's very prone to bugs, it's dangerous if you don't treat the data in the scripts correctly), but it's at least a start if you do decide to interpret your own mini-language.
Seriously though, if you try this, be very, very, srs careful. Don't give the scripts any functionality that they don't definitely need, because you are almost certainly opening security holes by doing something like this.
Don't say I didn't warn you.
I'm currently working on a simple wxPython app that's essentially document based. So far I've been manually implementing the usual open/save/undo/redo etc etc stuff.
It occurred to me that wxPython must have something to help me out and after a bit of searching revealed the docview package.
At this point though I'm just not quite sure how to hook everything up and get things started. Anyone got any good links or hints about places to start?
The docs seems to be a little thin about this and Robin Dunn's wxPython book doesn't really cover this package at all.
You might take a look at the docviewdemo.py from the wxPython Docs and Demos:
on my machine they are located:
C:\Program Files\wxPython2.8 Docs and Demos\samples\pydocview\
C:\Program Files\wxPython2.8 Docs and Demos\samples\docview\
In addition to the ones mentioned, there is quite an extensive example docview/pydocview in the samples\ide. If you want it to run you will have to make a few code corrections (I have submitted a ticket that outlines the fixes at trac.wxwidgets.org #11237). It is pretty complex but I found it handy to figure out how to do some more complex things. For example, samples\ide\activegrid\tools\ProjectEditor.py is built from scratch and has undo support etc rather than just relying on a control that does everything for you already. That way you can see how things are supposed to be done at the detailed level. The documentation is rather useless in that regard.
If you have decided against using docview/pydocview I have a spreadsheet application built on wxPython that you may find useful as an example. While it does not implement a document view framework it does have some characteristics of it and I've implemented an undo/redo system. Check it out at http://www.missioncognition.net/pysheet/ I'm currently working on a pydocview based app so I expect that to be up on my site eventually.
This is a follow-up to two questions I asked a week or so back. The upshot of those was that I was building a prototype of an AI-based application for the web, and I wondered what language(s) to use. The conclusion seemed to be that I should go for something like python and then convert any critical bits into something faster like Java or C/C++.
That sounds fine to me, but I'm wondering now whether python is really the right language to use for building a web application. Most web applications I've worked on in the past were C/C++ CGI and then php. The php I found much easier to work with as it made linking the user interface to the back-end so much easier, and also it made more logical sense to me.
I've not used python before, but what I'm basically wondering is how easy is CGI programming in python? Will I have to go back to the tedious way of doing it in C/C++ where you have to store HTML code in templates and have the CGI read them in and replace special codes with appropriate values or is it possible to have the templates be the code as with php?
I'm probably asking a deeply ignorant question here, for which I apologise, but hopefully someone will know what I'm getting at! My overall question is: is writing web applications in python a good idea, and is it as easy as it is with php?
Python is a good choice.
I would avoid the CGI model though - you'll pay a large penalty for the interpreter launch on each request. Most Python web frameworks support the WSGI standard and can be hooked up to servers in a myriad of ways, but most live in some sort of long-running process that the web server communicates with (via proxying, FastCGI, SCGI, etc).
Speaking of frameworks, the Python landscape is ripe with them. This is both good and bad. There are many fine options but it can be daunting to a newcomer.
If you are looking for something that comes prepackaged with web/DB/templating integration I'd suggest looking at Django, TurboGears or Pylons. If you want to have more control over the individual components, look at CherryPy, Colubrid or web.py.
As for whether or not it is as "easy as PHP", that is subjective. Usually it is encouraged to keep your templates and application logic separate in the Python web programming world, which can make your life easier. On the other hand, being able to write all of the code for a page in a PHP file is another definition of "easy".
Good luck.
"how easy is CGI programming in python?" Easier than C, that's for sure. Python is easier because -- simply -- it's an easier language to work with than C. First and foremost: no memory allocation-deallocation. Beyond that, the OO programming model is excellent.
Beyond the essential language simplicity, the Python WSGI standard is much easier to cope with than the CGI standard.
However, raw CGI is a huge pain when compared with the greatly simplified world of an all-Python framework (TurboGears, CherryPy, Django, whatever.)
The frameworks impose a lot of (necessary) structure. The out-of-the-box experience for a CGI programmer is that it's too much to learn. True. All new things are too much to learn. However, the value far exceeds the investment.
With Django, you're up and running within minutes. Seriously. django-admin.py startproject and you have something you can run almost immediately. You do have to design your URL's, write view functions and design page templates. All of which is work. But it's less work than CGI in C.
Django has a better architecture than PHP because the presentation templates are completely separated from the processing. This leads to some confusion (see Syntax error whenever I put python code inside a django template) when you want to use the free-and-unconstrained PHP style on the Django framework.
linking the user interface to the back-end
Python front-end (Django, for example) uses Python view functions. Those view functions can contain any Python code at all. That includes, if necessary, modules written in C and callable from Python.
That means you can compile a CLIPS module with a Python-friendly interface. It becomes something available to your Python code with the import statement.
Sometimes, however, that's ineffective because your Django pages are waiting for the CLIPS engine to finish. An alternative is to use something like a named pipe.
You have your CLIPS-based app, written entirely in C, reading from a named pipe. Your Django application, written entirely in Python, writes to that named pipe. Since you've got two independent processes, you'll max out all of your cores pretty quickly like this.
I would suggest Django, but given that you ask for something "as easy as it is with php" then you must take a look at PSP (Python Server Pages).
While Django is a complete framework for doing websites, PSP can be used in the same way than PHP, without any framework.
It is easier to write web-apps in python than it's in php. Particularly because python is not a broken language.
Pick up some web framework that supports mod_wsgi or roll out your own. WSGI apps are really easy to deploy after you get a hold from doing it.
If you want templates then genshi is about the best templating engine I've found for python and you can use it however you like.