hashids-python | Implementation of hashids ( http : //hashids | Build Tool library
kandi X-RAY | hashids-python Summary
kandi X-RAY | hashids-python Summary
Implementation of hashids (http://hashids.org) in Python. Compatible with Python 2 and Python 3
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Initialize this class with the given parameters .
- Private helper to encode a set of values .
- Ensure length .
- Decode a hashid .
- Reorder a string .
- Wraps a deprecated method .
- Split a string .
- unhash a given character
- Hash a number .
- is an unsigned integer .
hashids-python Key Features
hashids-python Examples and Code Snippets
Community Discussions
Trending Discussions on hashids-python
QUESTION
I'm writing a simple flask-restful API and I need to insert some resource into database. I want to have hash id visible in the URL like this /api/resource/hSkR3V9aS
rather than just simple auto-increment id /api/resource/34
My first thought was to use Hashids and just generate the hash_id
from auto-increment id
and store both values in the database, but the problem is that I would have to first INSERT
new row of data, GET
the id
and then UPDATE
the hash_id
field.
Second attempt was to generate hash_id
(e.g. sha1) not from id
but some other field that I'm passing to databse and use it as a primary key (get rid of auto-inc id), but I fear that searching and comparing string each time rather than int will be much, much slower.
What is the best way to achive desired hash_id
based URL along with acceptable speed of database SELECT
queries?
I think this is the most related stack question, but it doesn't answer my question.
Major technology details: Python 3.6, flask_mysqldb library, MySQL database Please let me know if I ommited some information and I will provide it.
...ANSWER
Answered 2017-Oct-02 at 13:32I think I found a decent solution myself in this answer
Use
cursor.lastrowid
to get the last row ID inserted on the cursor object, orconnection.insert_id()
to get the ID from the last insert on that connection.
It's per-connection based so there is no fear that I'll have 2 rows with the same ID.
I'll now use previously mentioned by myself Hashids and return hashed value to client. Hashids can be also decoded and I'll do it each time I get a request from url with this hash id included.
Also I found out that MongoDB database generates this kind of hashed id by itself, maybe this is a solution for someone else with similar problem.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install hashids-python
You can use hashids-python like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page