Category Archives: Python

How to Solve Python RuntimeError: Working outside of application context.

Error in running a python program:

Error content: running timeout — &> working outside of application context

So you can try to add context to the problem code manually

( app.app_ Context () is a context expression that returns a context manager (appcontext())

with app.app_context():
  #TODO  your codes

Run the program again

Jupyter + Matplotlib > Warning:”matplotlib is currently using a non-GUI backend, ” > fig.show()

Action environment
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.1.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

After changing the environment from Ubuntu 14.04 LTS to Ubuntu 16.04 LTS, the following Warning is displayed when drawing with Jupyter + Matplotlib.

/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py:403: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
“matplotlib is currently using a non-GUI backend, “

% matplotlib  inline

# sine curve learning 
# Jul. 08, 2017

import numpy as np
import matplotlib.pyplot as plt

data1 = np.loadtxt('input.csv', delimiter=',')
#data2 = np.loadtxt('res.170708_0830.cut', delimiter=',')
data2 = np.loadtxt('res.170709_0900.cut', delimiter=',')

input1 = data1[:,0]
output1 = data1[:,1]
input2 = data2[:,0]
output2 = data2[:,1]

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)

ax1.scatter(input1,output1)
ax2.scatter(input2,output2)

ax1.set_xlabel('x')
ax1.set_ylabel('sin(x)')
ax1.set_xlim([0,1.0])
ax1.grid(True)

ax2.set_xlabel('x')
ax2.set_ylabel('sin(x)')
ax2.set_xlim([0,1.0])
ax2.grid(True)

fig . show ()

Correspondence> fig.show () is unnecessary

How to Solve Error: parent directory is world writable but not sticky

When installing pyenv locally, the following error occurred:

