Tuesday, 30 September 2014
Naming convention (programming)
From Wikipedia, the free encyclopedia
In computer programming, a naming convention is a set of rules for choosing the character sequence to be used foridentifiers which denote variables, types, functions, and other entities in source code and documentation.
Reasons for using a naming convention (as opposed to allowing programmers to choose any character sequence) include the following:
- to reduce the effort needed to read and understand source code;[1]
- to enhance source code appearance (for example, by disallowing overly long names or unclear abbreviations).
The choice of naming conventions can be an enormously controversial issue, with partisans of each holding theirs to be the best and others to be inferior. Colloquially, this is said to be a matter of dogma.[2] Many companies have also established their own set of conventions to best meet their interests.
Monday, 29 September 2014
Friday, 26 September 2014
Database and SQL Testing
Finish making notes on your blogs from my notes on databases and SQL clcik on these in my cloud
Label your notes on your blog
then take these tests
Data and Databases
Databases, DBMS and SQL
Programming
Wednesday, 24 September 2014
What is a Pickle in Python
In this post i am going to tell you about pickle. It is used for serializing and de-serializing a Python object structure. Any object in python can be pickled so that it can be saved on disk. What pickle does is that it “serialises” the object first before writing it to file. Pickling is a way to convert a python object (list, dict, etc.) into a character stream. The idea is that this character stream contains all the information necessary to reconstruct the object in another python script.
So lets continue:
1. First of all you have to import it through this command:
import pickle
pickle has two main methods. The first one is dump, which dumps an object to a file object and the second one is load, which loads an object from a file object.
2. Prepare something to pickle:
Now you just have to get some data which you can pickle. For the sake of simplicity i will be pickling a python dictionary. So just read the below code and you will be able to figure it out yourself.
Now you just have to get some data which you can pickle. For the sake of simplicity i will be pickling a python dictionary. So just read the below code and you will be able to figure it out yourself.
import pickle
a = ['test value','test value 2','test value 3']
a
['test value','test value 2','test value 3']
file_Name = "testfile"
# open the file for writing
fileObject = open(file_Name,'wb')
# this writes the object a to the
# file named 'testfile'
pickle.dump(a,fileObject)
# here we close the fileObject
fileObject.close()
# we open the file for reading
fileObject = open(file_Name,'r')
# load the object from the file into var b
b = pickle.load(fileObject)
b
['test value','test value 2','test value 3']
a==b
True
The above code is self explanatory. It shows you how to import the pickled object and assign it to a variable. So now we need to know where we should use pickling. It is very useful when you want to dump some object while coding in the python shell. So after dumping whenever you restart the python shell you can import the pickled object and deserialize it. But there are other use cases as well which i found on stackoverflow. Let me list them below.
1) saving a program's state data to disk so that it can carry on where it left off when restarted (persistence)
2) sending python data over a TCP connection in a multi-core or distributed system (marshalling)
3) storing python objects in a database
4) converting an arbitrary python object to a string so that it can be used as a dictionary key (e.g. for caching & memoization).
Pickle in Python
# Pickle It
# Demonstrates pickling and shelving data
import pickle, shelve
print("Pickling lists.")
variety = ["sweet", "hot", "dill"]
shape = ["whole", "spear", "chip"]
brand = ["Claussen", "Heinz", "Vlassic"]
f = open("pickles1.dat", "wb")
pickle.dump(variety, f)
pickle.dump(shape, f)
pickle.dump(brand, f)
f.close()
print("\nUnpickling lists.")
f = open("pickles1.dat", "rb")
variety = pickle.load(f)
shape = pickle.load(f)
brand = pickle.load(f)
print(variety)
print(shape)
print(brand)
f.close()
print("\nShelving lists.")
s = shelve.open("pickles2.dat")
s["variety"] = ["sweet", "hot", "dill"]
s["shape"] = ["whole", "spear", "chip"]
s["brand"] = ["Claussen", "Heinz", "Vlassic"]
s.sync() # make sure data is written
print("\nRetrieving lists from a shelved file:")
print("brand -", s["brand"])
print("shape -", s["shape"])
print("variety -", s["variety"])
s.close()
input("\n\nPress the enter key to exit.")
# Demonstrates pickling and shelving data
import pickle, shelve
print("Pickling lists.")
variety = ["sweet", "hot", "dill"]
shape = ["whole", "spear", "chip"]
brand = ["Claussen", "Heinz", "Vlassic"]
f = open("pickles1.dat", "wb")
pickle.dump(variety, f)
pickle.dump(shape, f)
pickle.dump(brand, f)
f.close()
print("\nUnpickling lists.")
f = open("pickles1.dat", "rb")
variety = pickle.load(f)
shape = pickle.load(f)
brand = pickle.load(f)
print(variety)
print(shape)
print(brand)
f.close()
print("\nShelving lists.")
s = shelve.open("pickles2.dat")
s["variety"] = ["sweet", "hot", "dill"]
s["shape"] = ["whole", "spear", "chip"]
s["brand"] = ["Claussen", "Heinz", "Vlassic"]
s.sync() # make sure data is written
print("\nRetrieving lists from a shelved file:")
print("brand -", s["brand"])
print("shape -", s["shape"])
print("variety -", s["variety"])
s.close()
input("\n\nPress the enter key to exit.")
Reading a text file in Python
# Read It
# Demonstrates reading from a text file
print("Opening and closing the file.")
text_file = open("read_it.txt", "r")
text_file.close()
print("\nReading characters from the file.")
text_file = open("read_it.txt", "r")
print(text_file.read(1))
print(text_file.read(5))
text_file.close()
print("\nReading the entire file at once.")
text_file = open("read_it.txt", "r")
whole_thing = text_file.read()
print(whole_thing)
text_file.close()
print("\nReading characters from a line.")
text_file = open("read_it.txt", "r")
print(text_file.readline(1))
print(text_file.readline(5))
text_file.close()
print("\nReading one line at a time.")
text_file = open("read_it.txt", "r")
print(text_file.readline())
print(text_file.readline())
print(text_file.readline())
text_file.close()
print("\nReading the entire file into a list.")
text_file = open("read_it.txt", "r")
lines = text_file.readlines()
print(lines)
print(len(lines))
for line in lines:
print(line)
text_file.close()
print("\nLooping through the file, line by line.")
text_file = open("read_it.txt", "r")
for line in text_file:
print(line)
text_file.close()
input("\n\nPress the enter key to exit.")
# Demonstrates reading from a text file
print("Opening and closing the file.")
text_file = open("read_it.txt", "r")
text_file.close()
print("\nReading characters from the file.")
text_file = open("read_it.txt", "r")
print(text_file.read(1))
print(text_file.read(5))
text_file.close()
print("\nReading the entire file at once.")
text_file = open("read_it.txt", "r")
whole_thing = text_file.read()
print(whole_thing)
text_file.close()
print("\nReading characters from a line.")
text_file = open("read_it.txt", "r")
print(text_file.readline(1))
print(text_file.readline(5))
text_file.close()
print("\nReading one line at a time.")
text_file = open("read_it.txt", "r")
print(text_file.readline())
print(text_file.readline())
print(text_file.readline())
text_file.close()
print("\nReading the entire file into a list.")
text_file = open("read_it.txt", "r")
lines = text_file.readlines()
print(lines)
print(len(lines))
for line in lines:
print(line)
text_file.close()
print("\nLooping through the file, line by line.")
text_file = open("read_it.txt", "r")
for line in text_file:
print(line)
text_file.close()
input("\n\nPress the enter key to exit.")
Writing to a text file in Python
# Write It
# Demonstrates writing to a text file
print("Creating a text file with the write() method.")
text_file = open("write_it.txt", "w")
text_file.write("Line 1\n")
text_file.write("This is line 2\n")
text_file.write("That makes this line 3\n")
text_file.close()
print("\nReading the newly created file.")
text_file = open("write_it.txt", "r")
print(text_file.read())
text_file.close()
print("\nCreating a text file with the writelines() method.")
text_file = open("write_it.txt", "w")
lines = ["Line 1\n",
"This is line 2\n",
"That makes this line 3\n"]
text_file.writelines(lines)
text_file.close()
print("\nReading the newly created file.")
text_file = open("write_it.txt", "r")
print(text_file.read())
text_file.close()
input("\n\nPress the enter key to exit.")
# Demonstrates writing to a text file
print("Creating a text file with the write() method.")
text_file = open("write_it.txt", "w")
text_file.write("Line 1\n")
text_file.write("This is line 2\n")
text_file.write("That makes this line 3\n")
text_file.close()
print("\nReading the newly created file.")
text_file = open("write_it.txt", "r")
print(text_file.read())
text_file.close()
print("\nCreating a text file with the writelines() method.")
text_file = open("write_it.txt", "w")
lines = ["Line 1\n",
"This is line 2\n",
"That makes this line 3\n"]
text_file.writelines(lines)
text_file.close()
print("\nReading the newly created file.")
text_file = open("write_it.txt", "r")
print(text_file.read())
text_file.close()
input("\n\nPress the enter key to exit.")
Monday, 22 September 2014
Saturday, 20 September 2014
Thursday, 18 September 2014
Wednesday, 17 September 2014
The Python Standard Library
While The Python Language Reference describes the exact syntax and semantics of the Python language, this library reference manual describes the standard library that is distributed with Python. It also describes some of the optional components that are commonly included in Python distributions.
Python’s standard library is very extensive, offering a wide range of facilities as indicated by the long table of contents listed below. The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming.
Hard drive
The hard drive on your computer is where the software is installed, and it's also where your documents and other files are stored. The hard drive is long-term storage, which means the data is still saved even if you turn the computer off or unplug it.
When you run a program or open a file, the computer copies some of the data from the hard drive onto the RAM. When you save a file, the data is copied back to the hard drive. The faster the hard drive is, the faster your computer can start up and load programs.
DDL, DML and Basic SQL
We will look at the SQL language and the fact that we have the two parts in the DDL and DML.
This lesson will focus on DDL and how we can create the structure of our database.
Todays Task
I would like this following questions answered on a Powerpoint please and emailed to me. You then need to upload ontop your G Drive and link to and label on your blog.
What does DML stand for and what does it do?
What does DDL stand for and what does it do?
Be able to explain the terms record, field, table, query, primary key, relationship, index and search criteria. (Text and Picture on each slide)
Be able to create simple SQL statements to extract, add and edit data stored in databases
Notes from todays lesson
also use the BCS Glossary in your classroom
This lesson will focus on DDL and how we can create the structure of our database.
Todays Task
I would like this following questions answered on a Powerpoint please and emailed to me. You then need to upload ontop your G Drive and link to and label on your blog.
What does DML stand for and what does it do?
What does DDL stand for and what does it do?
Be able to explain the terms record, field, table, query, primary key, relationship, index and search criteria. (Text and Picture on each slide)
Be able to create simple SQL statements to extract, add and edit data stored in databases
Notes from todays lesson
also use the BCS Glossary in your classroom
Tuesday, 16 September 2014
What is an Operator in SQL?
An operator is a reserved word or a character used primarily in an SQL statement's WHERE clause to perform operation(s), such as comparisons and arithmetic operations.
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for multiple conditions in a statement.
- Arithmetic operators
- Comparison operators
- Logical operators
- Operators used to negate conditions
SQL Arithmetic Operators:
Assume variable a holds 10 and variable b holds 20, then:
Operator | Description | Example |
---|---|---|
+ | Addition - Adds values on either side of the operator | a + b will give 30 |
- | Subtraction - Subtracts right hand operand from left hand operand | a - b will give -10 |
* | Multiplication - Multiplies values on either side of the operator | a * b will give 200 |
/ | Division - Divides left hand operand by right hand operand | b / a will give 2 |
% | Modulus - Divides left hand operand by right hand operand and returns remainder | b % a will give 0 |
SQL Comparison Operators:
Assume variable a holds 10 and variable b holds 20, then:
Operator | Description | Example |
---|---|---|
= | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (a = b) is not true. |
!= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (a != b) is true. |
<> | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (a <> b) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (a > b) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (a < b) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (a >= b) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (a <= b) is true. |
!< | Checks if the value of left operand is not less than the value of right operand, if yes then condition becomes true. | (a !< b) is false. |
!> | Checks if the value of left operand is not greater than the value of right operand, if yes then condition becomes true. | (a !> b) is true. |
SQL Logical Operators:
Here is a list of all the logical operators available in SQL.
Operator | Description |
---|---|
ALL | The ALL operator is used to compare a value to all values in another value set. |
AND | The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause. |
ANY | The ANY operator is used to compare a value to any applicable value in the list according to the condition. |
BETWEEN | The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value. |
EXISTS | The EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria. |
IN | The IN operator is used to compare a value to a list of literal values that have been specified. |
LIKE | The LIKE operator is used to compare a value to similar values using wildcard operators. |
NOT | The NOT operator reverses the meaning of the logical operator with which it is used. Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator. |
OR | The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause. |
IS NULL | The NULL operator is used to compare a value with a NULL value. |
UNIQUE | The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates). |
Examples
Consider the CUSTOMERS table having the following records:
SQL> SELECT * FROM CUSTOMERS; +----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+ 7 rows in set (0.00 sec)
Here are simple examples showing usage of SQL Comparison Operators:
SQL> SELECT * FROM CUSTOMERS WHERE SALARY > 5000; +----+----------+-----+---------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+---------+----------+ | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+---------+----------+ 3 rows in set (0.00 sec) SQL> SELECT * FROM CUSTOMERS WHERE SALARY = 2000; +----+---------+-----+-----------+---------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---------+-----+-----------+---------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 3 | kaushik | 23 | Kota | 2000.00 | +----+---------+-----+-----------+---------+ 2 rows in set (0.00 sec) SQL> SELECT * FROM CUSTOMERS WHERE SALARY != 2000; +----+----------+-----+---------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+---------+----------+ | 2 | Khilan | 25 | Delhi | 1500.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+---------+----------+ 5 rows in set (0.00 sec) SQL> SELECT * FROM CUSTOMERS WHERE SALARY <> 2000; +----+----------+-----+---------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+---------+----------+ | 2 | Khilan | 25 | Delhi | 1500.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+---------+----------+ 5 rows in set (0.00 sec) SQL> SELECT * FROM CUSTOMERS WHERE SALARY >= 6500; +----+----------+-----+---------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+---------+----------+ | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+---------+----------+ 3 rows in set (0.00 sec)
Consider the CUSTOMERS table having the following records:
SQL> SELECT * FROM CUSTOMERS;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
7 rows in set (0.00 sec)
Here are simple examples showing usage of SQL Comparison Operators:
SQL> SELECT * FROM CUSTOMERS WHERE AGE >= 25 AND SALARY >= 6500;
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+---------+---------+
2 rows in set (0.00 sec)
SQL> SELECT * FROM CUSTOMERS WHERE AGE >= 25 OR SALARY >= 6500;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
5 rows in set (0.00 sec)
SQL> SELECT * FROM CUSTOMERS WHERE AGE IS NOT NULL;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
7 rows in set (0.00 sec)
SQL> SELECT * FROM CUSTOMERS WHERE NAME LIKE 'Ko%';
+----+-------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+-------+-----+---------+---------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+-------+-----+---------+---------+
1 row in set (0.00 sec)
SQL> SELECT * FROM CUSTOMERS WHERE AGE IN ( 25, 27 );
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+---------+---------+
3 rows in set (0.00 sec)
SQL> SELECT * FROM CUSTOMERS WHERE AGE BETWEEN 25 AND 27;
+----+----------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+---------+---------+
3 rows in set (0.00 sec)
SQL> SELECT AGE FROM CUSTOMERS
WHERE EXISTS (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
+-----+
| AGE |
+-----+
| 32 |
| 25 |
| 23 |
| 25 |
| 27 |
| 22 |
| 24 |
+-----+
7 rows in set (0.02 sec)
SQL> SELECT * FROM CUSTOMERS
WHERE AGE > ALL (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
+----+--------+-----+-----------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+--------+-----+-----------+---------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+--------+-----+-----------+---------+
1 row in set (0.02 sec)
SQL> SELECT * FROM CUSTOMERS
WHERE AGE > ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
+----+----------+-----+-----------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+---------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+-----------+---------+
4 rows in set (0.00 sec)
Subscribe to:
Posts (Atom)