Monthly Archives: January 2017
Package a Python program to a single Executable file with Pyinstaller
The advantage of packaging a program is that the user do NOT need any programming environment to use the packaged software.
Packaging a program is very common.For example, when you install most of the software, the ‘setup’ installer will install a packaged software into your computer as a folder and add a shortcut of the executable to your desktop.
However, packaging the program into a single file is much more difficult, I’ll explain why later, let us see how to do it for a Python project.
First things first, make sure you have pyinstaller. I’ve used other libraries like py2exe, but pyinstaller is the best in terms of its compatibility, stability, and user-friendly, the list goes on and on.
in the command window
-> pyinstaller file_tobe_pack.py -o image.ico -F –upx-dir upxfolder
-c (default) open a console window
-w will NOT open a console window
The biggest problem when packaging, especially to a single file:
can not find some specific library dependencies
these are tons of solutions online, but much are so complex and not direct
these is a very simple solution:
Locate the library you import in your program (the one failed packaging) then move the source code to your current directory with the program you wrote, the CHANGE the name of the source code file and change the library name you import.
The packaging software will then search dependency files from the files you made.
Here’s an example:
wordpress tips
questions | methods | |
how to embed content from another site? | install iframe plug in, then insert input content like
|
|
tips: quick edit widgets | under ‘customize’ state, hover mouse to the place you want to edit, shift-click | |
how to add personal customized menu | go to ‘Dashboard’->’Appearance’->’customize’ -> ‘menus’ -> add new menu | |
how to add internal link? | highlight words and insert link (Ctrl + K), type in content from your own site and apply | |
how to add read more? | insert read more tag (shift+alt+t) into your article | |
useful wordpress plugins
plugins | function | ||
|
create share bars contains social medias & create follow me bars | ||
|
can choose and disable contents in Meta widget | ||
|
Draw and edit flow charts, diagrams, images and more while editing a post. |
||
|
Add HTML code to your pages and posts easily using shortcodes | ||
|
Front-end page builder for WordPress with drag and drop editing. Visually customize any page element. | ||
|
profile display widget plugin with pic and social media icons | ||
|
add subtitles | ||
|
add more colors while editing | ||
|
add <pre> button | ||
|
to either downgrade or update WordPress Core to an arbitrary version of your choice. | ||
|
great content Editing | ||
|
highlight codes for various languages | ||
|
convert phone viewers into app viewing | ||
|
visiting stat | ||
|
hide widgets for specific categories, pages, etc |
Pandas basis
Question | answer | explain | ||
how to get how big in memory a DataFrame object is? | df.info() | |||
what is the best representative of null value in Pandas object? | np.nan | import numpy as np | ||
what is the best way to slice a DataFrame by index? | df.iloc[-5:, 2:] | use iloc method | ||
how to convert a DataFrame (excluding indexes) to a numpy ndarray? | df.values | it is a attribute, can’t be called | ||
what is the most basic way to create a DataFrame? | pd.DataFrame(dict) | pass dictionary to; keys are column names | ||
what is broadcasting? | pd[‘new’]=7 | all the values of the new column will be 7 | ||
how to change df’s column names, index names? | pd.columns = [‘a’,’b’,…]
pd.index = [‘c’,’d’,…] |
assign value directly | ||
when read csv, how to specify names of the column | pd.read_csv(path, names=[‘a’,’b’,…..]) | instead, pass header=None will prevent pandas using data as column names, but use 0,1,2,3 …. | ||
when read csv, how to let pandas to turn some specific values into NaN? | pd.read_csv(path, na_values = ‘-1’)
pdf.read_csv(path, na_values = {‘column3’:[‘ -2’, ‘wtf’,…]}) |
all the values which is character ‘-1’ will be rendered to NaN | ||
how to parse data in reading csv | pd.read_csv(path, parse_dates = [[0,1,2]]) | pandas will parse column 1, 2, 3 into one datetype column | ||
does index of df have a name? | pd.index.name = ‘xxx’ | assign a name to the index of df | ||
how to save df to a csv file with other delimiters rather than ‘,’ | pd.to_csv(path, sep=’\t’) | save to a csv file which separates data by tab |
how to batch convert string to Date type
df[‘datestring’]=pd.to_datetime(df[‘datestring’])
how to get 2 DataFrame together, & append one df to another?
bash code
1,3,5…99 | for (( c=1; c<=99; c+=2 )) do echo “Welcome $c times” done |
for i in {1..99..2} do echo $i done |
assign value
read input |
read wowo echo “Welcome $wowo” |
|
calculation output in shell | read X read Y echo “$(($X+$Y))” echo “$(($X-$Y))” echo “$(($X*$Y))” echo “$(($X/$Y))” |
|
if (( a > b )); then … fi |
echo “enter two numbers”;
read a b; echo “a=$a”; |
|
#!/bin/bash
$a = 2462620 if [ “$a” -eq “$b” ];then |
-eq # equal -ne # not equal -lt # less than -le # less than or equal -gt # greater than -ge # greater than or equal |
#!/bin/sh a=10 b=20 if [ $a == $b ] then echo “a is equal to b” elif [ $a -gt $b ] then echo “a is greater than b” elif [ $a -lt $b ] then echo “a is less than b” else echo “None of the condition met” fi |
the Python challange
Python get local varible, call
locals()
2**38 1<<38 pow(2,38)
s=""" g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. """ import string mapping=string.maketrans('abcedfghijklmnopqrstuvwxyz','cedfghijklmnopqrstuvwxyzab') print s.translate(mapping)
import re data = """copy html source code highlighted""" print "".join(re.findall("[A-Za-z]", data))
print "".join(re.findall("[^A-Z]+[A-Z]{3}([a-z])[A-Z]{3}[^A-Z]+", data))
import urllib nothing = '12345' while True: content = urllib.urlopen('http://pythonchallenge.com/pc/def/linkedlist.php?nothing=%(nothing)s' % locals()).read() try: nothing = str(int(content.split(' ')[-1])) except ValueError: if 'Divide' in content: nothing = str(int(nothing) / 2) else: break print content
import requests import re next = "12345" while next.isdigit(): p = requests.get('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s' % next) if 'Divide' in p.content: next = str(int(next) / 2) else: message = "".join(re.findall('nothing is [0-9]*', p.content)) next = message.lstrip('nothing is ') print p.content