Pythonに投稿されたコード一覧

Python reqHireScorpionClan

def reqHireScorpionClan(argsList):
	pCaster, pPlot, eSpell = argsList
	pTeam = gc.getTeam(pCaster.getTeam())
	if pTeam.isAtWar(gc.getBARBARIAN_TEAM()):
		return ReasonTypes.REASON_DISABLED_BARBARIAN_IS_ENEMY
	return True

Python テスト

def doWindsofMiasma(argsList):
	ePlayer, eEvent, kTriggeredData = argsList
	iPlayer = kTriggeredData.ePlayer
	pPlayer = gc.getPlayer(iPlayer)
	py = PyPlayer(iPlayer)
	liPromotions = []
	if pPlayer.getStateReligion() != ReligionTypes.NO_RELIGION and gc.getReligionInfo(pPlayer.getStateReligion()).getAlignment() == gc.getInfoTypeForString('ALIGNMENT_EVIL'):
		liPromotions += [	gc.getInfoTypeForString('PROMOTION_DISEASED'),
					gc.getInfoTypeForString('PROMOTION_FEAR'),
					gc.getInfoTypeForString('PROMOTION_FIRE_RESISTANCE'),
					gc.getInfoTypeForString('PROMOTION_FLYING'),
					gc.getInfoTypeForString('PROMOTION_LIGHT'),
					gc.getInfoTypeForString('PROMOTION_MAGIC_IMMUNE'),
					gc.getInfoTypeForString('PROMOTION_MUTATED'),
					gc.getInfoTypeForString('PROMOTION_POISON_RESISTANCE'),
					gc.getInfoTypeForString('PROMOTION_PLAGUED'),
					gc.getInfoTypeForString('PROMOTION_PROPHECY_MARK'),
					gc.getInfoTypeForString('PROMOTION_STIGMATA'),
					gc.getInfoTypeForString('PROMOTION_STONESKIN'),
					gc.getInfoTypeForString('PROMOTION_UNHOLY_TAINT'),
					gc.getInfoTypeForString('PROMOTION_VILE_TOUCH'),
					gc.getInfoTypeForString('PROMOTION_VULNERABLE_TO_FIRE'),
					gc.getInfoTypeForString('PROMOTION_WINTERBORN'),
					gc.getInfoTypeForString('PROMOTION_WITHERED'),
					gc.getInfoTypeForString('PROMOTION_KAIJIN_1'),
					gc.getInfoTypeForString('PROMOTION_MAGIC_WEAKNESS'),
					gc.getInfoTypeForString('PROMOTION_GRAVITATION'),
					gc.getInfoTypeForString('PROMOTION_HOOF'),
					gc.getInfoTypeForString('PROMOTION_FOUR_EYE'),
					gc.getInfoTypeForString('PROMOTION_SLAUGHTER_IMPULSER'),
					gc.getInfoTypeForString('PROMOTION_SHELL'),
					gc.getInfoTypeForString('PROMOTION_HAND_OF_THE_POISON')	]
	elif pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_FELLOWSHIP_OF_LEAVES'):
		liPromotions += [	gc.getInfoTypeForString('PROMOTION_AMPHIBIOUS'),
					gc.getInfoTypeForString('PROMOTION_COLD_RESISTANCE'),
					gc.getInfoTypeForString('PROMOTION_COMMAND1'),
					gc.getInfoTypeForString('PROMOTION_DRILL1'),
					gc.getInfoTypeForString('PROMOTION_FEAR'),
					gc.getInfoTypeForString('PROMOTION_FIRE_RESISTANCE'),
					gc.getInfoTypeForString('PROMOTION_FLYING'),
					gc.getInfoTypeForString('PROMOTION_LIGHT'),
					gc.getInfoTypeForString('PROMOTION_LIGHTNING_RESISTANCE'),
					gc.getInfoTypeForString('PROMOTION_MAGIC_IMMUNE'),
					gc.getInfoTypeForString('PROMOTION_MEDIC1'),
					gc.getInfoTypeForString('PROMOTION_MUTATED'),
					gc.getInfoTypeForString('PROMOTION_NOMAD'),
					gc.getInfoTypeForString('PROMOTION_PERFECT_SIGHT'),
					gc.getInfoTypeForString('PROMOTION_POISON_RESISTANCE'),
					gc.getInfoTypeForString('PROMOTION_PROPHECY_MARK'),
					gc.getInfoTypeForString('PROMOTION_SPIRIT_GUIDE'),
					gc.getInfoTypeForString('PROMOTION_STIGMATA'),
					gc.getInfoTypeForString('PROMOTION_STONESKIN'),
					gc.getInfoTypeForString('PROMOTION_UNHOLY_TAINT'),
					gc.getInfoTypeForString('PROMOTION_VILE_TOUCH'),
					gc.getInfoTypeForString('PROMOTION_VULNERABLE_TO_FIRE'),
					gc.getInfoTypeForString('PROMOTION_WINTERBORN'),
					gc.getInfoTypeForString('PROMOTION_KAIJIN_1'),
					gc.getInfoTypeForString('PROMOTION_MAGIC_WEAKNESS'),
					gc.getInfoTypeForString('PROMOTION_MARK_OF_THE_WARDEN'),
					gc.getInfoTypeForString('PROMOTION_ADVENTURER'),
					gc.getInfoTypeForString('PROMOTION_GRAVITATION'),
					gc.getInfoTypeForString('PROMOTION_HOOF'),
					gc.getInfoTypeForString('PROMOTION_FOUR_EYE'),
					gc.getInfoTypeForString('PROMOTION_SLAUGHTER_IMPULSER'),
					gc.getInfoTypeForString('PROMOTION_SHELL'),
					gc.getInfoTypeForString('PROMOTION_HAND_OF_THE_POISON')	]
	else:
		liPromotions += [	gc.getInfoTypeForString('PROMOTION_DISEASED'),
					gc.getInfoTypeForString('PROMOTION_DRILL1'),
					gc.getInfoTypeForString('PROMOTION_LIGHT'),
					gc.getInfoTypeForString('PROMOTION_MUTATED'),
					gc.getInfoTypeForString('PROMOTION_POISON_RESISTANCE'),
					gc.getInfoTypeForString('PROMOTION_SPIRIT_GUIDE'),
					gc.getInfoTypeForString('PROMOTION_STONESKIN'),
					gc.getInfoTypeForString('PROMOTION_VULNERABLE_TO_FIRE'),
					gc.getInfoTypeForString('PROMOTION_MAGIC_WEAKNESS'),
					gc.getInfoTypeForString('PROMOTION_GRAVITATION'),
					gc.getInfoTypeForString('PROMOTION_HOOF'),
					gc.getInfoTypeForString('PROMOTION_FOUR_EYE'),
					gc.getInfoTypeForString('PROMOTION_SLAUGHTER_IMPULSER'),
					gc.getInfoTypeForString('PROMOTION_SHELL'),
					gc.getInfoTypeForString('PROMOTION_HAND_OF_THE_POISON')	]
	for pUnit in py.getUnitList():
		if pUnit.isAlive():
			if CyGame().getSorenRandNum(100, "Winds of Miasma") < 10 and len(liPromotions) > 0:
				iCount = CyGame().getSorenRandNum(2, 'the number of effects') + 1
				liChosenPromotion = []
				for iLoop in range(iCount):
					liChosenPromotion.append(liPromotions.pop( CyGame().getSorenRandNum(len(liPromotions), "Variant") ))
				liChosenPromotion.sort()
				for iChosenPromotion in liChosenPromotion:
					pUnit.setHasPromotion(iChosenPromotion, True)

