
import os
import fcntl


class FileLock(object):

    def __init__(self, fd):

        # the fd is borrowed, so do not close it
        self.fd = fd

    def acquire(self, write=False, block=False):

        try:

            lock_flags = fcntl.LOCK_EX if write else fcntl.LOCK_SH

            if not block:

                lock_flags |= fcntl.LOCK_NB

            fcntl.flock(self.fd, lock_flags)

            return True

        except IOError:
            
            return False

    def release(self):

        fcntl.flock(self.fd, fcntl.LOCK_UN)





import os
import fcntl


class FileLock(object):

    def __init__(self, fd):

        # the fd is borrowed, so do not close it
        self.fd = fd

    def acquire(self, ex=False, nb=True):
        """
        Acquire a lock on the fd.

        :param ex (optional): default False, acquire a exclusive lock if True
        :param nb (optional): default True, non blocking if True
        :return: True if acquired

        """

        try:

            lock_flags = fcntl.LOCK_EX if ex else fcntl.LOCK_SH

            if nb:

                lock_flags |= fcntl.LOCK_NB

            fcntl.flock(self.fd, lock_flags)

            return True

        except IOError:
            
            return False

    def release(self):
        """
        Release lock on the fd.

        """

        fcntl.flock(self.fd, fcntl.LOCK_UN)




Change parameter names in acquire. Add some doc.
Change parameter names in acquire.
Add some doc.

  
  import os
  import fcntl
  
  
  class FileLock(object):
  
      def __init__(self, fd):
  
          # the fd is borrowed, so do not close it
          self.fd = fd
  
-     def acquire(self, write=False, block=False):
+     def acquire(self, ex=False, nb=True):
+         """
+         Acquire a lock on the fd.
+ 
+         :param ex (optional): default False, acquire a exclusive lock if True
+         :param nb (optional): default True, non blocking if True
+         :return: True if acquired
+ 
+         """
  
          try:
  
-             lock_flags = fcntl.LOCK_EX if write else fcntl.LOCK_SH
+             lock_flags = fcntl.LOCK_EX if ex else fcntl.LOCK_SH
  
-             if not block:
+             if nb:
  
                  lock_flags |= fcntl.LOCK_NB
  
              fcntl.flock(self.fd, lock_flags)
  
              return True
  
          except IOError:
              
              return False
  
      def release(self):
+         """
+         Release lock on the fd.
+ 
+         """
  
          fcntl.flock(self.fd, fcntl.LOCK_UN)
  
  
  
  
Change parameter names in acquire. Add some doc.
## Code Before:

import os
import fcntl


class FileLock(object):

    def __init__(self, fd):

        # the fd is borrowed, so do not close it
        self.fd = fd

    def acquire(self, write=False, block=False):

        try:

            lock_flags = fcntl.LOCK_EX if write else fcntl.LOCK_SH

            if not block:

                lock_flags |= fcntl.LOCK_NB

            fcntl.flock(self.fd, lock_flags)

            return True

        except IOError:
            
            return False

    def release(self):

        fcntl.flock(self.fd, fcntl.LOCK_UN)




## Instruction:
Change parameter names in acquire. Add some doc.
## Code After:

import os
import fcntl


class FileLock(object):

    def __init__(self, fd):

        # the fd is borrowed, so do not close it
        self.fd = fd

    def acquire(self, ex=False, nb=True):
        """
        Acquire a lock on the fd.

        :param ex (optional): default False, acquire a exclusive lock if True
        :param nb (optional): default True, non blocking if True
        :return: True if acquired

        """

        try:

            lock_flags = fcntl.LOCK_EX if ex else fcntl.LOCK_SH

            if nb:

                lock_flags |= fcntl.LOCK_NB

            fcntl.flock(self.fd, lock_flags)

            return True

        except IOError:
            
            return False

    def release(self):
        """
        Release lock on the fd.

        """

        fcntl.flock(self.fd, fcntl.LOCK_UN)



