SQL: Shorten string to length

digg del.icio.us TRACK TOP
By Eric Downing | Filed in SQL | No comments yet.

I had a project the other day that required me to shorten a description down to a certain length and add an elipsis. The problem I encountered was making sure that the truncation did not happen in the middle of a word.

So using a Postgres database, I used the following query to return the shortened description without ending in a partial word.

select 
    case when length(long_description)>= 100
    then reverse(substring(reverse(left(long_description,100)),position(' ' in reverse(left(long_description,100))))) || '...'
    else long_description
    end
    from data_feed

The steps involved were to

  1.  Check if the field is over 100 characters,  if >100, then continue to step 2
  2.  Use left function to get 100 characters of the string
  3.  Reverse the string returned from left
  4.  Get a substring of the reversed truncated string from the first space (‘ ‘) to the end of the reversed truncated string
  5. Reverse the final string (So it is back to the correct direction of text)
  6. Append the ellipsis to the string ( or add a link or leave off if no extra chars are required)

 

Tags: ,

Invalid JPEG when uploading to photo service

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Uncategorized | No comments yet.

I create cards in Adobe Illustrator or Photoshop on occasion and have had issues with getting my prints made at on-line or in-store photo services (such as Walgreens, Walmart or Target). I have received invalid JPEG errors.

You will need to make sure that your color mode is set to RGB for your files and not CMYK.

Ironically, even though most printers do better with CMYK, the RGB format is required for your JPEGS at these services.

VI: Indent lines

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Editors, Utilities, vi | No comments yet.

The other day I was editing some code and as usual I was in Vi. It was python code and I had refactored some code and needed to indent a section of code. I did not relish the idea of trying to make sure that all the indents were done on several dozen lines of code so I looked into how to indent the code in VI.

Here is the sequence that let me indent blocks easily.

I used <shift>-V to select the entire line.

Then I selected multiple lines by using the arrow keys or j or k depending on which direction you need to select.

Then you use the ‘>’ key to indent to the right and the ‘<‘ key to decrease the indent to the left.

Install Python modules to a different directory

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Programming, Python | No comments yet.

There are times that I have wanted to install some Python module on a system where I did not have access to do so.
When you do not have permission to write into the standard library directories you
may need to run the “python setup.py install” command with the additional option of

 --home=<Some other directory>

make sure that this base directory has a lib/python directory within it and that you have
exported the PYTHONPATH variable to contain this directory.

Then simply run the command “python setup.py install –home=<Some other directory>” and your libs will
be installed.

Then you will need to make sure that your PYTHONPATH environment variable is always available
to whatever runs your scripts.

As an alternative to the PYTHONPATH, you can use the following within your scripts to target a non-standard python module directory:

import sys
sys.path.append("/path/to/the/module/directory")
## import any modules you have in your own directory

Using the method in the script allows you to make the scripts available to other users that may not have the PYTHONPATH setup.

Tags:

Counting lines, words and characters with wc

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Linux, Shell, Utilities | No comments yet.

There is a simple Linux utility for counting characters, lines and words in files called wc. This allows you to give a file as an argument or a stream and get the counts of the aforementioned items.

wc <filename>

or

cat <filename> | wc

The options that wc accepts

-c,–bytes print the byte counts
-m,–chars print the character counts
-l,–lines print the line counts
-L,–max-line-length print the length of the longest line
-w,–words print the word counts

The ouput with no arguments will be three numbers (words,lines,characters. Otherwise the the number will reflect the argument you have passed.

And if you pass multiple filenames the output displays counts for all files and totals.

 >wc -l HelloWorld.java sample.txt
    8 HelloWorld.java
   25 sample.txt
   33 total

Tags: ,

HTML: Basic HTML5 page

digg del.icio.us TRACK TOP
By Eric Downing | Filed in html | No comments yet.

This is where I start coding my web pages. This is a simple framework for an HTML5 web page. The DOCTYPE has been simplified and is supported by all major browsers even if they do not fully support the current HTML5 tags.

<!DOCTYPE html>
<html>
<head>
<title>My Sample HTML5 web page</title>
</head>
<body>
This is my sample web page.
</body>
</html>

Tags: ,

Postgres: Get table and field names from a given schema

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Programming, SQL | No comments yet.

I work with a large Postgres install that has many hundreds of schemas. there are times that I need to find a list of tables and fields from a specific schema for a web application that I am developing.

This will return the list of table names within the giving schema:

select relname 
from pg_stat_user_tables 
WHERE schemaname='schema1';

Then to get the given field names from the table that I have chosen:

select column_name 
from information_schema.columns 
where table_name='mytable';

Tags:

How to include Javascript on a web page

digg del.icio.us TRACK TOP
By Eric Downing | Filed in html, Programming, Web Design | No comments yet.

There are three ways to include Javascript files on a website. You can use scripts placed in the head element, scripts placed within the body element or you can include an external file.

The <script> tag is used to contain the Javascript code when you are placing it within the web page.

Example of code within the <head> tag:

 <!DOCTYPE html>
<html>
<head>
<title>Javascript in Head tag</script>
<script language="javascript" type="text/javascript" >
     document.write("Hello, World!");
</script>
</head>
<body>
</body>
</html>

When you are expecting to output directly into the page it is helpful to place the script in the
<body> tag.

<!DOCTYPE html>
<html>
<head>
<title>Javascript in Body tag</script>
<script>

</script>
</head>
<body>
<script language="javascript" type="text/javascript" >
  document.write("Hello, World!");
</script>
</body>
</html>

And when you are including an external script like jQuery or a custom script, you will use an
tag in the head with a src identifier to specify the file. Where you might reference the file in an onload or some other scripting within the page.

<!DOCTYPE html>
<html>
<head>
<title>Javascript in External file</script>
<script language="javascript" type="text/javascript" src="somefile.js" > </script>
</head>
<body onload="callsomefile();" >
</body>
</html>

There is also the possibility that someone will have turned off Javascript and this is where the <noscript> tag can be used to either display the site designed with no scripting involved or inform the user that they cannot use the site unless Javascript is enabled.

Tags: , ,

Hello world!

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Uncategorized | No comments yet.

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Awk: Using awk to Truncate fields

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Linux, Utilities | No comments yet.

Working with data feeds is sometimes frustrating when you have over 50 columns and are trying to find the field that is too long for your insert into your database system of choice. I have found processing the files with different Linux utilities useful and one that I regularly use it “awk”. I was trying to get the substring of all fields in a product feed the other day and used the following trick.

awk -F 't' '{for(i = 1;i<NF;i++) printf("%s,",substr($i,0,50));print;};

The for loop iterates over all the fields separated by a tab in each line and the prints out the field with a length of 50 characters or less. The extra print is for the newline at the end of each line.

Tags: ,