pip install --allow-unverified django-admin-tools django-admin-tools==0.5.1
Ярлыки
- Задачки (3)
- Поиск маршрутов (1)
- Amazon (2)
- apache (2)
- Celery (3)
- curl (1)
- Django (9)
- english (1)
- fabric (1)
- GLPK (1)
- HTML (1)
- java (6)
- Job (1)
- JS (1)
- knockout JS (3)
- LP (2)
- mongo (1)
- MySQL (9)
- neo4j (5)
- Oracle (1)
- parallel (1)
- PostgreSQL (3)
- PsyCo (1)
- PyQt (17)
- Python (30)
- routers finding (3)
- Soft (1)
- SSH (4)
- supervisor (1)
- SVG (2)
- SVN (1)
- TEX (1)
- Ubuntu (15)
- web (1)
- wsgi (1)
Показаны сообщения с ярлыком Python. Показать все сообщения
Показаны сообщения с ярлыком Python. Показать все сообщения
среда, 23 апреля 2014 г.
четверг, 10 апреля 2014 г.
воскресенье, 16 марта 2014 г.
pycallgraph
You may import pycallgraph as a module and run the trace graph within your application.
Just before your first line of code you want to graph, put in the following code:
import pycallgraph pycallgraph.start_trace()
To stop the trace and generate the graph, put in the following code /usually at the end of your program/:
pycallgraph.make_dot_graph(' test.png')
The default is to output in png with with the dot tool. You can change them by specifying arguments:
pycallgraph.make_dot_graph(' test.jpg', format='jpg', tool='neato')
понедельник, 22 июля 2013 г.
PyQt: create components dinamically
What to do: create some number of cells (like matrix n by m).
1) we create ScrollArea in *.ui file, and QWidget under it:
<widget class="QScrollArea" name="scrollArea">
<property name="mouseTracking">
<bool>true</bool>
</property>
<property name="verticalScrollBarPolicy" >
<enum>Qt::ScrollBarAlwaysOn</ enum>
</property>
<property name=" horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</ enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="contents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>788</width>
<height>223</height>
</rect>
</property>
</widget>
</widget>
2) my code:
class mainWindow(QtGui.QMainWindow, UIclass.Ui_MainWindow):
def __init__(self, win_parent = None):
QtGui.QMainWindow.__init__( self, win_parent)
self.setupUi(self)
#create QGridLayout on the first start
self.scrollArea.widget(). layout = QtGui.QGridLayout()
self.scrollArea.widget(). layout.objectName=' inputLayout'
# create cells for input data
self.connect(self.createCells, QtCore.SIGNAL("clicked()"), self.createCellsFunc)
# function for create cells:
def createCellsFunc(self):
# all was doing if we know number columns and number rows
if str(self.text1.toPlainText()). isdigit() and str(self.text2.toPlainText()). isdigit():
# height and length for each cell
h=25
l=50
# delete all old objects (exclude layout)
for u in self.scrollArea.widget(). children():
if u.objectName!='inputLayout':
# "delete some later"
u.deleteLater()
# hide now
u.hide()
# set window as parent (not ScrollArea)
u.setParent(self)
# number of columns
hCount=int(self.text1. toPlainText())
# number of rows
vCount=int(self.text2. toPlainText())
for i in range(0,hCount):
for j in range(0,vCount):
# while creating insert text '0' and determine parent
textArea = QtGui.QTextEdit('0', self.scrollArea)
# we give name to the element for fast find him
textArea.objectName = 'a'+str(i)+'-'+str(j)
# dimentions
textArea.setMinimumSize(l,h)
textArea.setMaximumSize(l,h)
# add created element to layer of widget (textArea - adding object, (j + 1) and (i + 1) - coordinates on layer grid of adding object)
self.scrollArea.widget(). layout.addWidget(textArea,j+1, i+1)
# put layer to widget
self.scrollArea.widget(). setLayout(self.scrollArea. widget().layout)
self.scrollArea.widget(). layout
3) get data from cells:
#horizontal cells count
hCount=int(self.text1. toPlainText())
#vertical cells count
vCount=int(self.text2. toPlainText())
#check for all child objects "ScrollBox": did all data is number?
for u in self.scrollArea.findChildren( QtGui.QTextEdit):
try:
float(str(u.toPlainText()))
except:
self.labelWarning.setText( QtCore.QString(u'Some data is not number'))
return None
#put data to array
a,a0=list(),list()
for j in range(0,vCount):
for i in range(0,hCount):
for u in self.scrollArea.findChildren( QtGui.QTextEdit):
if u.objectName=='a'+str(i)+'-'+ str(j):
a0.append(float(u.toPlainText( )))
a.append(a0)
a0=list()
четверг, 21 марта 2013 г.
use mosek for solve LP in Python
#import module
import mosek
#import numpy arrays or mosek arraystry: from numpy import array,zeros,ones
except ImportError:
from mosek.array import array, zeros, ones
# stream for collect data from MOSEKdef streamprinter(text): sys.stdout.flush() #main functiondef main (bkc,blc,buc,bkx,blx,bux,csub, cval,asub,acof,ptrb,ptre,niv, ntc): # create mosek environment env = mosek.Env () # add stream to environment env.set_Stream (mosek.streamtype.log, streamprinter) # create task task = env.Task(0,0) # add output-data stream to task task.set_Stream (mosek.streamtype.log, streamprinter)
#there are examples of creation right part of constraints and variable constraints# bkc = [ mosek.boundkey.up, mosek.boundkey.lo ]# blc = [ -inf, -4.0 ]
# buc = [ 250.0, inf ]
# bkx = [ mosek.boundkey.lo, mosek.boundkey.lo ]
# blx = [ 0.0, 0.0 ]
# bux = [ inf, inf ]
#coefficients of goal function# c = [ 1.0, 0.64 ]
#coeff numbers in constraints
# asub = [ array([0, 1]), array([0, 1]) ]
#...and this values
# aval = [ array([50.0, 3.0]), array([31.0, -2.0]) ]
#number of variables numvar = len(bkx)
#number of constraints numcon = len(bkc)
# create constraints
task.append(mosek.accmode.con, numcon)
# create variables
task.append(mosek.accmode.var, numvar)
# The goal function adding (csub - numbers of variables, cval - coefficients of variables)
task.putcfix(0.0) task.putclist(csub,cval)
# add constraints task.putboundslice(mosek. accmode.con, 0, numcon,
bkc, blc, buc)
# add variable constraints task.putboundslice(mosek. accmode.var, 0, numvar,
bkx, blx, bux)
# Input non-zero elements of matrix A (by column) for j in range(numcon):
aptrb,aptre = ptrb[j],ptre[j]
task.putavec(mosek.accmode. con,j,
asub[aptrb:aptre],
acof[aptrb:aptre])
# input optimize criterion (minimize/maximize) task.putobjsense(mosek. objsense.minimize) # integer variables numlist=list()
typelist=list()
for i in range(niv,numvar):
numlist.append(i)
typelist.append(mosek. variabletype.type_int)
task.putvartypelist(numlist, typelist)
# constants of begin solve task.putintparam(mosek.iparam. mio_construct_sol, mosek.onoffkey.on);
# all variable are unknown in begin of solve task. makesolutionstatusunknown( mosek.soltype.itg);
#we can save task in some file #task.writedata("test.lp") # solve task task.optimize()
# output solution
if task.solutiondef(mosek. soltype.itg):
...
#import numpy arrays or mosek arraystry: from numpy import array,zeros,ones
except ImportError:
from mosek.array import array, zeros, ones
# stream for collect data from MOSEKdef streamprinter(text): sys.stdout.flush() #main functiondef main (bkc,blc,buc,bkx,blx,bux,csub,
#there are examples of creation right part of constraints and variable constraints# bkc = [ mosek.boundkey.up, mosek.boundkey.lo ]# blc = [ -inf, -4.0 ]
# buc = [ 250.0, inf ]
# bkx = [ mosek.boundkey.lo, mosek.boundkey.lo ]
# blx = [ 0.0, 0.0 ]
# bux = [ inf, inf ]
#coefficients of goal function# c = [ 1.0, 0.64 ]
#coeff numbers in constraints
# asub = [ array([0, 1]), array([0, 1]) ]
#...and this values
# aval = [ array([50.0, 3.0]), array([31.0, -2.0]) ]
#number of variables numvar = len(bkx)
#number of constraints numcon = len(bkc)
# create constraints
task.append(mosek.accmode.con,
# create variables
task.append(mosek.accmode.var,
# The goal function adding (csub - numbers of variables, cval - coefficients of variables)
task.putcfix(0.0) task.putclist(csub,cval)
# add constraints task.putboundslice(mosek.
bkc, blc, buc)
# add variable constraints task.putboundslice(mosek.
bkx, blx, bux)
# Input non-zero elements of matrix A (by column) for j in range(numcon):
aptrb,aptre = ptrb[j],ptre[j]
task.putavec(mosek.accmode.
asub[aptrb:aptre],
acof[aptrb:aptre])
# input optimize criterion (minimize/maximize) task.putobjsense(mosek.
typelist=list()
for i in range(niv,numvar):
numlist.append(i)
typelist.append(mosek.
task.putvartypelist(numlist,
# constants of begin solve task.putintparam(mosek.iparam.
# all variable are unknown in begin of solve task.
#we can save task in some file #task.writedata("test.lp") # solve task task.optimize()
# output solution
if task.solutiondef(mosek.
...
среда, 20 марта 2013 г.
Python: your time to each program (Windows)
Script that write how many time each program used in Windows (by title of show window).
import sys
#module for work with windows (32bit)
import win32gui
txt=''
time1=time.time()
#times dictionary
appdict=dict()
while True:
#pause (it's influence to accuracy)
time.sleep(5)
#title of the current window
newtxt = win32gui.GetWindowText(win32gu i.GetForegroundWindow())
#if window was changed than update time of beginning work with other window
if txt!=newtxt:
time2=time.time()
#add previous time to dict
if appdict.get(txt)!=None:
appdict[txt]=appdict[txt]+roun d(time2,0)-round(time1,0)
else:
appdict[txt]=round(time2,0)-ro und(time1,0)
txt=newtxt
time1=time.time()
f=open('timerep.csv', 'w')
csvwr = csv.writer(f, delimiter=';',lineterminator=' \n')
#We write only window that summary time over 5 minutes (Other window is non-influential).
for i in appdict.items():
if i[1]>=300:
csvwr.writerow(i)
f.close()
import sys
#module for work with windows (32bit)
import win32gui
txt=''
time1=time.time()
#times dictionary
appdict=dict()
while True:
#pause (it's influence to accuracy)
time.sleep(5)
#title of the current window
newtxt = win32gui.GetWindowText(win32gu
#if window was changed than update time of beginning work with other window
if txt!=newtxt:
time2=time.time()
#add previous time to dict
if appdict.get(txt)!=None:
appdict[txt]=appdict[txt]+roun
else:
appdict[txt]=round(time2,0)-ro
txt=newtxt
time1=time.time()
f=open('timerep.csv', 'w')
csvwr = csv.writer(f, delimiter=';',lineterminator='
#We write only window that summary time over 5 minutes (Other window is non-influential).
for i in appdict.items():
if i[1]>=300:
csvwr.writerow(i)
f.close()
понедельник, 18 марта 2013 г.
using GLPK in Python (Linear programming)
# import PuLP's functions
from pulp import *
# create new task of Linear programming (LP) with maximize of the goal functions
prob = LpProblem("Knapsack problem", LpMaximize)
# variable (integer)
x1 = LpVariable("x1", 0, 10, 'Integer')
x2 = LpVariable("x2", 0, 10, 'Integer')
x3 = LpVariable("x3", 0, 10, 'Integer')
# goal function ("the cost of all in backpack")
# create new task of Linear programming (LP) with maximize of the goal functions
prob = LpProblem("Knapsack problem", LpMaximize)
# variable (integer)
x1 = LpVariable("x1", 0, 10, 'Integer')
x2 = LpVariable("x2", 0, 10, 'Integer')
x3 = LpVariable("x3", 0, 10, 'Integer')
# goal function ("the cost of all in backpack")
prob += 17*x1 + 30*x2 + 75*x3, "obj"
# constraints ("weight of the backpack")
prob += 1.5*x1 + 2.5*x2 + 6*x3 <= 20, "c1"
# run solver
prob.solve()
# print task status
print "Status:", LpStatus[prob.status]
# print optimized variable result
for v in prob.variables():
print v.name, "=", v.varValue
# print value of goal function
print ("objective = %s$" % value(prob.objective))
Alternate:
we can create LP-file (CPLEX) and use it with GLPK:
# constraints ("weight of the backpack")
prob += 1.5*x1 + 2.5*x2 + 6*x3 <= 20, "c1"
# run solver
prob.solve()
# print task status
print "Status:", LpStatus[prob.status]
# print optimized variable result
for v in prob.variables():
print v.name, "=", v.varValue
# print value of goal function
print ("objective = %s$" % value(prob.objective))
Alternate:
we can create LP-file (CPLEX) and use it with GLPK:
cmd=['glpsol.exe','--cpxlp', outputname,'-o',solfile]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
for line in p.stdout:
print(">>>>> " + str(line.decode()).rstrip())
четверг, 15 ноября 2012 г.
Python processes: some another way
signExit = False
pCount = 0
while not signExit:
for p in processes:
if p.successful:
print p.get()
pCount += 1
if pCount == numTasks:
break
print pCount
понедельник, 12 ноября 2012 г.
python: delete files by regex
import os, re
def purge(dir, pattern):
for f in os.listdir(dir):
if re.search(pattern, f):
os.remove(os.path.join(dir, f))
суббота, 3 ноября 2012 г.
stdin stdout in Python
import sys
import time
import copy
import csv
import re
while True:import time
import copy
import csv
import re
#get string from stdin
s=sys.stdin.readline()
sss=[str(word).strip() for word in s.splitlines(False)]
s=sys.stdin.readline()
sss=[str(word).strip() for word in s.splitlines(False)]
#if string="Q", than exit
if sss[0]=='Q': break
try:
for ss in sss:
if sss[0]=='Q': break
try:
for ss in sss:
#with regexp parse string
pattern=re.compile('^(\d{6});( \d{6})$')
u=pattern.search(ss).groups()
try:
u=pattern.search(ss).groups()
try:
#data to our function
seq1,seq2=getSeq(u[0],u[1])
except:
stseq1=list()
trseq1=list()
#send results to stdoutseq1,seq2=getSeq(u[0],u[1])
except:
stseq1=list()
trseq1=list()
print(';'.join(str(j) for j in seq1) ,sep=';', end='\n', file=sys.stdout)
print(';'.join(str(j) for j in seq2) ,sep=';', end='\n', file=sys.stdout)
XML in Python
#import
from xml.dom import minidom
#parse file
xmldoc=minidom.parse('input. xml')
#get element by tag name
#xmldoc.getElementsByTagName(' ID')
#element as list
#xmldoc.getElementsByTagName(' ID')[0]
#get child node
#xmldoc.getElementsByTagName(' ID')[0].childNodes[0]
#some example
#loop by all child nodes of element with name 'Fragments'
for i in reversed(xmldoc. getElementsByTagName(' Fragments')[0].childNodes):
#only elements with name 'Fragment'
if i.nodeName=='Fragment':
#"number of fragment" - from attribute 'Num'
fragmentNum=i.getAttribute(' Num')
#list in child node
for j in i.getElementsByTagName('RPs')[ 0].childNodes:
if j.nodeName=='RP':
for u in j.childNodes:
if u.nodeName=='Times':
#choose attribute of the node
i1=j.getAttribute('Pgsz')
Python: neo4j.py embedded install
1) install
java-6-openjdk/jre
sudo easy_install neo4j-embedded
2) set path to JVM:
export JAVA_HOME=/usr/lib/jvm/
or set path in jpype (/usr/lib/python2.7/dist- packages/jpype/_linux.py):
in string 53: "java_home = os.getenv('JAVA_HOME')" => "java_home = '/usr/lib/jvm/java-6- openjdk/jre/'"
3) in file /usr/local/lib/python2.7/dist- packages/neo4j_embedded-1.6- py2.7.egg/neo4j/_backend.py
string 132 correct follow:
| ||
| 133 |
| |
| 134 |
| |
| 132 | 135 |
|
| 133 |
| |
| 136 |
|
4) try import neo4j:
>>import neo4j
>>
5) create DB in Python:
from neo4j import GraphDatabase
# Create db
db = GraphDatabase('/data/neo4j_DB')
...
print('all ok')
# Shut down db
db.shutdown()
problem with easy_install mysql-python
http://stackoverflow.com/ questions/1511661/virtualenv- mysql-python-pip-anyone-know- how
You can install "libmysqlclient-dev" from ubuntu software center
You can install "libmysqlclient-dev" from ubuntu software center
info about EC2 servers
get info about ec2 servers with boto:
import boto
from boto import ec2
def getEc2Hosts():
ec2Hosts = set()
AccessKey="""AccessKey"""
SecretAccessKey="""SecretAccessKey"""
# make connection
conn = ec2.connect_to_region('eu- west-1', aws_access_key_id = AccessKey, aws_secret_access_key = SecretAccessKey, )
# get servers status
instanceStatuses = conn.get_all_instance_status()
instanceStatuses = conn.get_all_instance_status()
# get info about all servers
allInst = conn.get_all_instances()
allInst = conn.get_all_instances()
# for our - only ID, name and system status of the each server:
instStatus = {i1.id: (i1.state_name, str(i1.system_status)) for i1 in instanceStatuses}
for i in allInst:
for g in i.groups:
instStatus = {i1.id: (i1.state_name, str(i1.system_status)) for i1 in instanceStatuses}
for i in allInst:
for g in i.groups:
# only servers for foto
if g.name == 'foto-servers':
if g.name == 'foto-servers':
#we can use only running servers with OK status
if instStatus[i.instances[0].id][ 0] == 'running' and instStatus[i.instances[0].id][ 1] == 'Status:ok':
if instStatus[i.instances[0].id][
# add public name to set
ec2Hosts.add(i.instances[0]. public_dns_name)
return ec2Hosts
ec2Hosts.add(i.instances[0].
return ec2Hosts
Python: use Psyco in PyQt4 thread
import psyco
#make function bind in thread before call it
psyco.bind(myFunc())
myFunc()
psyco.bind()
psyco.bind() for ezch function (for all don't working for me):
psyco.bind(mySolve.startSolve)
result=mySolve.startSolve(xArray)cleanup SVN-files
I copied folder from SVN. And it include svn-files. I deleted it with follow script:
import os
from subprocess import call
d = os.walk('my directory with SVN-tails')
for i in d:
print i # about folder
if i[0][-4:] == '.svn':
print i[0]
# delete SVN-files
call(['rm', '-R', i[0]])
Python: list of the equal lists
>>> months = [["0"*31]*12]
>>> months
[['0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000']]
[['0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000', '0000000000000000000000000000000']]
Python: local time for current timezone with pytz
import pytz
tzkiev = pytz.timezone('Europe/Kiev')
tzkiev.fromutc(< UTC time >)
<in result - local time>
time analysis execute Python script
follow command:
python -m cProfile -s time myProject.py
In result: execute time for each function, number of call each function
IMHO, if we want see execute time and calls of only one function, we can call only this function.
python -m cProfile -s time myProject.py
In result: execute time for each function, number of call each function
IMHO, if we want see execute time and calls of only one function, we can call only this function.
Подписаться на:
Сообщения (Atom)