➜   brew install pyenv
==> Downloading https://github.com/yyuu/pyenv/archive/v20160310.tar.gz
Already downloaded: /Library/Caches/Homebrew/pyenv-20160310.tar.gz
Error: parent directory is world writable but not sticky
Please report this bug:
    https://git.io/brew-troubleshooting
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tmpdir.rb:92:in `mktmpdir'
/usr/local/Library/Homebrew/utils/fork.rb:6:in `safe_fork'
/usr/local/Library/Homebrew/formula_installer.rb:572:in `build'
/usr/local/Library/Homebrew/formula_installer.rb:231:in `install'
/usr/local/Library/Homebrew/cmd/install.rb:214:in `install_formula'
/usr/local/Library/Homebrew/cmd/install.rb:93:in `block in install'
/usr/local/Library/Homebrew/cmd/install.rb:93:in `each'
/usr/local/Library/Homebrew/cmd/install.rb:93:in `install'
/usr/local/Library/brew.rb:83:in `<main>'

Solution:
first check the read and write permissions of the folder of your own /private/tmp, as follows on my mac machine:

  ls -ld /tmp
lrwxr-xr-x@ 1 root  wheel  11 11 13  2014 /tmp -> private/tmp
  ls -ld /private/tmp
drwxrwxrwt  13 root  wheel  442  1 18 09:07 /private/tmp

In order to solve this’sticky’ permission of /private/tmp, you need to execute the following command:
sudo chmod +t /private/tmp/

After the execution, the installation is successful:

➜  brew install pyenv
==> Downloading https://github.com/yyuu/pyenv/archive/v20160310.tar.gz
Already downloaded: /Library/Caches/Homebrew/pyenv-20160310.tar.gz
==> Caveats
To use Homebrew's directories rather than ~/.pyenv add to your profile:
  export PYENV_ROOT=/usr/local/var/pyenv

To enable shims and autocompletion add to your profile:
  if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
==> Summary
🍺  /usr/local/Cellar/pyenv/20160310: 469 files, 2M, built in 0 seconds

Django admin Error: ‘WSGIRequest’ object has no attribute ‘user’

Django admin error ‘wsgirequest’ object has no attribute ‘user’

Django version 1.8. Upgrade to django2.0

After the Django service is started, when logging into the admin background, the following error will be thrown:

Django admin error 'wsgirequest' object has no attribute 'user'

Google said that there is a problem with midview configuration, and the order should be kept. See( http://stackoverflow.com/questions/26576192/wsgirequest-object-has-no-attribute-user ):

'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',

But after this setting, it is still not solved.
My 1.8.3 midview configuration is as follows:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',

)

In fact, this is a problem with Django version. Before 1.10, the key of middleware was middware_ After midview, search is 1.10. So when the version of the development environment is inconsistent with other environments, you should be very careful, there will be holes.
Change the configuration to Django 2.0: (this is generated by Django 2.1 and directly copied and replaced)

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Problem solving.

TypeError: strptime() argument 1 must be str, not bytes

About TypeError:strptime()argument1mustbestr,notbytes Explanation
When using datetime.strptime(s,fmt) to output the result date result, an error occurs

TypeError:strptime()argument1mustbestr,notbytes

My source code is as follows

def datestr2num(s):
return datetime.strptime(s,“%d-%m-%Y”).date().weekday()

dates=np.loadtxt(‘data.csv’,delimiter=’,,usecols=(1,),converters={1: datestr2num},unpack=True)

data.csv:

When the compiler opens the data.csv file, the second column of array values in the table is extracted and returned to dates, the second column of values is a date format string, but because we are opening the second column of values in binary encoding format is, the returned value bytes string bytes, so it needs to be string, then the string is decoded with the function decode(‘asii ‘), into string format.

def datestr2num(s):
return datetime.strptime(s.decode(‘ascii’),“%d-%m-%Y”).date().weekday()

dates=np.loadtxt(‘data.csv’,delimiter=’,,usecols=(1,),converters={1: datestr2num},unpack=True)

 

lineis a bytestring,because you opened the file in binary mode. You’ll need to decode the string; if it is a date string matching the pattern,you can simply use ASCII:

 time.strptime(line.decode('ascii'), '%Y-%m-%d ...')

You can add a'ignore'argumentto ignore anything non-ASCII,but chances are the line won’t fit your date format then anyway.

Note that you cannot pass a value that containsmorethan the parsed format in it; a line with other text on itnotexplicitly covered by thestrptime()pattern willnotwork,whatever codec you used.

And if your input really varies that widely in codecs,you’ll need to catch exceptions one way or another anyway.

Aside from UTF-16 or UTF-32,I wouldnotexpect you to encounter any codecs that use differentbytesfor the arabic numerals. If your input really mixes multi-byte and single-byte codecs in one file,you have a bigger problem on your hand,notin the least because newline handling willbemajorly messed up.

 

 

TypeError: write() argument must be str, not bytes

ModuleNotFoundError: No module named ‘jsonpath’

The new situation after the last blog about no jsonpath solution

During execution, the console prompts the following error code:

TypeError: write() argument must be str, not bytes

As a beginner, I didn’t understand many copies on CSDN. Then I found the answer I wanted and solved my problem on stackoverflow

Link: typeerror: must be STR, not bytes

The question is like this, cut a picture

The answer is also to the point, cut a picture

The output file should be in binary mode

There is no problem with the console execution after modification

Then I found out from the python document query:

Python: open/file operation
F = open (‘/ TMP/Hello’,’w ‘)
#open (path + file name, read-write mode)
#read-write mode: R read-only, R + read-write, w New (will cover the original file), a append, B binary file. Common modes

 

[UNK]Mac OS X El Capitan Install IPython[UNK]XtraFinder

Originally, I didn’t think there would be this article, because it was very easy to install IPython on the Mac OS X 10.9 system at the beginning. Who would have thought that the upgrade to Mac OS X 10.11 (El Capitan) now makes many things that were done naturally no longer so convenient.

Because Mac OS X 10.11 (El Capitan) has enabled the SIP (system integrity protection) function by default, some system files can’t be solved even with sudo Dafa, such as installing IPython and XtraFinder.app , can not be perfectly installed and used under SIP “protection”.

IPython:

Official website: http://ipython.org

IPython is a python interactive shell, which is much easier to use than the default Python shell , supports variable auto completion, auto indentation, supports bash shell commands, and has many useful functions and functions built in.

The installation method is as follows:

First, restart the system, press and hold the Command + R key while the power on sound sounds, release the keyboard when the black-and-white image of Apple logo appears, enter the operation panel of recovery mode, and then open the Terminal , input:

csrutil disable

Execute the command and restart the system, so the SIP function is turned off.

After entering the system, open “terminal” provided that PIP has been installed (refer to this article), and then enter:

sudo pip install ipython

XtraFinder:

Official website: https://www.trankynam.com/xtrafinder

xtrafinder a finder enhancement tool, supports multi label finder, supports full screen function of Mac OS X, supports setting whether to display xtrafinder icon in the top right corner menu, supports fast show/hide files, supports custom shortcut keys, supports right-click new files, etc.

the installation method is as follows:

First go to the official website to download the installation package, and then execute the installation file on the premise that the SIP function has been turned off.

XtraFinder.app After successful installation, the in /library/scriptingadditions/ directory will be saved XtraFinder.osax move the file to the /system/library/scriptingadditions/ directory

Then open the Terminal and enter the command:

sudo ln -s /System/Library/ScriptingAdditions/XtraFinder.osax /Library/ScriptingAdditions/

After restarting the system, it can be used normally.

If the above two items are installed successfully, you can restart the SIP function. The method of opening is to press and hold the Command + R key to enter the operation panel of recovery mode after restart, and then open the Terminal Input:

csrutil enable

After the command is executed, restart the system, so that the SIP function works normally again

Python: How to Brute Force Crack Zip Password with Batch Dictionary

Tool cracking

Two days ago, a wave of project cases came down on the Internet, and all of them turned out to be encrypted compressed packages, so I went to the Internet to find a tool to crack compressed packages.
it took a long time for one compressed package to crack. After decompressing three compressed packages, I gave up and prepared to find another method

Password dictionary

Coincidentally, the three cracked codes are all 4-digit codes, which reminds me to rely on the dictionary to crack
do as you say, and reach out to

f = open('passdict4.txt','w')
for id in range(10000):
  password = str(id).zfill(4)+'\n'
  f.write(password)
f.close()

Sure enough, the guess is right, and the cracking speed is really fast.
Xi dada said, Science and technology is the first productivity, and innovation is the first driving force leading development.
since there are faster methods, why not study an automatic method

Zipfile Library

Python has a library called zipfile, which can extract zip files from its related documents

ZipFile.extractall (path = none, members = none, PWD = none)
unzip the specified file in the zip document to the current directory.
The parameter path specifies the folder where the parsing file is saved
the parameter members specifies the name of the file to be decompressed or the corresponding zipinfo object
the parameter pwd is the decompression password.

Just loop through the zip files in the folder and unzip them one by one.
note: there is a Chinese file name in Python 3 with messy code, which will ZipFile.py “Cp437” in can be changed to “GBK” (two places need to be changed)


z = zipfile.ZipFile(f'{file_path}', 'r'
z.extractall(path=f"{
   root})
z.close()

On this basis, add the circular password dictionary
note: since decompression is a password error, an exception will be generated. Here, try except can be used for processing


passFile = open(r"D:\python\passdict4.txt")
 for line in passFile.readlines():
    password = line.strip('\n')
      try:
         zip_file.extractall(path=f"{root}", pwd=password.encode("utf-8"))
		 print(password)
		 break
	except:
		 pass	 
 zip_file.close()

After such an operation, I thought it would be a great success, but I didn’t expect it to be so simple.
most of the time, it can be cracked, but some of them fail, and the helpless websites find a way to do it.
at first, they thought it was the wrong call of the extractall method, but the error was the wrong password, which made me very fascinated
by this Only when I saw an article in one place did I know

By default, WinRAR uses AES-256 to encrypt zip files in CTR mode, while traditional encryption uses CRC32 encryption, that is, zip 2.0 traditional encryption algorithm, although AES-256 is better than zip 2.0 traditional encryption algorithm is much safer, but it may be compatible with some old decompression software. However, the zipfile module in Python standard library only supports CRC32 encrypted zip files, so it is impossible to decompress through zipfile library without traditional encryption

I’ve spent so much effort, and it’s really too much effort if I give up here.
since the encryption method is different, how can the decompression software be directly decompressed?
here’s an idea. If I can call the decompression software from the code, it’s easy to do.
so I quickly use the stunt

to solve the problem Successful search, 7z and other decompression software has related functions

Calling the third party software command line

Configure environment variables

command line verification

nese, the configuration is successful

passFile = open(r"D:\python\passdict4.txt")
   for line in passFile.readlines():
      password = line.strip('\n')
      # t
      command='7z -p'+password+' t '+file_path
      child=subprocess.call(command)
      if(child==0):
         print(password)
         break

Different encryption methods are solved, but the greed is really terrible
repeated calls to the command line make me unhappy again

Pyzipper Library (Ultimate)

It happened that when checking the encryption method, I saw someone propose that
Python has a pyzipper library, which can be very compatible instead of zipfile, and can read and write AES encrypted zip files
all come here, there is no retreat

#Install pyzipper
PIP install pyzipper

There will be garbled Chinese name, remember to change it

f1 = open('D:\python\passdict4.txt','r')
with pyzipper.AESZipFile(file_path,'r') as f:
   for i in f1:
       i = i.rstrip('\n')
       f.pwd = str.encode(i)
       try:
           f.extractall(path=f"{root}")
           print(file_path+"\t the password is:"+i)
           break
       except Exception:
           pass
f.close()
f1.close()

This method is perfect
Click to download relevant codes, files and tools