Web2Py - Python Syntax - python

I recently began working with Web2Py, after a long period of learning Python. As a beginner, I'm used to having modules and knowing their attributes to getting my work done. For example, I can import the time module and look up the time.sleep() function in the existing documentation to see how I can employ it the way I see fit.
Web2Py, however, employs a lot of Python code that, while easy on the eyes and relatively straightforward if you take the time to study it, isn't exactly covered in detail in the manual. For instance, I occasionally see a response.something in the code, and I can't find the details of the response function anywhere; or I'll see a URL pop up in curly brackets which references a file in, say, my static folders that has syntax like {{=URL(r = "request", c = 'static', f = 'image.png')}} with no explanations of the various attributes of the URL call anywhere in the manual. The manual appears to cover only the aspects of configuring the MVCs and doing specific tasks.
What I'm trying to say is: is there a python.org type documentation for all the exceptional Python functions that appear in Web2Py code?
I'll be much obliged. Thanks!

These are covered in chapter 4 of the web2py book
response
URL

Related

Structure of a python project that is not a package

If you search over the internet about python project structures, you will find some articles about python package structure. Based on it, What I want to know is if there is any kind of instructions for creating structure for python projects that isn't packages, that is, projects that the code is the end code it self?
For example, I created a package that handles some requests of some specific endpoints. This package will serve the main code that will handle the data fetched by this package. The main code is not a package, that is, it don't have classes and __init__ files, because in this software layer, there will be no necessity of code reuse. Instead, the main code relate straight to the end it self.
Is there any instructions for it?
It would be good to see the structure itself instead of reading the description of it - it can help visualize the problem and answer properly to your case 😉
projects that isn't packages, that is, projects that the code is the end code it self
In general, I would say you should always structure your code! And by telling that, I mean exactly the work with the modules/packages. It is needed mostly to sperate the responsibilities and to introduce things that can be reused. It also gives the possibility to find things easier/faster instead of going through the unstructured tones of the code.
Of course, as I said, it is a general thought and as far as you are experienced you can experiment with the structure to find the best one for the project which you are working on. But without any structure, you won't survive in a bigger project (or the life will be harder than you want).

Python Twisted - How to use cache() in a non-rpy script

I am still somewhat new to Python, and have begun learning to use the Twisted framework so that I may set up an asynchronous web server. The details about storing stateful information in the Session object is pretty straightforward, but there is something that is lacking in the documentation that is throwing me off. The first line in the script on this tutorial reads:
cache()
...rest of the script goes here
This is something that only works in what is called an rpy script - more about that here. The problem is, I don't really want to use an rpy script, and it allegedly is not a requirement. The page I referenced describes rpy scripts as being mainly for experimenting with new ideas AND NOT MUCH ELSE.
My issue is that when I try to run a non-rpy version of my script, I get this error:
NameError: name 'cache' is not defined
Some additional research has told me that cache() is a part of the globals for every rpy script, so there is no need to import. However, the documentation doesn't describe how to use cache() in a non-rpy file. So, there is my question - how can I use cache() in a non-rpy file? I am pretty sure it is just a matter of knowing which module to import, which I do not. Any help will be appreciated.
A distinctive feature of the handling of rpy scripts by Twisted Web is that the source code is re-evaluated on each request.
cache is an API specifically for rpy scripts to tell the runtime not to re-evaluate the source again. If cache is called, the results of evaluating the source are saved and used to satisfy the next request for that resource.
Since this feature is unique to handling of rpy scripts, there is no need or value in using cache when defining resources for Twisted Web in a different way.
Apparently, if you aren't using an rpy file, you simply don't need to use cache(). I simply removed that line from the code and it seems to be working fine. Any additional input on this is still appreciated, because the documentation is lacking.

How to find good documentation for Python modules

I can't seem to find a good explanation of how to use Python modules. Take for example the urllib module. It has commands such as
req = urllib.request.Request().
How would I find out what specific commands, like this one, are in certain Python modules?
For all the examples I've seen of people using specific Python modules, they just know what to type, and how to use them.
Any suggestions will be much appreciated.
My flow chart looks something like this:
Reading the published documentation (or use help(moduleName) which gives you the same information without an internet connection in a harder to read format). This can be overly verbose if you're only looking for one tidbit of information, in which case I move on to...
Finding tutorials or similar stack overflow posts using specific keywords in your favorite search engine. This is generally the approach you will use 99% of the time.
Just recursively poking around with dir() and __doc__ if you think the answer for what you're looking for is going to be relatively obvious (usually if the module has relatively simple functions such as math that are obvious by the name)
Looking at the source of the module if you really want to see how things works.

Porting Django's templates engine to C

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++

How do I use the wx.lib.docview package?

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.

Categories

Resources