Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'shortuuid' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
def post(self, request, pk):
namelist = list(filter(lambda x: x, map(lambda x: x.strip(), request.POST['list'].split('\n'))))
user_id = 1
contest = Contest.objects.get(pk=pk)
for name in namelist:
if name.startswith('*'):
comment = name[1:].strip()
star = True
else:
comment = name
star = False
password_gen = shortuuid.ShortUUID("23456789ABCDEF")
password = password_gen.random(8)
while True:
try:
username = self._get_username(pk, user_id)
email = '%s@fake.ecnu.edu.cn' % username
user = User.objects.create(username=username, email=email)
user.set_password(password)
user.save()
user.avatar.save('generated.png', Identicon(user.email).get_bytes())
ContestParticipant.objects.create(user=user, comment=comment, hidden_comment=password,
star=star, contest=contest)
break
except IntegrityError:
pass
user_id += 1
invalidate_contest(contest)
def setUp(self):
self.original_email_backend = settings.EMAIL_BACKEND
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
self.inviter = User.objects.create(username=shortuuid.uuid())
self.existing = User.objects.create(username=shortuuid.uuid(),
email='existing@example.com')
@staticmethod
def _create(contest, comments):
random_gen = shortuuid.ShortUUID()
ContestInvitation.objects.bulk_create(
[ContestInvitation(contest=contest, code=random_gen.random(12), comment=comment) for comment in comments])
def _create(contest, comments):
random_gen = shortuuid.ShortUUID()
ContestInvitation.objects.bulk_create(
[ContestInvitation(contest=contest, code=random_gen.random(12), comment=comment) for comment in comments])
def test_encoding(self):
su = ShortUUID()
u = UUID("{3b1f8b40-222c-4a6e-b77e-779d5a94e21c}")
self.assertEqual(su.encode(u), "CXc85b4rqinB7s5J52TRYb")
def test_consistency(self):
su = ShortUUID()
num_iterations = 1000
uid_lengths = defaultdict(int)
for count in range(num_iterations):
random_uid = uuid4()
encoded_random = su.encode(random_uid)
uid_lengths[len(encoded_random)] += 1
decoded_random = su.decode(encoded_random)
self.assertEqual(random_uid, decoded_random)
self.assertEqual(len(uid_lengths), 1)
uid_length = next(iter(uid_lengths.keys())) # Get the 1 value
self.assertEqual(uid_lengths[uid_length], num_iterations)
self.assertRaises(ValueError, lambda x: ShortUUID(x), "0")
def test_encoded_length(self):
su1 = ShortUUID()
self.assertEqual(su1.encoded_length(), 22)
base64_alphabet = string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/"
su2 = ShortUUID(base64_alphabet)
self.assertEqual(su2.encoded_length(), 22)
binary_alphabet = "01"
su3 = ShortUUID(binary_alphabet)
self.assertEqual(su3.encoded_length(), 128)
su4 = ShortUUID()
self.assertEqual(su4.encoded_length(num_bytes=8), 11)
def test_decoding(self):
su = ShortUUID()
u = UUID("{3b1f8b40-222c-4a6e-b77e-779d5a94e21c}")
self.assertEqual(su.decode("CXc85b4rqinB7s5J52TRYb"), u)
def test_alphabet(self):
alphabet = "01"
su1 = ShortUUID(alphabet)
su2 = ShortUUID()
self.assertEqual(alphabet, su1.get_alphabet())
su1.set_alphabet("01010101010101")
self.assertEqual(alphabet, su1.get_alphabet())
self.assertEqual(set(su1.uuid()), set("01"))
self.assertTrue(116 < len(su1.uuid()) < 140)
self.assertTrue(20 < len(su2.uuid()) < 24)
u = uuid4()
self.assertEqual(u, su1.decode(su1.encode(u)))
u = su1.uuid()
self.assertEqual(u, su1.encode(su1.decode(u)))