Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- from cStringIO import StringIO
- src = StringIO()
- p = pickle.Pickler(src)
- def persistent_id(obj):
- if hasattr(obj, 'x'):
- return 'the value %d' % obj.x
- else:
- return None
- p.persistent_id = persistent_id
- class Integer:
- def __init__(self, x):
- self.x = x
- def __str__(self):
- return 'My name is integer %d' % self.x
- i = Integer(7)
- print i
- p.dump(i)
- datastream = src.getvalue()
- print repr(datastream)
- dst = StringIO(datastream)
- up = pickle.Unpickler(dst)
- class FancyInteger(Integer):
- def __str__(self):
- return 'I am the integer %d' % self.x
- def persistent_load(persid):
- if persid.startswith('the value '):
- value = int(persid.split()[2])
- return FancyInteger(value)
- else:
- raise pickle.UnpicklingError, 'Invalid persistent id'
- up.persistent_load = persistent_load
- j = up.load()
- print j
- """
- $ pylint pickle_example_from_docs.py
- No config file found, using default configuration
- ************* Module pickle_example_from_docs
- C: 1, 0: Missing module docstring (missing-docstring)
- C: 4, 0: Invalid constant name "src" (invalid-name)
- C: 5, 0: Invalid constant name "p" (invalid-name)
- C: 7, 0: Missing function docstring (missing-docstring)
- C: 17, 8: Invalid attribute name "x" (invalid-name)
- C: 15, 0: Missing class docstring (missing-docstring)
- C: 15, 0: Old-style class defined. (old-style-class)
- C: 16, 4: Invalid argument name "x" (invalid-name)
- R: 15, 0: Too few public methods (0/2) (too-few-public-methods)
- C: 25, 0: Invalid constant name "datastream" (invalid-name)
- C: 27, 0: Invalid constant name "dst" (invalid-name)
- C: 29, 0: Invalid constant name "up" (invalid-name)
- C: 31, 0: Missing class docstring (missing-docstring)
- R: 31, 0: Too few public methods (0/2) (too-few-public-methods)
- C: 35, 0: Missing function docstring (missing-docstring)
- W: 40, 8: Use raise ErrorClass(args) instead of raise ErrorClass, args. (old-raise-syntax)
- Report
- ======
- 33 statements analysed.
- Raw metrics
- -----------
- +----------+-------+-------+---------+-----------+
- |type |number |% |previous |difference |
- +==========+=======+=======+=========+===========+
- |code |34 |100.00 |NC |NC |
- +----------+-------+-------+---------+-----------+
- |docstring |0 |0.00 |NC |NC |
- +----------+-------+-------+---------+-----------+
- |comment |0 |0.00 |NC |NC |
- +----------+-------+-------+---------+-----------+
- |empty |0 |0.00 |NC |NC |
- +----------+-------+-------+---------+-----------+
- Messages by category
- --------------------
- +-----------+-------+---------+-----------+
- |type |number |previous |difference |
- +===========+=======+=========+===========+
- |convention |13 |13 |= |
- +-----------+-------+---------+-----------+
- |refactor |2 |2 |= |
- +-----------+-------+---------+-----------+
- |warning |1 |1 |= |
- +-----------+-------+---------+-----------+
- |error |0 |0 |= |
- +-----------+-------+---------+-----------+
- Messages
- --------
- +-----------------------+------------+
- |message id |occurrences |
- +=======================+============+
- |invalid-name |7 |
- +-----------------------+------------+
- |missing-docstring |5 |
- +-----------------------+------------+
- |too-few-public-methods |2 |
- +-----------------------+------------+
- |old-style-class |1 |
- +-----------------------+------------+
- |old-raise-syntax |1 |
- +-----------------------+------------+
- Global evaluation
- -----------------
- Your code has been rated at 5.15/10
- Statistics by type
- ------------------
- +---------+-------+-----------+-----------+------------+---------+
- |type |number |old number |difference |%documented |%badname |
- +=========+=======+===========+===========+============+=========+
- |module |1 |1 |= |0.00 |0.00 |
- +---------+-------+-----------+-----------+------------+---------+
- |class |2 |2 |= |0.00 |0.00 |
- +---------+-------+-----------+-----------+------------+---------+
- |method |3 |3 |= |100.00 |0.00 |
- +---------+-------+-----------+-----------+------------+---------+
- |function |2 |2 |= |0.00 |0.00 |
- +---------+-------+-----------+-----------+------------+---------+
- Duplication
- -----------
- +-------------------------+------+---------+-----------+
- | |now |previous |difference |
- +=========================+======+=========+===========+
- |nb duplicated lines |0 |0 |= |
- +-------------------------+------+---------+-----------+
- |percent duplicated lines |0.000 |0.000 |= |
- +-------------------------+------+---------+-----------+
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement