import random import string # The pool of characters to pick the characters for your password from password_char_set = string.ascii_letters + string.digits + string.punctuation def random_character(): return password_char_set[random.randint(0, len(password_char_set))]
- Seattle, Washington
- Sign in to view email
I recently came across the need to spawn multiple threads, each of which needs to write to the same file. Since the file will experience contention from multiple resources, we need to guarantee thread-safety.
NOTE: The following examples work with Python 3.x. To execute the following programs using Python 2.7, please replace
thread.get_ident(). As a result, you would need to import
thread and not
- (The following example will take a very long time). It will create 200 threads, each of which will wait until a global lock is available for acquisition.
# threading_lock.py import threading
Ruby: Different ways to define methods in
The Ruby Object Model
One of the central aspects of the Ruby programming language is the Ruby object model, as per which everything (but for constructs such as methods, and keywords) in Ruby -- classes, instances, lambdas, procs, strings, numbers, decimals, hashmaps -- is an object. This renders a kind of uniformity to Ruby that few other languages offer.
At the very same time, the object model can also become confusing. In particular, the following two questions can sometimes become difficult to answer, but being able to readily answer which can set you apart as a Ruby programmer.
- What is self in a given context?
- If I define a method here, where will it go? That is, which object is it going to be defined on?