What is the pythonic way to implement a css parser/replacer - python

I want to implement a script that reads a CSS file and makes meaningful changes to it (adding/removing/replacing lines/words etc.). The basic logic is implement an RTL (right-to-left) transformation.
I could think of quite a few approaches to it:
file reader - read a line, analyze it and make the needed changes to it.
two phase scan - create in memory model, scan and change it, save model to text.
regular expressions - It might be quite difficult because some of them might be very complex.
basically what I'm wondering is which of those, or other methods, would be the python way to do it? are there any relevant libraries you think I should be familiar with for this kind of operation?
Edit:
it's should be noted that this is a "learn python through this usable project" kind of project so I'm not familiar with most libraries you would mention here.

If you want something "quick and dirty" there are many interesting ways to do this. (As you said: line-by-line, regular expressions, …)
But if you want to do it "right" (correct on all kinds of inputs) you’ll need a real parser based on the official CSS tokenization and grammar. In Python there is cssutils and tinycss. (Disclaimer: I’m tinycss’s author.) If you want to learn, I like to think that tinycss’s source code is straightforward and easy to learn :)

Related

Looking for a Python CSS pre-processor with Stylus-like syntax?

Stylus is a language that compiles into normal CSS. The language is a huge time- and space saver for many reasons. For instance, I can put selectors inside each other:
div.foo
color: red
div.bar
color: blue
font-weight: bold
div.baz
color: green
background-color: pink
I can also do stuff like make vendor-specific properties work without the prefixes, which saves a lot of space.
My question is: Are there any alternatives to Stylus? I can't really think of a description of what Stylus is, so I'm finding it hard to find any alternatives using Google.
Note: By alternatives, I mean languages that compile into CSS and offer time-saving goodies.
The reason I'm looking for alternatives is that I use Stylus for all my projects, and I want to see if there's a better solution out there. The reason I'm not 100% happy with Stylus is that the TextMate bundle for it is terrible, and Stylus requires Node.js to compile which is also a step I'd like to avoid. I use mainly Python.
Stylus' older siblings are LESS and Sass. I prefer Stylus to both, both because of features and speed, but they are pretty much interchangeable, AFAIK.
LESS is also written in javascript, and so will not release you from your node.js dependency. Sass is written in Ruby.
There is a Stylus implementation in Python. It's called Stilus and is nearing completion. You find it here: https://github.com/jw/stilus
CSS-On-Diet is written in Python. You can install it easily by
pip install CSSOnDiet
It saves a lot of time and space, but syntax is different
I can't really think of a description of what Stylus is
The term you are looking for is CSS preprocessor.
Check this ones:
Less: Unlike Stylus is a css super-set. In other words, every valid CSS document is also a valid Less document. This way is easier to migrate from existing CSS.
Sass: Very similar to Less. It has a variant (SCSS) which notation is also a SCC super-set. Some people prefer Sass over Less because his control-structures syntax (selection, iteration) is more natural for most programmers, since is very similar to the one used in most imperative programming languages (C++, Java, etc) while Less uses a recursive approach, more similar to functional programming languages.
Absurd JS: The styles are described by writting pure JavaScript code, given that his notation for objects literals is very similar to the CSS notation. This interesting approach allows you to manipulate and preprocess the styles in any form you can imagine, since you are no longer limited by a small bunch of control structures like in the two above, but you can use the whole toolset of features provided by a programming language. I strongly recommend this options if you are a programmer, or if as a designer you are interested in learning how to code, or if you want to describe and reuse your style definitions in a non-trivial or crazy way you can't achieve with just a preprocessor.
I didn't know Stylus before reading this question. It seems to be pretty nice. I like his Python-like approach of use line breaks and tabulations to define the structure of the code. I will give it a try. If you already use this preprocessor and you don't find any important drawback, I would stick with it.

What's a good resource to build my first python project?