Python Dump screen of Coot with white background, black chicken wire, and ball-stick representation

# This script works on Coot.
# Save screen image with white background, ball-stick representation, and black mesh for Gakusei Jikken.
#
# Ver. 2 of http://codetter.com/?p=400
# by yam_cpp, 2011-10-31
#

def save_png_gakusei(arg):
        def get_displayed_maps():
                displayed_maps = []
                for i in range(graphics_n_molecules()):
                        if map_is_displayed(i):
                                print i, "is displayed map."
                                displayed_maps.append(i)

                return displayed_maps
        # get_displayed_maps()

        def change_representation(ballstick):
                # http://biop.ox.ac.uk/coot/doc/coot/Ball-and-Stick-Representation.html
                # (make-ball-and-stick imol atom-selection bond-thickness sphere-size draw-spheres-flag)
                for i in range(graphics_n_molecules()):
                        if mol_is_displayed(i):
                                if  ballstick:
                                        make_ball_and_stick(i, "*", 0.2, 0.3, 1)
                                else:
                                        clear_ball_and_stick(i)
        # get_displayed_mols()

        # Assuming only one map is displayed
        displayed_map = get_displayed_maps()[0]

        # save map colour to restore later
        save_previous_map_colour(displayed_map)

        # change map & background colour and set ball-stick representation
        set_map_colour(displayed_map, 0, 0, 0)
        brighten_maps()
        set_background_colour(255,255,255)
        change_representation(True)

        # save image
        screendump_image(time.strftime("coot-%Y%m%d_%H%M%S.png"))

        # restore
        set_background_colour(0,0,0)
        darken_maps()
        restore_previous_map_colour(displayed_map)
        change_representation(False)

