天道酬勤,学无止境

python

[Python/Tkinter]How can I fetch the value of data which was set in function “event_generate”

I'm using Python 2.7.4 and new to Tkinter, and I'm stuck with the following code. I generated an event "test" and set the "data" option with a string, but an error occurred when retrieving it from event. Error -> AttributeError: Event instance has no attribute 'data' from Tkinter import * def handle_it(event): # print "event handler" print event.data root = Tk() root.after(1, lambda: root.event_generate('<<test>>', data="hi there")) root.bind('<<test>>', handle_it) root.mainloop() I can't find the related Python docs for this case, so I referred to the tcl document as below http://www.tcl.tk

2021-06-15 18:16:11    分类:问答    python   user-interface   tkinter   tk

How to import python script files in folders on Google App Engine?

I'm fairly new to both Python and Google App Engine. I want to organize my script files by creating a folder structure. However when I do that I can no longer figure out how to import them. For example: main.py /eggs/spam.py How do I import spam.py in main.py?

2021-06-15 18:13:01    分类:问答    python   google-app-engine

beautifulsoup and invalid html document

I am trying to parse the document http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/acf8e.htm. I want to get countries and names at the beginning of the document. Here is my code import urllib import re from bs4 import BeautifulSoup url="http://www.consilium.europa.eu/uedocs/cms_data/docs/pressdata/en/ecofin/acf8e.htm" soup=BeautifulSoup(urllib.urlopen(url)) attendances_table=soup.find("table", {"width":850}) print attendances_table #this works, I see the whole table print attendances_table.find_all("tr") I get the following error: AttributeError: 'NoneType' object has no

2021-06-15 18:07:43    分类:问答    python   html   parsing   html-parsing   beautifulsoup

Pandas: How to find percentage of group members type per subgroup?

(Data sample and attempts at the end of the question) With a dataframe such as this: Type Class Area Decision 0 A 1 North Yes 1 B 1 North Yes 2 C 2 South No 3 A 3 South No 4 B 3 South No 5 C 1 South No 6 A 2 North Yes 7 B 3 South Yes 8 B 1 North No 9 C 1 East No 10 C 2 West Yes How can I find what percentage of each type [A, B, C, D] that belongs to each area [North, South, East, West]? Desired output: North South East West A 0.66 0.33 0 0 B 0.5 0.5 0 0 C 0 0.5 0.25 0.25 My best attempt so far is: df_attempt1= df.groupby(['Area', 'Type'])['Type'].aggregate('count').unstack().T Which returns

2021-06-15 18:06:05    分类:问答    python   pandas   dataframe   group-by

Can i press two keys simultaneously for a single event using Pygame?

I am making a game using Pygame and Python.I wish to move a block by pressing two keys simultaneously.How can i do that? I am able to move the block using a single key.. but it doesn't work for two keys together. I want the block to move wen i press "right key" and "1" together The given code works efficiently move using a single key if event.type==KEYDOWN: if event.key==K_RIGHT: move_fullcube=left I tried using "and" but it still doesn't work if event.type==KEYDOWN: if event.key==K_RIGHT and event.key==K_1: move_fullcube=left

2021-06-15 18:03:54    分类:问答    python   pygame

How to Pass variables to python script?

I know it can be achieved by command line but I need to pass at least 10 variables and command line will mean too much of programming since these variables may or may not be passed. Actually I have build A application half in vB( for GUI ) and Half in python( for script ). I need to pass variables to python, similar, to its keywords arguments, i.e, x = val1, y = val2. Is there any way to achieve this?

2021-06-15 17:58:20    分类:问答    python

Manually signing an email with DKIM in Python

I'm new to Python and trying to create a email sending script via socket communication but can't seem to sign it with the dkimpy lib. I tried a couple of examples on the web but all returned the same error when running dkim.sign: File "C:\Python34\lib\re.py", line 196, in split return _compile(pattern,flags).split(string, maxsplit) TypeError: expected string or buffer Near as I can tell, the first variable in the dkim.sign function should be a string so I tried readlines () and even .as_string() just to be sure. I have checked the message and it seems RFC822 compliant. But I'll double check if

2021-06-15 17:55:41    分类:问答    python   email   dkim

Multivariate multiple linear regression using Sklearn

I want to train a linear model Y = M_1*X_1 + M_2*X_2 using sklearn with multidimensional input and output samples (e.g. vectors). I tried the following code: from sklearn import linear_model from pandas import DataFrame x1 = [[1,2],[2,3],[3,4]] x2 = [[1,1],[3,2],[3,5]] y = [[1,0],[1,2],[2,3]] model = { 'vec1': x1, 'vec2': x2, 'compound_vec': y} df = DataFrame(model, columns=['vec1','vec2','compound_vec']) x = df[['vec1','vec2']].astype(object) y = df['compound_vec'].astype(object) regr = linear_model.LinearRegression() regr.fit(x,y) But I get the following error: regr.fit(x,y) ... array =

2021-06-15 17:54:37    分类:问答    python   scikit-learn   linear-regression

Seaborn catplot combined with PairGrid

I am playing with the Titanic dataset, and trying to produce a pair plot of numeric variables against categorical variables. I can use Seaborn's catplot to graph a plot of one numeric variable against one categorical variable: import seaborn as sns sns.catplot(data=train, x='Fare', y='Sex') However, if I try to use PairGrid to graph numeric variables against categorical variables: x_vars = ['Fare'] y_vars = ['Sex'] g = sns.PairGrid(train, x_vars=x_vars, y_vars=y_vars) g.map(sns.catplot) It fails with an error: ---------------------------------------------------------------------------

2021-06-15 17:49:35    分类:问答    python   data-visualization   seaborn

how to create a 3D height map in python

I have a 2D array Z that stores the height at that element's position. Other than using the method here in which I need to create array X and Y with the same size as Z, are there any simpler methods to create a 3D height map? The 3D surface height map is something like the first surface plot here.

2021-06-15 17:47:06    分类:问答    python   matplotlib   plot   3d   surface