I'm tired of books and tutorials who walk me through how to print things before I can do anything fun. I want to build simple apps or programs. Any suggestions for where to start so I can make and learn at the same time?
If you're just looking for something to do that will challenge you to start actually using Python rather than reading about it, try the Ruby Quiz. You don't have to use Ruby to create solutions.
Each quiz is a problem that can be scripted (using any language, really). Ideally it'll force you to apply the concepts you've been reading about to "real life" problems.
If you are tired of tutorials, then just start building something. Anything. If you get stuck, glance back over the tutorials, or consult the documentation. I'm a big fan of learning by doing.
A short list of options from off the top of my head:
If you're into web development, Django is a popular python web framework that is very well documented. The Blog app is a popular starting point.
Python itself is pretty well documented. If you're a complete beginner to python AND programming in general, you may want to try something less complex. Pick a random task and try to do it using python:
Read and print the ID3 tags from all your mp3 files using mutagen.
List or download your email using the python imaplib or poplib modules.
Write a zip/unzip utility using zipfile.
Don't be in too much of a hurry, and be realistic. Unless you've got a pile of programming under your belt, you won't be able to jump into a complex project after reading a few tutorials. Patience and practice will get you to the place where you can tackle really interesting projects. Impatience will merely lead to frustration.
Might i suggest pygame http://www.pygame.org/news.html ? If you want to do things with visuals, your not going to get anywhere with the default python modules unless, of course you know how to implement SDL. As stated above in the comments, the tutorials are there for a reason; although they are simple, they are meant to teach you the basics and perhaps, have you think about ideas of implementing such given tools to larger projects. Give pygame I try. You can create a window with lines and shapes in a little as ~10 lines. From there you can expand your knowledge to Object-Oriented programming(which a must for UI) and be on your way to larger projects such as AI, graphics, etc.
P.S. Check this book out http://apress.com/book/view/1590598725. Although you might not want to get into game development, it will teach you some rather useful techniques which may help your research in application development.

Universal syntax file format?

Hey as a project to improve my programing skills I've begun programing a nice code editor in python to teach myself project management, version control, and gui programming. I was wanting to utilize syntax files made for other programs so I could have a large collection already. I was wondering if there was any kind of universal syntax file format much in the same sense as .odt files. I heard of one once in a forum, it had a website, but I can't remember it now. If not I may just try to use gedit syntax files or geany.
thanks
If you're planning to do syntax highlighting, check out Pygments, especially the bit about lexers.
Since you mentioned Geany, you might want to look at the Scintilla docs. (Geany is built upon Scintilla).
You might find this post interesting.
Also, be sure to get familiar with the venerable lex and yacc.
Not sure what .odt has to do with any of this.
I could see some sort of BNF being able to describe (almost) any syntax: Just run the text and the BNF through a parser, and apply a color scheme to the terminals. You could even get a bit more fancy, since you'd have the syntax tree.
In reality, I think most syntax files take an easier approach, such as regular expressions. This would put then somewhere above regular expressions but not really quite context-free in terms of power.
As for file formats, if you re-use something that exists, then you can just loot and pillage (subject to license agreements) their syntax file data.

programming language implemented in pure python

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.

How to parse *.py file with python?

I'd like to parse Python source in order to try making a basic source code converter from Python to Go.
What module should I use?
Should I proceed or not?
If I should proceed, how?
Have a look at the language services packages, particularly the ast.
My guess is that if you don't already have a solid grasp of both parsing as well as code generation techniques, this is going to be a difficult project to undertake.
good luck!
As for the 'should I go ahead or better not' question: why do you want to do this in the first place?
If it's a purely learning exercise, then you don't don't need to ask us whether it's worthwhile. You want to learn, so go right ahead.
If it's meant to be a practical tool, then my suggestion is to not do it. An industrial-strength tool to perform such conversions might be useful but I would guess that you're not going to go that far. With that in mind it's probably more fruitful to rewrite the Python code in Go manually.
That assumes there is any real benefit to compiling to Go; current testing suggests that you get better performance and similar code structure from using Stackless Python.
The Boo Solution
Are you trying to make a python-like language, that can compiles into Go? This seems most sensible, as you will want to do Go-specific things (to take advantage of Go features).
Look at pyparsing. It includes an example of a complete python parser, but you probably don't want to do that.
You want to incrementally build your converter / translator, so you want to incrementally build the parser, otherwise you might choke on the AST. OK, you could parse everything and just ignore the stuff you don't understand, but that's not great behavior from a compiler.
You could start with parsing basic arithmetic.
The Pyrex Solution
This is similar to the Boo solution, but much harder. Get the Boo solution working first. Then learn to generate wrapper code, so your Go and python parts can work together.
The PyPy Solution
A complete Python-Go compiler? Good luck. You'll need it.
There's a good list of parsers rounded-up by Ned Batchelder which might help.

Categories

Resources