[PYTHON] Qiitas Syntax hebt Memorandum hervor

Unter Syntaxhervorhebung: Liste der unterstützten Sprachen bei Qiita - Qiita können Sie die häufig verwendete Syntaxhoch verwenden. Wrights Memorandum.

Möglicherweise ist Qiitas Syntax-Hervorhebungs-Engine Pylements.

Die Verwendung und Beispiele der einzelnen Elemente werden angezeigt.

Shell verwandt

console Für Terminalausgang.

console


$ apt-cache depends auditd
auditd
Abhängigkeit: lsb-base
 |Abhängigkeit: mawk
Abhängigkeit: gawk
Abhängigkeit: init-system-helpers
Abhängigkeit: libaudit1
Abhängigkeit: libauparse0
Abhängigkeit: libc6
Abhängigkeit: libgssapi-krb5-2
Abhängigkeit: libkrb5-3
Abhängigkeit: libwrap0
Vorschlag: audispd-plugins
$

bash Beispiel für spamhaus.sh aus cowgill / spamhaus.

spamhaus.sh


#!/bin/bash

# based off the following two scripts
# http://www.theunsupported.com/2012/07/block-malicious-ip-addresses/
# http://www.cyberciti.biz/tips/block-spamming-scanning-with-iptables.html

# path to iptables
IPTABLES="/sbin/iptables";

# list of known spammers
URL="www.spamhaus.org/drop/drop.lasso";

# save local copy here
FILE="/tmp/drop.lasso";

# iptables custom chain
CHAIN="Spamhaus";

# check to see if the chain already exists
$IPTABLES -L $CHAIN -n

# check to see if the chain already exists
if [ $? -eq 0 ]; then

    # flush the old rules
    $IPTABLES -F $CHAIN

    echo "Flushed old rules. Applying updated Spamhaus list...."    

else

    # create a new chain set
    $IPTABLES -N $CHAIN

    # tie chain to input rules so it runs
    $IPTABLES -A INPUT -j $CHAIN

    # don't allow this traffic through
    $IPTABLES -A FORWARD -j $CHAIN

    echo "Chain not detected. Creating new chain and adding Spamhaus list...."

fi;

# get a copy of the spam list
wget -qc $URL -O $FILE

# iterate through all known spamming hosts
for IP in $( cat $FILE | egrep -v '^;' | awk '{ print $1}' ); do

    # add the ip address log rule to the chain
    $IPTABLES -A $CHAIN -p 0 -s $IP -j LOG --log-prefix "[SPAMHAUS BLOCK]" -m limit --limit 3/min --limit-burst 10

    # add the ip address to the chain
    $IPTABLES -A $CHAIN -p 0 -s $IP -j DROP

    echo $IP

done

echo "Done!"

# remove the spam list
unlink $FILE

Python im Zusammenhang

python, py, python3, py3

Dies ist die Python-Quelle wie sie ist. Die Anzeige unterscheidet sich geringfügig zwischen Python und Python3.

Beispiel für Python / Python3

python


import re
import sys
import json
import six
from voluptuous import Schema, Required, Any, Range, Invalid, ALLOW_EXTRA


def validate_io_size(v):
	if re.search("^[0-9]+[bkm]", v) is None:
		raise Invalid("not a valid value (%s)" % str(v))


def get_schema():
	schema = Schema({
		"comment"					: six.text_type,
		Required("operation")		: Any("read", "write"),
		"thread"					: Range(min=1),
		Required("io_size")			: validate_io_size,
		Required("access_percentage")	: Range(min=1, max=100),
	}, extra=ALLOW_EXTRA)
	
	return schema


def main():
	schema	= get_schema()
	
	with open(sys.argv[1], "r") as fp:
		dict_sample	= json.load(fp)
	
	schema(dict_sample)
	six.print_(dict_sample)
	
	return 0


if __name__ == '__main__':
	sys.exit(main())

python3


