pyssed: Easily generate CSS from Python

I spent this evening executing an idea that I had a few weeks ago, after looking at Sass and LessCSS.  The idea is this:

Don’t write CSS.  Write Python, and then output it as CSS.

The advantages are that you can do-away with all the annoyingness of CSS, and gain all the fun of Python.  Variables? Check! Functions? Check! Lots of fun syntax and code/style reuse? Check!

I committed it to github, so you can go check it out there.  It’s called pyssed.  Take a look at the README, as well as the source code in examples.  I’d love to hear your feedback.

Posted in General | Leave a comment

Answer to Python kwargs quiz!

In a previous post, I asked if anyone knew the difference between these 3 forms of kwargs:

def printargs(**kwargs):
  print kwargs

printargs(foo='bar')
printargs(**dict(foo='bar'))
printargs(**{'foo':'bar'})

Here’s the answer:

In the 3rd case, where you’re explicitly passing in the dict as kwargs, and using the {}-style of declaring the dict, your dict keys can be things that are Python reserved words, or invalid.  Take this, for example:

printargs(**{'class': 'something', '#': 'another'})

The 3rd syntax (and it’s equivalents) are the only way to pass arguments with reserved words like this.

Unfortunately, you can’t declare your explicit kwargs arguments to be these reserved words, so once you’re doing things this way, you may as well just be passing around a dict full of args instead of using the ** operator to do it as kwargs.

Posted in General | Leave a comment

A selection of CSS helper libraries.

Lots of people are making helper libraries to make the CSS syntax a bit more user friendly.  Here’s a selection of them:

If you know of any others, please let me know.  I’d like to take a look at them.

Posted in General | Leave a comment

A quick Python kwargs quiz

Here’s some sample code:

def printargs(**kwargs):
  print kwargs

printargs(foo='bar')
printargs(**dict(foo='bar'))
printargs(**{'foo':'bar'})

Today’s question is:  In what way are the 3 bottom lines different with respect to their behavior?

Posted in General | Leave a comment

Is there any way to finagle Python into supporting attributes with a ‘-’ character in them?

Check out this bit of example code:

class test(object):
  def __init__(self):
    # We use self.__dict__ here because of the odd name of the
    # attribute.
    self.__dict__['font-weight'] = 'bold'
    self.__dict__['height'] = '32px' 

t = test()
print t.height # WORKS
print t.__getattribute__('font-weight') # WORKS  
print t.font-weight  # BROKEN: Syntax error

What I’m showing here is that the internal object structure supports attribute names with special characters in them (like hyphen), but the language syntax barfs when you try and access these attributes in a normal manner.

Is there any way to “trick” the language into allowing for easy support for attribute names with a hyphen or other special character in them?

I’m working my way towards expressing CSS selectors in native Python, and want to make it easy for CSS developers to transition to the library that I’m working on.   (Note that jQuery has this problem, and that they use camel case for things like font-weight and margin-right, etc.)

Even worse, CSS uses terms like “class” and “id” that are reserved words in Python, so there’s pretty much no way around that issue.

Posted in General | Leave a comment

Valid single-character Python function names?

I’m hunting for a good single-character non-alpha Python function name, along the lines of jQuery’s $() function.

The only non-alpha character that I think works is _(), which is okay, but not great.   Are there any others that work?  I tried a whole host of them, but none of them worked.

Posted in General | Leave a comment

fail2ban or denyhosts

Discuss.

Posted in General | Leave a comment

Turning off AIM in my GMail chat.

Hi all,

Just a quick note letting you know that I’ve turned off AIM chat support in my current GMail window.  The reason is that it seemed to be making all chats unstable, and I only very infrequently use AIM chat.  If you need to contact me via chat, please contact me via GMail chat at my e-mail address. Thanks!

Posted in General | Leave a comment

Understanding the universe of Avatar: The Last Airbender.

My wife and I have been watching episodes of the TV series each night.  It’s really well done, but there are lots of questions after each episode.  Here are a few, broken down by element:

All elements

  1. Does bending take human energy or magic energy?  For example, does creating fire burn “calories”?  If so, do fire benders need to consume extra food?  Is it possible to be “too hungry to bend?”  If it takes “magic energy” to bend, then what is the source of this energy?  Is it finite or infinite?
  2. Is there a limit to the amount of bending one can do in one session?  Do you get “tired” if you bend a lot?  Do you get “sore” from a lot of bending or training?

Fire

  1. Why do the fire nation ships bellow smoke?  Couldn’t they just have some benders down there creating flames for their boilers? (Presuming the ships are steam powered)
  2. Can fire benders extinguish fire, or just create it?
  3. If fire benders can extinguish fire (remove energy) can they cool any object?

Water

  1. Exactly what constitutes “Water”?  Clearly both sea water and fresh water count.  Additionally, frozen water counts.  What about other liquids, or other frozen forms?  Alcohol or alcoholic beverages?  Glass?  Other crystallized substances?
  2. Can water benders bend blood?  (If so, this would be a very dangerous and powerful skill.)
  3. What about water vapor? (i.e. natural humidity that’s in the air).  Can a water bender pull liquid water “from thin air” by acting like a dehumidifier?
  4. What about water contained within living cells?  Can a water bender extract all or some water from a living plant or animal?  Instant food/meat dehydration is one use case, and another is obviously a very deadly weapon.
  5. Can water benders bend wet or waterlogged substances like Jello, Mud, a wet towel or wet rope, etc.?
  6. We’ve seen water benders turn water into ice (removal of energy) and ice to water, and this implies that they can heat and cool water to any degree (i.e. produce boiling water, hot steam, etc.).  Is this true?

Earth

  1. Similar to water:  What constitutes “earth”?  Rock? Dirt? Metal ore?  Refined metals?  Naturally occurring noble gasses?
  2. Is mercury (the element) earth?  What about other substances that occur naturally in the earth, like oil, natural gas, uranium, etc.?
  3. Is lava earth?   Can earth benders turn rock into lava?

Air

  1. Similarly, what constitutes “air”?  Is it any gas?  Nitrogen gas?  Oxgen?  What about Helium, Hydrogen, Xenon, or other rare gasses?
  2. Can an air bender create a vacuum? For example, can you suffocate your enemies by encasing them in a vacuum bubble?
  3. Why does Aang need his glider?  With air control, he should be able to fly without it, and seems to do this every once in a while.  What’s the point of the glider?
  4. Can an air bender heat (and cool) air?  If so, then how is this different than the fire bending ability?
Posted in General | 4 Comments

New wordpress theme

I’m now using the latest “Twenty Ten” WordPress theme on my blog.  Mostly, because I got tired of how the other one looked, and this is the latest greatest theme from the WordPress developers.

Let me know if you like it.  Or, let me know if you never visit slacy.com because you always read this via RSS. :)

Posted in General | 1 Comment