# save_png_gakusei()

add_simple_coot_menu_menuitem(coot_menubar_menu("Extensions"), "Save Image", save_png_gakusei)

Python Dump screen of Coot with white background and black chicken wire

##
# This script works on Coot.
# Save screen image with white background and black mesh for Gakusei Jikken.
#
# by yam_cpp, 2011-09-30
#
def save_png_gakusei(arg):
    def get_displayed_maps():
        displayed_maps = []
        for i in range(graphics_n_molecules()):
            if map_is_displayed(i):
                print i, "is displayed map."
                displayed_maps.append(i)

        return displayed_maps
    # get_displayed_maps()

    # Assuming only one map is displayed
    displayed_map = get_displayed_maps()[0]

    # save map colour to restore later
    save_previous_map_colour(displayed_map)

    # change map & background colour and save image
    set_map_colour(displayed_map, 0, 0, 0)
    brighten_maps()
    set_background_colour(255,255,255)
    screendump_image(time.strftime("./coot-%Y%m%d_%H%M%S.png"))

    # restore
    set_background_colour(0,0,0)
    darken_maps()
    restore_previous_map_colour(displayed_map)

# save_png_gakusei()

add_simple_coot_menu_menuitem(coot_menubar_menu("Extensions"), "Save Image", save_png_gakusei)

Python Dump screen of Coot with white background and black chicken wire

##
# This script works on Coot.
# Save screen image with white background and black mesh for Gakusei Jikken.
#
# by yam_cpp, 2011-09-30
#

def save_png_gakusei(arg):
    def get_displayed_maps():
        displayed_maps = []
        for i in range(graphics_n_molecules()):
            if map_is_displayed(i):
                print i, "is displayed map."
                displayed_maps.append(i)

        return displayed_maps
    # get_displayed_maps()

    # Assuming only one map is displayed
    displayed_map = get_displayed_maps()[0]

    # save map colour to restore later
    save_previous_map_colour(displayed_map)

    # change map & background colour and save image
    set_map_colour(displayed_map, 0, 0, 0)
    set_background_colour(255,255,255)
    screendump_image(time.strftime("~/Desktop/coot-%Y%m%d_%H%M%S.png"))

    # restore
    set_background_colour(0,0,0)
    restore_previous_map_colour(displayed_map)

add_simple_coot_menu_menuitem(coot_menubar_menu("Extensions"), "Save Image", save_png_gakusei)

Python 今の時間

#-*- cofing:utf-8 -*-

import datetime

#u'2011/08/11 9:49:03' みたいな文字列として取得
now = datetime.datetime.now().strftime(u'%Y/%m/%d %H:%M:%S')

Python Pythonで「確実に閉じる」コード

# Python3.x(3.1以上) Python2.x(2.7)
with open(load.txt) as loadfile, open(save.txt) as savefile:
    #ここに処理を書く

# Python3.x(3.0) とか Python2.x(2.6以下)
with open(load.txt) as loadfile:
    with open(save.txt) as savefile:
        #ここに処理を書く

#close()を持たないurlopenの戻り値なんかを閉じる場合
import contextlib
import urllib2

with contextlib.closing(urllib2.urlopen('http://www.python.org')) as fp:
    #ここに処理を書く

Python はろーわーくど

#!/usr/bin/env python
# -*- coding:utf-8 -*-

print "Hello, world!"

Python testcode

#!/usr/bin/env python
#coding: utf-8

print "hello"
Total Pages: 2 / 212

よく投稿されているコード

タグ

最近投稿されたコード