import re
import sys
import json
import six
from voluptuous import Schema, Required, Any, Range, Invalid, ALLOW_EXTRA


def validate_io_size(v):
	if re.search("^[0-9]+[bkm]", v) is None:
		raise Invalid("not a valid value (%s)" % str(v))


def get_schema():
	schema = Schema({
		"comment"					: six.text_type,
		Required("operation")		: Any("read", "write"),
		"thread"					: Range(min=1),
		Required("io_size")			: validate_io_size,
		Required("access_percentage")	: Range(min=1, max=100),
	}, extra=ALLOW_EXTRA)
	
	return schema


def main():
	schema	= get_schema()
	
	with open(sys.argv[1], "r") as fp:
		dict_sample	= json.load(fp)
	
	schema(dict_sample)
	six.print_(dict_sample)
	
	return 0


if __name__ == '__main__':
	sys.exit(main())

pycon

Möglicherweise für die Konsolenausgabe, wenn Python auf der Konsole gestartet wird, wie z. B. ↓.

# python
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Pycon Beispiel

Von http://docs.python.jp/2/library/random.html Ein Beispiel für eine mit pycon geänderte Python-Konsolenausgabe.

pycon


>>> random.random()        # Random float x, 0.0 <= x < 1.0
0.37444887175646646
>>> random.uniform(1, 10)  # Random float x, 1.0 <= x < 10.0
1.1800146073117523
>>> random.randint(1, 10)  # Integer from 1 to 10, endpoints included
7
>>> random.randrange(0, 101, 2)  # Even integer from 0 to 100
26
>>> random.choice('abcdefghij')  # Choose a random element
'c'

>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]

>>> random.sample([1, 2, 3, 4, 5],  3)  # Choose 3 elements
[4, 1, 5]

pytb/py3tb

Für die Stack-Tracing-Ausgabe von Python.

Beispiel pytb / py3tb

Im folgenden Beispiel war kein Unterschied zwischen pytb und py3tb sichtbar.

pytb


Traceback (most recent call last):
  File "<doctest...>", line 10, in <module>
    lumberjack()
  File "<doctest...>", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range

py3tb


Traceback (most recent call last):
  File "<doctest...>", line 10, in <module>
    lumberjack()
  File "<doctest...>", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range

SQL-bezogen

sql Für SQL-Abfragen.

sql


SELECT hoge FROM table_namme WHERE value > 0

sqlite3

sqlite3


$ sqlite3
SQLite version 3.8.7.1 2014-10-29 13:59:56
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> create table hoge(key text, value integer);
sqlite> insert into hoge values("a", 1);
sqlite> insert into hoge values("b", 2);
sqlite> insert into hoge values("c", 3);
sqlite>
sqlite> select key, value from hoge;
a|1
b|2
c|3
sqlite>

HTML bezogen

HTML

html


<html>
	<head>
		<title>Hello world</title>
	</head>
	<body>
		Hello world
	</body>
</html>

CSS Für kaskadierende Sitze.

css


h1, h2, h3, h4 { margin-top: 0.5em; margin-bottom: 0.1em; }

h3 { margin-left: 0.5em; }
h4 { margin-left: 1.5em; }

body { counter-reset: part; }
h2 { counter-reset: chapter; }
h2:before {
	content: counter(part) ". ";
	counter-increment: part;
}

h3 { counter-reset: section; }
h3:before {
	content: counter(part) "." counter(chapter) ". ";
	counter-increment: chapter;
}

h4 { counter-reset: subsection; }
h4:before {
	content: counter(part) "." counter(chapter) "." counter(section) ". ";
	counter-increment: section;
}

Linux-Befehlsausgabe

diff

--- lhs 2016-01-05 08:13:48.341912876 +0900
+++ rhs 2016-01-05 08:14:19.913913237 +0900
@@ -1,4 +1,4 @@
 hoge
-foo
+aaaaaaaaaaaaa
 bar
