Let's see one simple example ...
>>> my_list = [1,2,3,22,33,11,33,'a','b','c','a']
>>> my_list = list(set(my_list))
>>> print my_list
['a', 1, 2, 3, 33, 11, 'c', 'b', 22]
As you can see the 33 and a items is removed.
Is a blog about python programming language. You can see my work with python programming language, tutorials and news.
>>> my_list = [1,2,3,22,33,11,33,'a','b','c','a']
>>> my_list = list(set(my_list))
>>> print my_list
['a', 1, 2, 3, 33, 11, 'c', 'b', 22]
You can use python google module to start searching anything using this module.
First you need to install the python module using pip.
# pip install google
Downloading/unpacking google
Downloading google-1.05.zip
Running setup.py egg_info for package google
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4
in /usr/local/lib/python2.7/dist-packages (from google)
Installing collected packages: google
Running setup.py install for google
changing mode of build/scripts-2.7/google.py from 644 to 755
changing mode of /usr/local/bin/google.py to 755
Successfully installed google
Cleaning up...
As you can see the next step is to upgrade: beautifulsoup4 ...
# pip install --upgrade beautifulsoup4
Downloading/unpacking beautifulsoup4 from https://pypi.python.org/packages/
source/b/beautifulsoup4/beautifulsoup4-4.3.2.tar.gz#md5=
b8d157a204d56512a4cc196e53e7d8ee
Downloading beautifulsoup4-4.3.2.tar.gz (143Kb): 143Kb downloaded
Running setup.py egg_info for package beautifulsoup4
Installing collected packages: beautifulsoup4
Found existing installation: beautifulsoup4 4.3.1
Uninstalling beautifulsoup4:
Successfully uninstalled beautifulsoup4
Running setup.py install for beautifulsoup4
Successfully installed beautifulsoup4
Cleaning up...
Let's make a simple script to find linux word using google.com ...
>>> from google import search
>>> for url in search('linux', tld='com', lang='en', stop=2):
... print(url)
...
http://en.wikipedia.org/wiki/Linux
http://en.wikipedia.org/wiki/Unix-like
http://en.wikipedia.org/wiki/Linus_Torvalds
http://en.wikipedia.org/wiki/Linux_kernel
http://en.wikipedia.org/wiki/List_of_Linux_distributions
...
If you want to know more about google search function then use this:
>>> help(google.search)
Help on function search in module google:
search(query, tld='com', lang='en', num=10, start=0, stop=None, pause=2.0)
Search the given query string using Google.
...
... and this is all.
$ python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sunpy
>>> from sunpy.net.helioviewer import HelioviewerClient
>>>
>>> hv = HelioviewerClient()
>>> datasources = hv.get_data_sources()
>>>
>>> # print a list of datasources and their associated ids
... for observatory, instruments in datasources.items():
... for inst, detectors in instruments.items():
... for det, measurements in detectors.items():
... for meas, params in measurements.items():
... print("%s %s: %d" % (observatory, params['nickname'], params['sourceId']))
...
Yohkoh SXT AlMgMn: 33
Yohkoh SXT thin-Al: 34
Yohkoh SXT white-light: 35
PROBA2 SWAP 174: 32
STEREO_A EUVI-A 195: 21
STEREO_A EUVI-A 304: 23
STEREO_A EUVI-A 284: 22
STEREO_A EUVI-A 171: 20
STEREO_A COR2-A: 29
STEREO_A COR1-A: 28
STEREO_B EUVI-B 195: 25
STEREO_B EUVI-B 304: 27
STEREO_B EUVI-B 284: 26
STEREO_B EUVI-B 171: 24
STEREO_B COR2-B: 31
STEREO_B COR1-B: 30
SOHO MDI Mag: 6
SOHO MDI Int: 7
SOHO EIT 195: 1
SOHO EIT 304: 3
SOHO EIT 284: 2
SOHO EIT 171: 0
SOHO LASCO C3: 5
SOHO LASCO C2: 4
SDO AIA 1700: 16
SDO AIA 211: 12
SDO AIA 335: 14
SDO AIA 1600: 15
SDO AIA 304: 13
SDO AIA 193: 11
SDO AIA 131: 9
SDO AIA 4500: 17
SDO AIA 94: 8
SDO AIA 171: 10
SDO HMI Mag: 19
SDO HMI Int: 18
This show me all instruments from sunpy. You see all instruments online here >>> hv.download_png('2013/11/29 00:15:00', 50, "[SOHO,LASCO,C3,white-light,1,100]", x0=0, y0=0, width=768, height=768)
The settings are : date and time , the image resolution (arcseconds per pixel), the SOHO LASCO C3 instrument with the layer visibility , center points and size
Today I will sow you some filters using python Image, ImageFilter and ImageChops.
I make this default python script to open one image and after will be delete from memory.See the default image:
I make also one python class named TEST_FILTER.
This class will have all data from filter, see filterargs.
The filterargs args it's one matrix and this will be the filter.
The default script ...
import Image
import ImageFilter
import ImageChops
class TEST_FILTER(ImageFilter.BuiltinFilter):
name="TestFilter"
filterargs = (3,3),10,0,(1,0,1,0,0,0,1,0,1)
def main ():
filename = "test-gentoo.jpg"
image = Image.open(filename);
del image;
if (__name__ =="__main__"):
main();
Let's try first filter : TEST_FILTER
import Image
import ImageFilter
import ImageChops
class TEST_FILTER(ImageFilter.BuiltinFilter):
name="TestFilter"
filterargs = (3,3),10,0,(1,0,1,0,0,0,1,0,1)
def main ():
filename = "test-gentoo.jpg"
image = Image.open(filename);
image.filter(TEST_FILTER).show();
del image;
if (__name__ =="__main__"):
main();
The result will be this:
Now for each filter will change this source code...
image.filter(TEST_FILTER).show();
...with the new filter source code.
Let's see some filters ...
EMBOSS - filter
image.filter(ImageFilter.EMBOSS).show();
FIND_EDGES - filter
image.filter(ImageFilter.FIND_EDGES).show();
BLUR - filter
image.filter(ImageFilter.BLUR).show();
CONTOUR - filter
image.filter(ImageFilter.CONTOUR).show();
DETAIL - filter
image.filter(ImageFilter.DETAIL).show();
EDGE_ENHANCE - filter
image.filter(ImageFilter.EDGE_ENHANCE).show();
Now image convert with bit 1.
image.convert("1").show();
Invert image with ImageChops.
ImageChops.invert(image).show();
The Image and ImageDraw provide simple 2D graphics to create new images, annotate or retouch existing images, and to generate graphics.
Also this can help you to make on the fly images for you.
Let's see one example ...
First you need to import this modules and random python module
import Image,ImageDraw
from random import randint as rint
The next step : make one image , get some random numbers...
You need two colors : first is one random color and second is make from first color, see next source code:
img = Image.new("RGB", (500,500), "#FFFFFF")
draw = ImageDraw.Draw(img)
r,g,b = rint(0,255), rint(0,255), rint(0,255)
dr = (rint(0,255) - r)/500.
dg = (rint(0,255) - g)/500.
db = (rint(0,255) - b)/500.
Now you need to draw lines with this gradient of two colors.
for i in range(500):
r,g,b = r+dr, g+dg, b+db
draw.line((i,0,i,500), fill=(int(r),int(g),int(b)))
... and the python script source code:
import Image,ImageDraw
from random import randint as rint
def random_gradient(name):
img = Image.new("RGB", (500,500), "#FFFFFF")
draw = ImageDraw.Draw(img)
r,g,b = rint(0,255), rint(0,255), rint(0,255)
dr = (rint(0,255) - r)/500.
dg = (rint(0,255) - g)/500.
db = (rint(0,255) - b)/500.
for i in range(500):
r,g,b = r+dr, g+dg, b+db
draw.line((i,0,i,500), fill=(int(r),int(g),int(b)))
img.save(name+".png", "PNG")
if __name__ == "__main__":
for name in range(10):
random_gradient(str(name))
The result of this script will make images :
I play today with jython and can be fun but seams to be to slow in a linux os.
Jython is invoked using the "jython" script and it's an implementation of Python for the JVM.
Install the package jython in your linux distro and you can start to deal with java and python.
When you use jython then script will start with :
#!/usr/bin/env jython
I make also some very simple scripts...
First script make one button and give a action to exit.
#!/usr/bin/env jython
from javax import *
import java
from java import *
import sys
frame = awt.Frame(size=(500,100))
frame.background = 255,255,0
def exit(event):
java.lang.System.exit(0)
my_button = awt.Button("Exit!", actionPerformed=exit)
frame.add(my_button,"Center")
frame.pack()
frame.setVisible(1)
The output is:
The script is easy to make ... it's like gtk with add, pack and action ...
Let's see the next script : one list.
from javax import *
from java import awt
import sys
python_list=[]
python_list.append('text 1')
python_list.append('text 2')
python_list.append('text 3')
python_list.append('text 4')
python_list.append('text 5')
frame=awt.Frame("test list")
panel=swing.JList(python_list)
frame.add(panel,"Center")
frame.pack()
frame.setVisible(1)
... and this is the gui with the list:
I make a simple list and add to the gui using pack() function.
The jython is not easy is much to learn and if you want then go to this website.
This is a simple example with two functions.
First will check the linux command : ls linux command.
The next function will give us some infos about system.
import shlex
import subprocess
from subprocess import Popen, PIPE
import platform
def check_command(command):
cmd='which ' + command
output = Popen(shlex.split(cmd), stdout=PIPE).communicate()[0]
command_path =output.split('\n')[0]
print command_path
return command_path
def check_platform():
arch, exe = platform.architecture()
my_system = platform.system()
if my_system == 'Linux':
distro_name, distro_version, distro_id = platform.linux_distribution()
elif my_system == 'Darwin':
distro_name, distro_version, distro_id = platform.mac_ver()
elif my_system == 'Windows':
distro_name, distro_version, distro_id = platform.win32_ver()
elif my_system == 'Java':
distro_name, distro_version, distro_id = platform.java_ver()
processor = platform.processor() or 'i386'
print processor, my_system, arch, distro_name, distro_version, distro_id
return processor, my_system, arch, distro_name, distro_version, distro_id
check_command('ls')
check_platform()
This python script can be use with any scripts when we need to test commands and system , distro version.
The SunPy python module it's an open-source software library for solar physics using the Python programming language.
The SunPy module is included into nasa projects.
Now, if you want to use this python module then you need to install lapack and blas libraries for development.
I use # pip under super user account to install sunpy and the python modules required by SunPy.
# pip install --upgrade distribute
# pip install --upgrade pyfits
# pip install --upgrade suds
# pip install --upgrade pandas
# pip install --upgrade beautifulsoup4
... also you need to have this python modules: Scipy and Matplotlib.
After that you can install sunpy using:
# pip install sunpy
# pip install --upgrade sunpy
The basic example is one output created by Map() function.
This function can deal with many data sources, like:
SDO/AIA, SDO/HMI
STEREO/EUVI, STEREO/COR
Hinode/XRT
SOHO/EIT, SOHO/LASCO, SOHO/MDI
PROBA2/SWAP
Yohkoh/SXT
Let's try a simple example and will show the result...
>>> import sunpy
>>> ati_map=sunpy.Map(sunpy.AIA_171_IMAGE).peek()
... and output is:
Let's make more sun maps...
>>> eit_map=sunpy.Map(sunpy.EIT_195_IMAGE).peek()
The the output is:
>>> rhessi_map=sunpy.Map(sunpy.RHESSI_IMAGE).peek()
You can see radio spectrometer image using:
>>> callisto_radio_maps=sunpy.Map(sunpy.CALLISTO_IMAGE).peek()
Also you can combine the maps , like in the next example:
>>> eti_rhessi_maps=sunpy.Map(sunpy.EIT_195_IMAGE, sunpy.RHESSI_IMAGE, composite=True).peek()
You can get more infos about Map using :
>>> help(sunpy.Map)
If you want more data to show then you can request data from here.
Also you can give parameters to the Map, like:
date : datetime
| Image observation time
...
This allow you to take more data and info...
Using matplotlib and scipy modules can help to parse and show all infos.
This is a common error when your system don't have the python-dev.
I got this error when I try to use : pip .
Just install the package python-dev and then all will working well.
From python official website I found this :
Python 3.4.0 alpha 2 was released on September 9th, 2013. This is a preview release of the next major release of Python, Python 3.4, and is not suitable for production environments.
Major new features of the 3.4 series, compared to 3.3
Python 3.4 includes a range of improvements of the 3.x series, including hundreds of small improvements and bug fixes. Major new features and changes in the 3.4 release series so far include:
PEP 435, a standardized "enum" module
PEP 442, improved semantics for object finalization
PEP 443, adding single-dispatch generic functions to the standard library
PEP 445, a new C API for implementing custom memory allocators
PEP 446, changing file descriptors to not be inherited by default in subprocesses...
#
-and enable line length checks use this instead#
-don't use multiple statements on the same line, like:if foo == 'test': do_test_thing()
one_test(); two_test()
... this is the correct way:if foo == 'test':
do_test_thing()
one_test()
two_test()
Also you can read about Blender 3D/Python and PEP 8 best practice here.