Precautions when using pit in Python

Try using pit and get hooked

A pit that does not write an account ID or password in the source and is prompted at runtime. It's a very good library for security, but I'm addicted to it when using it in Python, so make a note of it.

Click here for the python and pip versions I tried.

$ python -V
Python 2.7.9

$ pip freeze | grep pit
pit==0.3

Editor does not start at runtime

Click here for the minimum description.

import pit

opts = {
    'require': {
        'id': 'your id',
        'password': 'your password',
    }
}
account = pit.Pit.get('test.account', opts)

print account['id']
print account['password']

But when I actually run it, I get an error. For some reason, the editor does not start the first time it is executed. Since you have not entered both id and password, naturally you will get a KeyError when getting the value.

>>> import pit
>>>
>>> opts = {
...     'require': {
...         'id': 'your id',
...         'password': 'your password',
...     }
... }
>>> account = pit.Pit.get('test.account', opts)
>>> print account['id']

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'id'

In the default state, no editor was specified, so I had to specify vi.

if not os.environ.get('EDITOR'):
    os.environ['EDITOR'] = 'vi'

Correct and execute. vi was started, it became possible to input, and it ended normally.

>>> import pit
>>> import os
>>>
>>> if not os.environ.get('EDITOR'):
...     os.environ['EDITOR'] = 'vi'
...
>>> opts = {
...     'require': {
...         'id': 'your id',
...         'password': 'your password',
...     }
... }
>>> account = pit.Pit.get('test.account', opts)
>>> print account['id']
hoge
>>> print account['password']
hogehoge

Mysterious characters (!! python / unicode) are displayed in the editor

When you do this ...

from __future__ import absolute_import
from __future__ import unicode_literals

import pit
import os

if not os.environ.get('EDITOR'):
    os.environ['EDITOR'] = 'vi'

opts = {
    'require': {
        'id': 'your id',
        'password': 'your password',
    }
}
account = pit.Pit.get('test.account', opts)

print account['id']
print account['password']
!!python/unicode 'id': !!python/unicode 'your id'
!!python/unicode 'password': !!python/unicode 'your password'

A mysterious character is added when entering id and password in vi. In conclusion, the problem was that this line was there, so deleting it will solve it.

from __future__ import unicode_literals

If you pass the unicode character as an argument, the character "!! python / unicode" will be displayed. So, even if you don't specify unicode_literals, the same phenomenon will occur even if you write it like this.

opts = {
    u'require': {
        u'id': u'your id',
        u'password': u'your password',
    }
}
account = pit.Pit.get('test.account', opts)

Recommended Posts

Precautions when using pit in Python
Precautions when using phantomjs from python
Precautions when using six with Python 2.5
When using regular expressions in Python
Precautions when pickling a function in python
Precautions when using for statements in pandas
Precautions when using Chainer
Precautions when dealing with control structures in Python 2.6
Attention when os.mkdir in Python
Translate using googletrans in Python
Using Python mode in Processing
Initial settings when using the foursquare API in python
Precautions when dealing with ROS MultiArray types in Python
How to exit when using Python in Terminal (Mac)
GUI programming in Python using Appjar
Precautions when using TextBlob trait analysis
Behavior when listing in Python heapq
Precautions when using codecs and pandas
Try using LevelDB in Python (plyvel)
A memo when creating a directed graph using Graphviz in Python
Precautions when using the urllib.parse.quote function
Using global variables in python functions
[Python] Be careful when using print
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Solve the Japanese problem when using the CSV module in Python.
Precautions when creating a Python generator
Things to keep in mind when using Python with AtCoder
Scripts that can be used when using bottle in Python
Things to keep in mind when using cgi with python.
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using MeCab with virtualenv python
[VS Code] ~ Tips when using python ~
When writing a program in Python
GUI creation in python using tkinter 2
Things to watch out for when using default arguments in Python
[Python ORM] Notation when writing SQL using subquery in IN clause in SQLAlchemy
Precautions when passing def to sorted and groupby functions in Python? ??
I get a can't set attribute when using @property in python
Precautions when giving default values to arguments in Python function definitions
[SEO] Flow / sample code when using Google Analytics API in Python
When specifying multiple keys in python sort
Mouse operation using Windows API in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
GUI creation in python using tkinter part 1
Periodic execution processing when using tkinter [Python3]
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
Create ScriptableObject in Python when building ADX2
[FX] Hit oanda-API in Python using Docker
Precautions when solving DP problems with Python
Tweet using the Twitter API in Python
Favicon placement (when using Python, Flask, Heroku)
[python, multiprocessing] Behavior for exceptions when using multiprocessing
[Python] [Windows] Serial communication in Python using DLL