I want to generate unique uuids using uuid5 method in python. I am passing namespace as 'uuid.NAMESPACE_DNS' and name string as 'serial_number'.
The problem here is that namespace is constant and serial_number for each device is different. Now, when the application runs at different times it generate same uuid because namespace and name serial in input args become same.
I am thinking of adding timestamp in name string passed to uniquely identify each uuid.
Can you please suggest me any better alternative to always generate unique uuids. I am open to modify name argument in uuid5 method?
Python Code:
import uuid
def unique_uuid(self, *args):
uuidStr = ''
for arg in args:
uuidStr += '-'.join(arg)
return str(uuid.uuid5(uuid.NAMESPACE_DNS, str(uuidStr)))
# The serials are different for each device and doesn't change with time.
# Hence at different time same uuid are generated for same device.
# But I need to always have unique uuids regardless of serial or any input.
# Only uuid5 method is allowed for final uuid generation.
serials = ["abc123", "xyz456"]
print(unique_uuid(serials))
Example of same uuid at different timestamp using uuid5 method.
Related
I'm needing to both validate a list of UUID's as well as determine the version. For example, using https://www.beautifyconverter.com/uuid-validator.php and entering 25CCCA6F-1568-473E-BFED-EC08C31532C6 I can determine that it is both valid, and version 4. I see from https://www.snip2code.com/Snippet/12614/Validating-a-uuid4-with-Python- and How to determine if a string is a valid v4 UUID? that the UUID module can validate one at a time, or test for a particular version, but not sure if UUID will test for all 4 versions and return the version.
Here is how I would iterative over a list of potential UUIDs and return a parallel list with either the version number (if valid) or None otherwise.
Note especially that the UUID constructor accepts UUID strings of any version. If the string is valid, you can query the .version member to determine the version.
from uuid import UUID
def version_uuid(uuid):
try:
return UUID(uuid).version
except ValueError:
return None
def version_list(l):
return [version_uuid(uuid) for uuid in l]
if __name__=="__main__":
uuids = (
'0d14fbaa-8cd6-11e7-b2ed-28d244cd6e76',
'6fa459ea-ee8a-3ca4-894e-db77e160355e',
'16583cd3-8361-4fe6-a345-e1f546b86b74',
'886313e1-3b8a-5372-9b90-0c9aee199e5d',
'0d14fbaa-8cd6-11e7-b2ed-28d244cd6e7',
'6fa459ea-ee8a-3ca4-894e-db77e160355',
'16583cd3-8361-4fe6-a345-e1f546b86b7',
'886313e1-3b8a-5372-9b90-0c9aee199e5',
'481A8DE5-F0D1-E211-B425-E41F134196DA',
)
assert version_list(uuids) == [1,3,4,5,None,None,None,None,14]
def validate_uuid4(uuid_string):
try:
val = UUID(uuid_string, version=4)
except ValueError:
# If it's a value error, then the string
# is not a valid hex code for a UUID.
return False
return True
You can use the above function to go through your list of uuid string and it will tell you whether a particular string in the list if a valid version 4 uuid
I get the following error when trying to make an online prediction on my ML Engine model.
The key "values" is not correct. (See error on image.)
enter image description here
I already tested with RAW image data : {"image_bytes":{"b64": base64.b64encode(jpeg_data)}}
& Converted the data to a numpy array.
Currently I have the following code:
from googleapiclient import discovery
import base64
import os
from PIL import Image
import json
import numpy as np
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/jacob/Desktop/******"
def predict_json(project, model, instances, version=None):
"""Send json data to a deployed model for prediction.
Args:
project (str): project where the Cloud ML Engine Model is deployed.
model (str): model name.
instances ([Mapping[str: Any]]): Keys should be the names of Tensors
your deployed model expects as inputs. Values should be datatypes
convertible to Tensors, or (potentially nested) lists of datatypes
convertible to tensors.
version: str, version of the model to target.
Returns:
Mapping[str: any]: dictionary of prediction results defined by the
model.
"""
# Create the ML Engine service object.
# To authenticate set the environment variable
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
service = discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)
if version is not None:
name += '/versions/{}'.format(version)
response = service.projects().predict(
name=name,
body={'instances': instances}
).execute()
if 'error' in response:
raise RuntimeError(response['error'])
return response['predictions']
savepath = 'upload/11277229_F.jpg'
img = Image.open('test/01011000/11277229_F.jpg')
test = img.resize((299, 299))
test.save(savepath)
img1 = open(savepath, "rb").read()
def load_image(filename):
with open(filename) as f:
return np.array(f.read())
predict_json('image-recognition-25***08', 'm500_200_waug', [{"values": str(base64.b64encode(img1).decode("utf-8")), "key": '87'}], 'v1')
The error message itself indicates (as you point out in the question), that the key "values" is not one of the inputs specified in the model. To inspect the model's input, use saved_model_cli show --all --dir=/path/to/model. That will show you a list of the names of the inputs. You'll need to use the correct name.
That said, it appears there is another issue. It's not clear from the question what type of input your model is expecting, though it's likely one of two things:
A matrix of integers or floats
A byte string with the raw image file
contents.
The exact solution will depend on which of the above your exported model is using. saved_model_cli will help here, based on the type and shape of the input. It will either be DT_FLOAT32 (or some other int/float type) and [NONE, 299, 299, CHANNELS] or DT_STRING and [NONE], respectively.
If your model is type (1), then you will need to send a matrix of ints/floats (which does not use base64 encoding):
predict_json('image-recognition-25***08', 'm500_200_waug', [{CORRECT_INPUT_NAME: load_image(savepath).tolist(), "key": '87'}], 'v1')
Note the use of tolist to convert the numpy array to a list of lists.
In the case of type (2), you need to tell the service you have some base64 data by adding in {"b64": ...}:
predict_json('image-recognition-25***08', 'm500_200_waug', [{CORRECT_INPUT_NAME: {"b64": str(base64.b64encode(img1).decode("utf-8"))}, "key": '87'}], 'v1')
All of this, of course, depends on using the correct name for CORRECT_INPUT_NAME.
One final note, I'm assuming your model actually does have key as an additional inputs since you included it in your request; again, that can all be verified against the output of saved_model_cli show.
I used to get this errors too. If anyone comes across this error, and using gcloud.
Tensors are automatically called csv_rows. For example this works for me now
"instances": [{
"csv_row": "STRING,7,4.02611534,9,14,0.66700000,0.17600000,0.00000000,0.00000000,1299.76500000,57",
"key": "0"
}]
I want to send the email to the user that will contains url+hash
like this bleow
www.mywebsite.com/user/verify/121#$%3h2%^1kj3#$h2kj1h%$3kj%$21h
and save this hash against the user in the Database like this
ID | Email |Hash
1 | youremail#gmail.com |121#$%3h2%^1kj3#$h2kj1h%$3kj%$21h
When the user received the email it should check and compare the hash with it and perform the action as per situation.
My question is simple how to generate a unique hash for each user and how to store them in the Database.
If by "hash", you mean a unique string, you can just use uuid.uuid4 for that purpose.
>>> import uuid
>>> unique_id = str(uuid.uuid4())
>>> print unique_id
d8814205-f11e-46e1-925e-a878fc75cb8d
>>> # replace dashes, if you like
>>> unique_id.replace("-", "")
I've used this for projects where I need to verify a user's email.
P.S.: It's not called a hash, it's called a unique ID. Hashing is something else, where you generate a value from a given string. See this question for more explanation.
Django has a Cryptographic Signing module, which helps produce unique and verifiable signatures for any data you need. If you are trying to do this to verify that the request is done by the appropriate user or not, you can use the library to verify requests, without storing the hash in the database.
I would like to keep the original file name of an UploadedFile in Django that has its location stored in a FileField. Right now I am observing that if two files have the same name, the first file uploaded keeps its original name but the second time a file with that name is uploaded, it has a random string appended to make the file name unique. One solution is to add an additional field to the model: Django: How to save original filename in FileField? or Saving Original File Name in Django with FileField but these solutions seem suboptimal as they require changing the Model fields.
An alternative would be to prepend a random directory path to the front of the file make sure that in a given directory the file name is unique and allowing the basename to remain unchanged. One way to do this would be to pass in a callable upload_to that does just that. Another option would be to subclass FileField and override get_filename to not strip the input filename to the basename allowing the caller to pass in a filename with a prepended path. The latter option is not ideal if I want to use an ImageField as I would have to subclass that as well.
In looking at the code that actually generates the unique filename by appending the random string, it looks like the best solution to this problem might be to subclass the Storage class in-use and override get_available_name method to create unique filenames by prepending a directory rather than post-pending the string to the base name.
Sorry for the quick answere, here is another approach to your question :
The idea here is to create an unique folder for each uploaded file.
# in your settings.py file
MY_FILE_PATH = 'stored_files/'
The path were your files will be stored : /public/media/stored_files
# somewhere in your project create an utils.py file
import random
try:
from hashlib import sha1 as sha_constructor
except ImportError:
from django.utils.hashcompat import sha_constructor
def generate_sha1(string, salt=None):
"""
Generates a sha1 hash for supplied string.
:param string:
The string that needs to be encrypted.
:param salt:
Optionally define your own salt. If none is supplied, will use a random
string of 5 characters.
:return: Tuple containing the salt and hash.
"""
if not isinstance(string, (str, unicode)):
string = str(string)
if isinstance(string, unicode):
string = string.encode("utf-8")
if not salt:
salt = sha_constructor(str(random.random())).hexdigest()[:5]
hash = sha_constructor(salt+string).hexdigest()
return (salt, hash)
In your models.py
from django.conf import settings
from utils.py import generate_sha1
def upload_to_unqiue_folder(instance, filename):
"""
Uploads a file to an unique generated Path to keep the original filename
"""
salt, hash = generate_sha1('{}{}'.format(filename, get_datetime_now().now))
return '%(path)s%(hash_path)s%(filename)s' % {'path': settings.MY_FILE_PATH,
'hash_path': hash[:10],
'filename': filename}
#And then add in your model fileField the uplaod_to function
class MyModel(models.Model):
file = models.FileField(upload_to=upload_to_unique_folder)
The file will be uploaded to this location :
public/media/stored_file_path/unique_hash_folder/my_file.extention
Note : I got the code from Django userena sources, and adapted it to my needs
Note2 : For more informations take a look at this greate post on Django File upload : File upload example
Have a good day.
Edit : Trying to provide a working solution :)
To my understanding, during the form submission/file upload process, you can add form validation functions.
During the validation and cleaning process, you could check that the database does not already have a duplicate name (ie. query to see if that file name exists).
If it is duplicate, you could just rename it xyz_1, xyz_2, etc
I want to set the "default" value as a randomly generated String for the promotion_code part of my Promotion model, for that the code_generate function is used.
The issue with the code below that it seems like default=code_generate() generates this random string once every server start thus assigning the same value. I can see that by the admin panel, every time I try to generate a new Promotion, it gives me the exact same string.
#generate a string, which is not already existing in the earlier Promotion instances
def code_generate():
while 1:
from django.conf import settings
import random, string
prom_code = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
try:
Promotion.objects.get(promotion_code=prom_code)
except:
return prom_code
class Promotion(models.Model):
purchase = models.ForeignKey('Purchase')
promotion_code = models.CharField(max_length=20,unique=True,default=code_generate())
How can I make it random ?
Regards
You need to pass a callable as default, not call the callable:
promotion_code = models.CharField(max_length=20,unique=True,default=code_generate)
As indicated in the other answer, the simplest way to get a random string is as follows:
str(random.random())[2:]
Altho' it is a string of numbers. Fair enough, until you would want to replace it eventually with sha.