-
+bbbbbbbbbbbbbbb

Andere

JSON Obwohl nicht in der Liste enthalten, gilt die Syntaxhervorhebung auch für JSON.

JSON


{
	"text"		: "value",
	"value"		: 100,
	"list"		: ["a", "b", "c"],
	"nest_dict"	: {
		"child"	: 1
	}
}

YAML Aus [Demo] von Pygimenten (http://pylements.org/demo/97004/)

yaml


time: 120
title: '테스트 컨테스트'
contestpw: '0000'

languages:
  -
    display: 'GNU C 4.6.2'
    compile: 'gcc -lm -O2 -o {:basename}.exe {:mainfile}'
    execute: '{:basename}.exe'
    binary: '{:basename}.exe'
  -
    display: 'GNU C++ 4.6.2'
    compile: 'g++ -O2 -o {:basename}.exe {:mainfile}'
    execute: '{:basename}.exe'
    binary: '{:basename}.exe'
  -
    display: 'Microsoft Visual C++ 2010'
    compile: 'cl /Ox {:mainfile}'
    execute: '{:mainfile}.exe'
    binary: '{:mainfile}.exe'
  -
    display: 'Oracle Java SE 7'
    compile: 'javac {:mainfile}'
    execute: 'java {:basename}'
    binary: '{:basename}.class'

problems:
  -
    title: 'HELLOWORLD'
    input: './problems/HELLOWORLD/helloworld.in'
    output: './problems/HELLOWORLD/helloworld.out'
    timelimit: 10
    external_validation: yes
    validator: './validators/Validator.jar'
    validationcmd: 'java -jar {:validator} {:infile} {:outfile} {:ansfile} {:resfile} rz'
    color: 'color0'
  -
    title: 'HELLOWORLD2'
    input: './problems/HELLOWORLD/helloworld.in'
    output: './problems/HELLOWORLD/helloworld.out'
    timelimit: 1
    external_validation: no
    validator: 1
    validator_ignorecase: yes
    color: 'color1'

judgements:
  - 'Yes'
  - 'No - Compilation Error'
  - 'No - Run-time Error'
  - 'No - Time-limit Exceeded'
  - 'No - Wrong Answer'
  - 'No - Excessive Output'
  - 'No - Output Format Error'
  - 'No - Other - Contact Staff'

groups:
  - { name: 'KAIST', scoreboard: yes }
  - { name: 'Test accounts', scoreboard: no }

accounts:
  admins: # default scoreboard: no
    # default administrator -> 1
    - { name: 'Administrator 2', password: '0000' }

  judges: # default scoreboard: no
    - { name: 'Judge 1', password: 'judge1' }
    - { name: 'Judge 2', password: 'judge2' }

  scoreboards: # default scoreboard: no
    - { name: 'Scoreboard 1', password: 'scoreboard1' }
    - { name: 'Scoreboard 2', password: 'scoreboard2' }

  teams: # default scoreboard: yes
    - { name: 'Team 1', group: 1, password: 'team1' }
    - { name: 'Team 2', group: 2, password: 'team2' }
    - { name: 'Team 3', password: 'team3' }
    - { name: 'Team 4', scoreboard: no, password: 'team4' }

Die Vorteile sind im ansible-Playbook-Format nicht so sichtbar.

yaml


---
- name: reboot OS
  hosts: server-under-test
  gather_facts: no
  tasks:
    - name: reboot
      shell: reboot
      async: 0
      poll: 0 

    - name: wait for SSH port down
      local_action: wait_for host={{ inventory_hostname }} port=22 state=stopped

    - name: wait for SSH port up
      local_action: wait_for host={{ inventory_hostname }} port=22 state=started delay=10

    - pause: seconds=5

Recommended Posts

Qiitas Syntax hebt Memorandum hervor
Häufig verwendetes Syntaxmemorandum für jede Sprache
Syntax hebt den Quellcode in PowerPoint / Keynote hervor