Category Archives: Linux

experiences using Linux, the world’s best OS

How to Create and Drop Tables in SQL Alchemy


Quick and easy step by step approach to creating and dropping tables in SQL Alchemy 1.1 and 1.2.  All done with a few lines of code.

The following assumptions are made:

  1. You have iPython installed (pip install ipython #if you don’t have it)
  2. You have sqlAlchemy installed (pip install sqlalchemy)
  3. You have psycopg2 installs (pip install psycopg2)
  4. You have a Postgres connection you can connect to
  5. Your Postgres users has the ability to create and drop tables

Perfect, let’s get started

First, I want to begin looking at my existing database structure. I opened up pgAdminIII.  In Postgres, I have 2 databases, postgres and production.  I am interested in connecting and working in the production database.  In the production database, I have 2 schemas, public and python.  I will be working in the python schema in this example.  If you have any questions up to this point, please comment.

database environment for sqlAlchemy (Postgres)

Open your terminal (OS X, Linux) or your command line (Windows)

Type ‘ipython’ to open iPython then, execute the following lines of code.  Anything after the hashtag (#) is optional, as that is a comment regarding the line of code

from sqlalchemy import * # imports all needed modules from sqlalchemy

engine = create_engine('postgresql://python:python@127.0.0.1/production') # connection properties stored

metadata = MetaData() # stores the 'production' database's metadata

users = Table('users', metadata,
 Column('user_id', Integer),
 Column('first_name', String(150)),
 Column('last_name', String(150)),
 Column('email', String(255)),
 schema='python'
 ) # defines the 'users' table structure in the 'python' schema of our connection to the 'production' db

users.create(engine) # creates the users table

Let’s take a look at the table we created in pgAdmin

As  you an see, the table users has been created with the exact same structure you defined in your code.

What if you want to drop the table you created via sqlAlchemy? Easy peasy, just execute the following statement

users.drop(engine) # drops the Now, let say you want to drop the table you created.  You simply execute the following script:users table

Go back to your pgAdmin, refresh your schema, and confirm that it is gone!

Below is the entire code output in the iPython terminal.

sqlAlchemy Example

sqlAlchemy creating and dropping tables

This exercise was executed in a Ubuntu Linux virtualenv. =)

Thank you for reading!

Leave a comment

Filed under Linux, Python, SQL

Getting Started with virtualenv


Why should you care about using virtualenv?  Basically it provides you with a development environment for your various python projects.  Not all python projects will call for the same libraries and being able to solely keep track of the needed libraries within a specific environment can make life easier by:

  • Reducing the # of dependencies that need to be maintained in a single environment
  • Reduce the risk of breaking program dependencies of versioned libraries
  • Replicate the needed environment in other machines without re-installing every single library that you may have done once upon a time

Here are some simple steps to get started with virtualenv on a brand new machine (should apply to both linux and os x)

On a new installation of Linux (or OS X instance)
1.) sudo apt-get install pip #install pip so you can install virtualenv
2.) sudo pip install virtualenv #install virtualenv so you can have controlled python environments

 
3.) virtualenv new_project  #create a project directory and execute this command where new_project = your project name for e.g.

 
4.) source new_project/bin/activate #to start your virtual environment
5.) pip install psycopg2 #install any python library in your new python virtual environment

 

 
6.) pip install jupyter #installs jupyter notebooks! (which can be kicked off on your browser)

 

 

 

7.) deactivate #when you are done working in your virtual environment, deactivate

 

From here, you can return to step 4 when you want to work on your project again. Or go to step 3 if you want to start a new project.

To capture your python virtual environment components, do the following:
pip freeze > requirements.txt

This way, if you need to recreate your environment, you can use the file by typing:
pip install -r requirements.txt

For more info, go here: http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/

Before I go, if you are wondering how to start jupyter in your web browser within your new virtualenv, via the command line execute the following:

source my_project/bin/activate #assuming you exited from your virtualenv

jupyter-notebook #your web browser should open after this

 

 

Leave a comment

Filed under Linux, Python

Installing the sample database in IBM DB2 C in Linux (Ubuntu)


After you install DB2 on your Linux system (along with IBMs Data Studio) you may have problems connecting to the sample database.  It may be that it did not install.  If you have any questions about the above, post a comment.

To install the sample database, first be sure you log in to your instance.  By default during your first install, your instance name should be db2inst1

 

 

Login to your db2inst1 account

Once you are in you’re logged into your instance session, navigate to your sqllib/samples folder.   Execute the db2sampl file.  This will begin the Sample Database installation process.

installing sample DB2 database in Linux

 

Once the installation is complete, connect to the database by typing: db2 connect to sample.

You can list the tables in the database by typing: db2 list tables

connect to and list tables in database

 

Now that you’ve set up your playground, have fun.

Leave a comment

Filed under Linux, SQL

Review of Epson iPrint for Android


Recently I got a Galaxy S3.  I thought it would try to print from it.  Luckily an app was available for my Epson wireless printer.  No wires necessary.  All I needed was to ensure that both my printer and phone are on the network.

Below are some screenshots.

Once your printer is selected, you get a nifty snapshot of the ink levels.

image

Next you go to the initial screen.

image

This app has a built in browser and that is how u print off the web.

image

With your menu button you will ‘ve able to control the color settings, orientation and other things.

image

image

image

image

The app is intuitive and easy to use.  So far I have not had any problems.

Leave a comment

Filed under Computer - Technical, Linux

Add a wireless printer in Virtual Box


This is my scenario.  I am running Ubuntu as my main operating system.  Within Ubuntu I am also running Windows through Virtual Box.

  1. Install the necessary printer drivers to have your printer work with Linux.  (In my case, Epson Stylus NX430 has Linux drivers on their website that made wireless printing possible)
  2. Then install CUPS.  You can install CUPS through Ubuntu’s Software Center
  3. Next, to confirm that CUPS install correctly, go to http://localhost:631.  The following page should appear:
  4. Click on the Administration link and add your printer as a Network Printer.  Allow the “Share printers… ” checkbox to be checked.  Click on the Add a Printer Button.
  5. After clicking the button, CUPS will search for available Network Printers.  The available printer(s) will show up like below
  6. Next go to Windows.  Click on your start Menu and then click on Devices and Printers
  7. In the new Window, click on Add a Printer
  8. When this following screen appears, select the option “Add a network, wireless or Bluetooth printer”
  9. Your printer should show up.  Select it and complete the wizard.  Your new printer will be set as your default printer.

That’s all. Now you can begin printing in Windows.

Leave a comment

Filed under Computer - Technical, Linux

Configure Error: You need boost/bind.hpp from the boost library to run Aleph One


If you are receiving the errors below during your installation of Aleph One (I’m trying to get this on my machine so I can try out Marathon),

checking boost/bind.hpp usability... no
checking boost/bind.hpp presence... no
checking for boost/bind.hpp... no
configure: error: You need boost/bind.hpp from the boost library to run Aleph One

then you can install the dependencies you need by typing:

sudo apt-get install libboost-thread-dev

Happy gaming.

Leave a comment

Filed under Computer - Technical, Linux