You may optionally enter a comparison operator (<, <=, >, >=, <> or =) at the beginning of each of your search values to specify how the comparison should be done.
import java.awt.Dimension;
import javax.swing.JPanel;
Dimension dim = new Dimension();
dim.height=300;
dim.width=500;
JPanel pan = new JPanel();
pan.setPreferredSize(dim);
30
Java
Zahlen/Rechnen
Nachkommastellen
import java.text.*;
// mindestens 1 Vorkommastelle, genau 2 Nachkommastellen
DecimalFormat f = new DecimalFormat("#0.00");
double d1 = 1234.4843;
double d2 = 0.2;
System.out.println(f.format(d1));
System.out.println(f.format(d2));
// Ausgabe d1=1234,4843 <--- man beachte das Komma!
// Ausgabe d2=0,2
37
Java
GUI, Komponenten allgemein
L&F setzen/ändern
import javax.swing.UIManager;
// Set the L&F to the standard OS-Theme
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// there are other possibilites and L&F-Sets... Just google ;-)
// For example look at L2FProd or Substance...
196
Python
JSON
JSON Daten laden
import json
myJSONFile = "/tmp/Test/myData.json"
json_data = open(myJSONFile)
# In Python werden JSON Daten nach dem Laden wie Dictionaries behandelt
data_dict = json.load(json_data)
import os
os.name
# nt, posix
import platform
platform.system()
# 'Windows', 'Linux', 'Darwin'
platform.release()
# Linux, Windows, Windows, Darwin
'2.6.22-15-generic', 'Vista' ,'10', '8.11.1'
# Example from https://stackoverflow.com/questions/1854/python-what-os-am-i-running-on
from sys import platform as _platform
if _platform == "linux" or _platform == "linux2":
# linux
elif _platform == "darwin":
# MAC OS X
elif _platform == "win32":
# Windows
elif _platform == "win64":
# Windows 64-bit
192
Python
Filehandling
Prüfen ob Datei existiert
import os
filePath="/home/myUser/test.txt"
if (os.path.isfile(filePath)):
print "There it is! ;-)"
else:
print "Whow, file is missing at this place :-("
194
Python
Filehandling
Filename-Manipultation Basics
import os
myFilename = "/tmp/Test/myFile.txt"
# Get only the path
path_only = os.path.dirname(myFilename)
# /tmp/Test
# Filename only
filename_only = os.path.basename(myFilename)
# myFile.txt
# get only filename without extention
filename_without_ext = os.path.basename(os.path.splitext(myFilename)[0])
# myFile
# get only the extension
filename_extention_only = os.path.splitext(myFilename)[1]
# .txt
# put them together again
myFilename_together = os.path.join(path_only, filename_without_ext + filename_extention_only)
# /tmp/Test/myFile.txt
70
Python
Filehandling
Verzeichnis rekursiv durchsuchen
import os
searchPath = '/home/patrick/Desktop/'
# os.walk will go through all subdirectories
for pathentry in os.walk(searchPath, False):
for dir in pathentry[1]:
path = os.path.join(pathentry[0], dir)
if os.path.islink(path):
print path + "... is a link"
# os.unlink(path)
else:
print path + "... is a dir"
# os.rmdir(path)
for file in pathentry[2]:
path = os.path.join(pathentry[0], file)
# os.unlink(path)
print path + "... is a file"
# os.listdir only the given path
for file in os.listdir(searchPath):
fullfilename = os.path.join(searchPath, file)
if os.path.isfile(fullfilename):
print fullfilename + "... is a file"
280
Python
Filehandling
Prüfen ob Verzeichnis existiert
import os.path
from os import path
myFolder = "/tmp/test"
if path.exists(myFolder) & path.isdir(myFolder):
print "Folder exists!"
else:
print "Folder doesn't exist!"