Archive for the ‘Programming’ Category

6d6 Roll, roll, roll, roll……

I found this program while looking through some old backups labeled red_floppy. I thought it was pretty funny how shitty of a coder I was at 13. I am also kind of amazed at how much of a dork I was for writing a program to help AD&D Character Generation.


#define D 6

int roll();
int * sort_roll(int u[]);
int score(int s[]);
void seed();

int roll()
{
    int z, x, rs[4], *rr, r;
    int j = 6;
    while(--j){
    for(x = 0; x < 4; x++)
    {
          rs[x] = ((int) D * rand() / (RAND_MAX + 1.0)) + 1;
          for (z = 0; z < 100; z++) rand();
    }
    rr = sort_roll(rs);
    r = score(rr);
    }
    return r;
}

int * sort_roll(int u[])
{
    int x,y,temp;

    for(x = 0;x < 4; x++)
    {
      for(y = 0;y < 4; y++)
      {
       if(u[x] < u[y])
       {
         temp = u[x];
         u[x] = u[y];
         u[y] = temp;
       }
      }
    }
    return u;
} 

int score(int s[])
{
    return s[1] + s[2] + s[3];
}

void seed()
{
     srand((unsigned int)time((time_t *)NULL));
}

int main()
{
  int x, j;
  seed();
  do{
  for(x = 0; x < 6; x++) printf("%d\n", roll());
  printf("\n\n");
  } while((j = toupper(getch()) == 'X'));
  system("pause");
  return 0;
}

I don’t always test my code.

1337

Calculating how many lines my project is at currently (not including tester code or templates):

(kserve)@haven:~/kserve/fresh/kserver$ find lib/ controllers/ model/ -type f -name “*.py”  | grep -v template.py | grep -v auth.py | grep -v error.py | grep -v __init__ |grep -v app_globals.py| grep -v base.py |xargs cat  | wc -l
1337

l33t.. no bs.

Unfortunately, it is not done. I am going to to have to code 30,000 more lines now.. damnit!

Jrod.

Woe to the client…

that triggers this exception! Woe to them indeed!

SQLAlchemy declarative_base: setting the storage engine and other table options

Using mysql for your Engine.

DeclarativeBase = declarative_base()
class TableClass(DeclarativeBase):
    __tablename__ = 'table_name'
    __table_args__ =  {'mysql_engine':'InnoDB', 'mysql_charset':'utf8'}
    col_id = Column(Integer, primary_key=True)
    ...

Exposing sqlalchemy engine in turbogears 2.1

Getting access to the internals of the sqlalchemy stack in turbogears can be difficult if you do not know where to look. The use of declarative_base() to create model classes further obfuscates the location of the table and metadata objects.

In order to create/drop a table using an SQLAlchemy object, you need access to the table object, the table metadata, and a database engine object. When extending DeclarativeBase, the table object is exposed by the __table__ attribute of every model class. For instance:

class Distro(DeclarativeBase):
__tablename__ = ‘distro’
id = Column(Integer, primary_key=True, unique=True)
label = Column(Unicode(35), nullable=False)

In [1]: from yourapp.model import *

In [2]: import transaction

In [3]: distro_obj = Distro()

In [4]: print distro_obj.__table__.__doc__
——> print(distro_obj.__table__.__doc__)
Represent a table in a database.

e.g.::

mytable = Table(“mytable”, metadata,
Column(‘mytable_id’, Integer, primary_key=True),
Column(‘value’, String(50))
)

As you can see, you may access the methods of Table() from the DeclarativeBase __table__ attribute. In order to use the model to create/drop the table it describes, you need to bind the table metadata to an instantiated SQLAlchemy engine object. Pylons, the framework which Turbogears 2+ is based, stores the engine (defined in the paster .ini configuration) in the config global configuration dictionary, specifically:

config['pylons.app_globals'].sa_engine

Going back to the model we created, we can create the table using the Table() method create.

distro_obj.__table__.create(config['pylons.app_globals'].sa_engine)

Now that the table is created you may use the session object to interact with the table.

In [5]: distro_obj.label = 'rhel5_minimal'
In [6]: DBSession.add(distro_obj)
In [7]: transaction.commit()
In [8]: distro_label = DBSession.query(Distro.label).filter(Distro.label=='rhel5_minimal').one()[0]
In [9]: print distro_label
-------> print(distro_label)
rhel5_minimal

For more information, take a look at the documentation for TG2.1 and SQLAlchemy declarative syntax.

A good day

image

@ the office

sqlalchemy, MySQLdb, debian

(in virtualevn)

$ sudo apt-get install libmysqlclient-dev python-dev
$ easy_install SQLAlchemy mysql-python

Not sure why the package is labeled mysql-python. Also, you need the development headers because the MySQLdb module is written in c.

Rackspace Cloud API

In an article here, Rackspace is announcing the availability of their beta web services api that can be used to jack into their cloud. Much like EC2, this should lead the way for projects like scaler, that dynamically grow a configuration as thresholds are reached.

Check out http://www.rackspacecloud.com/ for more info. I am going to sign up, I will let you know how it goes.

Stop the plesk adminstrator from recieving dr.web license failures

… And other things that might annoy him/her

There was a request that went around for months from a plesk administrator griping about the various messages that appear in his mailbox, such as the dr.web license notification. By default, messages to the root and postmaster accounts are sent to the plesk administrator. This is accomplished via qmail aliases.

# cd /var/qmail/alias/
# ls -al
total 28
drwxr-sr-x 2 alias qmail 4096 Mar 9 01:59 .
drwxr-xr-x 11 root qmail 4096 Sep 29 21:14 ..
-rw-r–r– 1 root qmail 26 Dec 10 2007 .qmail-mailer-daemon
-rw-r–r– 1 root qmail 16 Mar 9 01:57 .qmail-postmaster
-rw-r–r– 1 root qmail 16 Mar 9 01:57 .qmail-root

Typically these look something link this:
# cat .qmail-postmaster
&plesk_admin_address@foo.bar.com

To keep the customer happy, I made a slight amendment to the alias:

# cat .qmail-postmaster
|cat >/dev/null
# cat .qmail-root
|cat >/dev/null

Removing the aliases will cause all kinds of noise in the logs and actually break the server’s already fragile rfc compliance (rfc2821:4.5.1) . This allows the messages to be ‘handled’ silently. Since most plesk ‘administrators’ don’t care to know how many failed ssh attempts there were in a month, or how much disk space they are using, it is usually a working solution when you can’t get dr.web to stfu.

Return top